Skip to content

Commit 76115b3

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. Keep the store actions, selectors tests.
1 parent 0fe6e1e commit 76115b3

26 files changed

+574
-1858
lines changed

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: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
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+
})
16+
17+
it('render DiffView w/Patch', () => {
18+
render(<DiffView {...fixtures.patchMock} />)
19+
const table = screen.getByRole('table')
20+
21+
expect(table).toHaveClass("diff-unified")
22+
})
23+
})
924
});

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

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 69 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,68 @@ 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} />);
36+
37+
expect(getAceEditors().length).toBe(2);
38+
});
39+
});
2440

2541
describe('triggering', () => {
42+
const getTabById = id =>
43+
screen.getAllByRole('presentation', { current: "page" })
44+
.find(tab => tab.getAttribute('id') === id)
45+
2646
it('should trigger input view', async () => {
2747
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);
48+
renderEditor(props);
49+
const inputTab = getTabById('input-navitem');
50+
51+
expect(inputTab).toBeInTheDocument();
52+
expect(inputTab).toHaveClass('pf-m-current');
53+
expect(inputTab).toHaveTextContent('Editor');
3654
});
3755
it('should trigger input view with no template', async () => {
3856
const props = {
3957
...editorOptions,
4058
...didMountStubs(),
4159
data: { ...editorOptions.data, template: null },
4260
};
43-
const component = mount(<Editor {...props} />);
44-
await act(async () => jest.advanceTimersByTime(1000));
45-
expect(component.props().template).toBe('<? />');
61+
renderEditor(props);
62+
const aceEditors = document.querySelectorAll('.ace_editor_form');
63+
const hasTemplateText = Array.from(aceEditors).some(container => container.textContent.includes('<? />'));
64+
65+
expect(hasTemplateText).toBe(false);
4666
});
4767
it('should trigger diff view', async () => {
4868
const props = {
4969
...editorOptions,
5070
...didMountStubs(),
5171
selectedView: 'diff',
5272
};
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);
73+
renderEditor(props);
74+
const diffTab = getTabById('diff-navitem');
75+
76+
expect(diffTab).toBeInTheDocument();
77+
expect(diffTab).toHaveClass('pf-m-current');
78+
expect(diffTab).toHaveTextContent('Changes');
6179
});
6280
it('should trigger preview view', async () => {
6381
const props = {
@@ -66,18 +84,14 @@ describe('Editor', () => {
6684
selectedView: 'preview',
6785
isRendering: true,
6886
};
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);
87+
renderEditor(props);
88+
const closeButton = screen.queryByLabelText(/close danger alert/i);
89+
90+
if (closeButton) await act(async () => await fireEvent.click(closeButton));
91+
const previewTab = getTabById('preview-navitem');
92+
93+
expect(previewTab).toBeInTheDocument();
94+
expect(previewTab).toHaveClass('pf-m-current');
8195
});
8296
});
8397
it('should trigger hidden value editor', async () => {
@@ -88,20 +102,29 @@ describe('Editor', () => {
88102
isRendering: true,
89103
isMasked: true,
90104
};
91-
const wrapper = mount(<Editor {...props} />);
92-
await act(async () => jest.advanceTimersByTime(1000));
93-
expect(wrapper.find('.mask-editor').exists()).toBe(true);
105+
renderEditor(props);
106+
const maskedEditor = document.querySelector('.mask-editor');
107+
108+
expect(maskedEditor).toBeInTheDocument();
94109
});
95110
it('textarea disappears if readOnly', async () => {
111+
const getTextAreasHidden = () => document.querySelectorAll('textarea.hidden')
96112
const props = {
97113
...editorOptions,
98114
...didMountStubs(),
99115
selectedView: 'input',
116+
readOnly: false,
100117
};
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);
118+
const { rerender } = renderEditor(props);
119+
120+
expect(getTextAreasHidden().length).toBe(1);
121+
122+
const newProps = {
123+
...props,
124+
readOnly: true,
125+
};
126+
rerender(<Editor {...newProps} />);
127+
128+
expect(getTextAreasHidden().length).toBe(0);
106129
});
107130
});

0 commit comments

Comments
 (0)