Skip to content

Commit fac1a80

Browse files
author
Konstantinos Familonidis
committed
Fixes #36106: PF5 Refactor - EditorNavbar, EditorOption
Upgrade NavBar (Alert, Button), Options (RadioButton, View), Settings. Remove enzyme in tests including DiffView, HostSelect, EditorView. Keep actions, and selectors tests.
1 parent 2e415fc commit fac1a80

33 files changed

+957
-2169
lines changed

webpack/assets/javascripts/react_app/components/ConfigReports/DiffModal/__tests__/DiffModal.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const fixtures = {
1515
changeViewType: changeState,
1616
},
1717
};
18-
configure({ testIdAttribute: 'data-ouia-component-id'})
18+
configure({ testIdAttribute: 'data-ouia-component-id' });
1919

2020
describe('DiffModal', () => {
2121
describe('rendering', () => {

webpack/assets/javascripts/react_app/components/DiffView/Diff.fixtures.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export const patchMock = {
3030
};
3131

3232
export const fixtures = {
33-
'render DiffView w/oldText & newText': diffMock,
34-
'render DiffView w/Patch': patchMock,
33+
diffMock,
34+
patchMock,
3535
};
3636

3737
export const PF_SELECTED = 'pf-m-selected';
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
import React from 'react';
12
import { fixtures } from './Diff.fixtures';
2-
import { testComponentSnapshotsWithFixtures } from '../../common/testHelpers';
3+
import { render, screen } from '@testing-library/react';
4+
import '@testing-library/jest-dom/extend-expect';
35

46
import DiffView from './DiffView';
57

68
describe('DiffView', () => {
7-
describe('rendering', () =>
8-
testComponentSnapshotsWithFixtures(DiffView, fixtures));
9+
describe('rendering', () => {
10+
it('render DiffView w/oldText & newText', () => {
11+
render(<DiffView {...fixtures.diffMock} />)
12+
const table = screen.getByRole('table')
13+
14+
expect(table).toHaveClass("diff-split")
15+
expect(screen.getByText('hello there friend')).toBeInTheDocument()
16+
})
17+
18+
it('render DiffView w/Patch', () => {
19+
render(<DiffView {...fixtures.patchMock} />)
20+
const table = screen.getByRole('table')
21+
22+
expect(table).toHaveClass("diff-unified")
23+
expect(screen.getByText('fooo')).toBeInTheDocument()
24+
})
25+
})
926
});

webpack/assets/javascripts/react_app/components/DiffView/__snapshots__/DiffView.test.js.snap

Lines changed: 0 additions & 90 deletions
This file was deleted.

webpack/assets/javascripts/react_app/components/Editor/Editor.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import { Alert, AlertActionCloseButton } from '@patternfly/react-core';
4-
54
import { noop } from '../../common/helpers';
65
import DiffView from '../DiffView/DiffView';
76
import EditorView from './components/EditorView';
@@ -11,6 +10,7 @@ import {
1110
EDITOR_THEMES,
1211
EDITOR_KEYBINDINGS,
1312
EDITOR_MODES,
13+
EDITOR_TAB_NAMES,
1414
} from './EditorConstants';
1515
import './editor.scss';
1616

@@ -185,19 +185,19 @@ class Editor extends React.Component {
185185
{...editorViewProps}
186186
key="editorPreview"
187187
name={editorNameTab.preview}
188-
isSelected={selectedView === 'preview'}
188+
isSelected={selectedView === EDITOR_TAB_NAMES.preview}
189189
className="ace_editor_form ace_preview"
190190
/>
191191
<EditorView
192192
{...editorViewProps}
193193
key="editorCode"
194194
name={editorNameTab.input}
195-
isSelected={selectedView === 'input'}
195+
isSelected={selectedView === EDITOR_TAB_NAMES.input}
196196
className="ace_editor_form ace_input"
197197
/>
198198
<div
199199
id="diff-table"
200-
className={selectedView === 'diff' ? '' : 'hidden'}
200+
className={selectedView === EDITOR_TAB_NAMES.diff ? '' : 'hidden'}
201201
>
202202
<DiffView
203203
oldText={template || ''}

webpack/assets/javascripts/react_app/components/Editor/EditorConstants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const EDITOR_HOST_SELECT_CLEAR = 'EDITOR_HOST_SELECT_CLEAR';
2020
export const EDITOR_HOST_SELECT_RESET = 'EDITOR_HOST_SELECT_RESET';
2121
export const EDITOR_HOST_INITIAL_FETCH = 'EDITOR_HOST_INITIAL_FETCH';
2222
export const EDITOR_CHANGE_KIND = 'EDITOR_CHANGE_KIND';
23+
export const EDITOR_TAB_NAMES = { input: 'input', diff: 'diff', preview: 'preview' };
2324

2425
export const EDITOR_HOSTS_URL = '/hosts/preview_host_collection.json';
2526
export const EDITOR_HOST_ARR = 'hosts';
Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
2-
import { act } from '@testing-library/react';
3-
import { mount } from 'enzyme';
4-
import { testComponentSnapshotsWithFixtures } from '../../../common/testHelpers';
2+
import { render, screen, act, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom/extend-expect';
4+
55
import Editor from '../Editor';
66
import { editorOptions } from '../Editor.fixtures';
77

@@ -14,50 +14,67 @@ const didMountStubs = () => ({
1414
});
1515

1616
const fixtures = {
17-
'renders editor': editorOptions,
17+
renders: editorOptions,
1818
};
1919

20+
const renderEditor = (props = fixtures.renders) => render(<Editor {...props} />);
21+
2022
describe('Editor', () => {
21-
jest.useFakeTimers();
22-
describe('rendering', () =>
23-
testComponentSnapshotsWithFixtures(Editor, fixtures));
23+
describe('rendering', () => {
24+
const getAceEditors = () => screen.getAllByRole('textbox', { name: 'Cursor at row 1' })
25+
26+
it('renders', () => {
27+
renderEditor();
28+
29+
expect(screen.getByText('<? />')).toBeInTheDocument();
30+
expect(getAceEditors().length).toBe(2);
31+
});
32+
it('re-renders', () => {
33+
const { rerender } = renderEditor();
34+
const props = { ...editorOptions, ...didMountStubs() };
35+
rerender(<Editor {...props} />);
2436

37+
expect(getAceEditors().length).toBe(2);
38+
});
39+
});
2540
describe('triggering', () => {
41+
const getTabById = id =>
42+
screen.getAllByRole('presentation', { current: "page" })
43+
.find(tab => (tab.getAttribute('id') || '').match(id));
44+
2645
it('should trigger input view', async () => {
2746
const props = { ...editorOptions, ...didMountStubs() };
28-
const component = mount(<Editor {...props} />);
29-
await act(async () => jest.advanceTimersByTime(1000));
30-
expect(
31-
component
32-
.find('li[role="presentation"]')
33-
.at(0)
34-
.hasClass('active')
35-
).toBe(true);
47+
renderEditor(props);
48+
const inputTab = getTabById('input-navitem');
49+
50+
expect(inputTab).toBeInTheDocument();
51+
expect(inputTab.parentElement).toHaveClass('pf-m-current');
52+
expect(inputTab).toHaveTextContent('Editor');
3653
});
3754
it('should trigger input view with no template', async () => {
3855
const props = {
3956
...editorOptions,
4057
...didMountStubs(),
4158
data: { ...editorOptions.data, template: null },
4259
};
43-
const component = mount(<Editor {...props} />);
44-
await act(async () => jest.advanceTimersByTime(1000));
45-
expect(component.props().template).toBe('<? />');
60+
renderEditor(props);
61+
const aceEditors = document.querySelectorAll('.ace_editor_form');
62+
const hasTemplateText = Array.from(aceEditors).some(container => container.textContent.includes('<? />'));
63+
64+
expect(hasTemplateText).toBe(false);
4665
});
4766
it('should trigger diff view', async () => {
4867
const props = {
4968
...editorOptions,
5069
...didMountStubs(),
5170
selectedView: 'diff',
5271
};
53-
const component = mount(<Editor {...props} />);
54-
await act(async () => jest.advanceTimersByTime(1000));
55-
expect(
56-
component
57-
.find('li[role="presentation"]')
58-
.at(1)
59-
.hasClass('active')
60-
).toBe(true);
72+
renderEditor(props);
73+
const diffTab = getTabById('diff-navitem');
74+
75+
expect(diffTab).toBeInTheDocument();
76+
expect(diffTab.parentElement).toHaveClass('pf-m-current');
77+
expect(diffTab).toHaveTextContent('Changes');
6178
});
6279
it('should trigger preview view', async () => {
6380
const props = {
@@ -66,18 +83,14 @@ describe('Editor', () => {
6683
selectedView: 'preview',
6784
isRendering: true,
6885
};
69-
const wrapper = mount(<Editor {...props} />);
70-
wrapper.find('button.close').simulate('click');
71-
await act(async () => jest.advanceTimersByTime(1000));
72-
const component = mount(<Editor {...props} />);
73-
await act(async () => jest.advanceTimersByTime(1000));
74-
75-
expect(
76-
component
77-
.find('li[role="presentation"]')
78-
.at(2)
79-
.hasClass('active')
80-
).toBe(true);
86+
renderEditor(props);
87+
const closeButton = screen.queryByLabelText(/close danger alert/i);
88+
89+
if (closeButton) await act(async () => await fireEvent.click(closeButton));
90+
const previewTab = getTabById('preview-navitem');
91+
92+
expect(previewTab).toBeInTheDocument();
93+
expect(previewTab.parentElement).toHaveClass('pf-m-current');
8194
});
8295
});
8396
it('should trigger hidden value editor', async () => {
@@ -88,20 +101,29 @@ describe('Editor', () => {
88101
isRendering: true,
89102
isMasked: true,
90103
};
91-
const wrapper = mount(<Editor {...props} />);
92-
await act(async () => jest.advanceTimersByTime(1000));
93-
expect(wrapper.find('.mask-editor').exists()).toBe(true);
104+
renderEditor(props);
105+
const maskedEditor = document.querySelector('.mask-editor');
106+
107+
expect(maskedEditor).toBeInTheDocument();
94108
});
95109
it('textarea disappears if readOnly', async () => {
110+
const getTextAreasHidden = () => document.querySelectorAll('textarea.hidden')
96111
const props = {
97112
...editorOptions,
98113
...didMountStubs(),
99114
selectedView: 'input',
115+
readOnly: false,
100116
};
101-
const wrapper = mount(<Editor {...props} />);
102-
await act(async () => jest.advanceTimersByTime(1000));
103-
expect(wrapper.find('textarea.hidden').exists()).toBe(true);
104-
wrapper.setProps({ readOnly: true });
105-
expect(wrapper.find('textarea.hidden').exists()).toBe(false);
117+
const { rerender } = renderEditor(props);
118+
119+
expect(getTextAreasHidden().length).toBe(1);
120+
121+
const newProps = {
122+
...props,
123+
readOnly: true,
124+
};
125+
rerender(<Editor {...newProps} />);
126+
127+
expect(getTextAreasHidden().length).toBe(0);
106128
});
107129
});

0 commit comments

Comments
 (0)