Skip to content

Commit 63c2862

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 bc8a381 commit 63c2862

26 files changed

+575
-1860
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: 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') === 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).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).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).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)