Skip to content

Commit 70faa07

Browse files
committed
Fix react tests
1 parent a0e8a61 commit 70faa07

File tree

11 files changed

+116
-167
lines changed

11 files changed

+116
-167
lines changed
Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { fireEvent, render, waitFor } from '@testing-library/react';
1+
import { render } from '@testing-library/react';
22
import AdminTaskFilters from './AdminTaskFilters';
3+
import userEvent from '@testing-library/user-event';
34

45
jest.mock('middleware/AppContext', () => ({
56
useAppContext: () => ({}),
@@ -21,12 +22,12 @@ it('Render loading state (disabled)', () => {
2122
/>,
2223
);
2324

24-
const filterInput = getByRole('textbox');
25+
const filterInput = getByRole('searchbox', { name: '' });
2526
expect(filterInput).toHaveAttribute('disabled');
2627
});
2728

28-
it('Select a filter of each type and ensure chips are present', () => {
29-
const { queryByText, getByRole, getByLabelText } = render(
29+
it('Select a filter of each type and ensure chips are present admin', async () => {
30+
const { queryByText, getByRole, getByLabelText, queryAllByText } = render(
3031
<AdminTaskFilters
3132
setFilterData={() => null}
3233
filterData={{
@@ -39,62 +40,59 @@ it('Select a filter of each type and ensure chips are present', () => {
3940
);
4041

4142
// Enter an account ID item
42-
const accountIdInput = getByRole('textbox');
43-
expect(accountIdInput).not.toHaveAttribute('disabled');
44-
fireEvent.change(accountIdInput, { target: { value: '11593016' } });
43+
const filterInput = getByRole('searchbox', { name: '' });
44+
expect(filterInput).not.toHaveAttribute('disabled');
4545

46-
const optionMenu = getByRole('button', { name: 'filterSelectionDropdown' });
46+
await userEvent.type(filterInput, '11593016');
4747

48-
waitFor(() => {
49-
fireEvent.click(optionMenu);
50-
});
48+
const optionMenu = getByRole('button', { name: 'filterSelectionDropdown' });
49+
await userEvent.click(optionMenu);
5150

5251
const orgIdOption = queryByText('Org ID') as Element;
5352
expect(orgIdOption).toBeInTheDocument();
54-
fireEvent.click(orgIdOption);
53+
await userEvent.click(orgIdOption);
5554

5655
// Enter an org ID item
57-
const orgIdInput = getByRole('textbox');
58-
expect(orgIdInput).not.toHaveAttribute('disabled');
59-
fireEvent.change(orgIdInput, { target: { value: '13446804' } });
6056

61-
fireEvent.click(optionMenu);
57+
await userEvent.type(filterInput, '13446804');
58+
expect(filterInput).toHaveValue('13446804');
6259

6360
// Select a Status item
61+
await userEvent.click(optionMenu);
6462
const statusOption = queryByText('Status') as Element;
6563
expect(statusOption).toBeInTheDocument();
66-
fireEvent.click(statusOption);
64+
await userEvent.click(statusOption);
6765

6866
const statusSelector = getByLabelText('filter status') as Element;
6967
expect(statusSelector).toBeInTheDocument();
70-
fireEvent.click(statusSelector);
68+
await userEvent.click(statusSelector);
7169

7270
const statusItem = queryByText('Running') as Element;
7371
expect(statusItem).toBeInTheDocument();
74-
fireEvent.click(statusItem);
72+
await userEvent.click(statusItem);
7573

7674
// Click the optionsButton to make the statusMenu disappear
77-
fireEvent.click(optionMenu);
78-
75+
await userEvent.click(optionMenu);
7976
// Select a Type item
8077
const typeOption = queryByText('Type') as Element;
8178
expect(typeOption).toBeInTheDocument();
82-
fireEvent.click(typeOption);
79+
await userEvent.click(typeOption);
8380

8481
const typeSelector = getByLabelText('filter type') as Element;
8582
expect(typeSelector).toBeInTheDocument();
86-
fireEvent.click(typeSelector);
83+
await userEvent.click(typeSelector);
8784

8885
const typeItem = queryByText('introspect') as Element;
8986
expect(typeItem).toBeInTheDocument();
90-
fireEvent.click(typeItem);
87+
await userEvent.click(typeItem);
9188

9289
// Click the optionsButton to make the typeMenu disappear
93-
fireEvent.click(optionMenu);
90+
await userEvent.click(typeSelector);
9491

9592
// Check all the chips are there
9693
expect(queryByText('11593016')).toBeInTheDocument();
9794
expect(queryByText('13446804')).toBeInTheDocument();
95+
9896
expect(queryByText('Running')).toBeInTheDocument();
99-
expect(queryByText('introspect')).toBeInTheDocument();
97+
expect(queryAllByText('introspect')).toHaveLength(2);
10098
});

src/Pages/Repositories/AdminTaskTable/components/AdminTaskFilters.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
FlexItem,
88
InputGroup,
99
TextInput,
10-
InputGroupItem,
1110
type SelectOptionProps,
1211
} from '@patternfly/react-core';
1312

src/Pages/Repositories/ContentListTable/components/ContentListFilters.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ it('Render loading state (disabled)', () => {
4343
/>,
4444
);
4545

46-
const filterInput = getByRole('textbox');
46+
const filterInput = getByRole('searchbox');
4747
expect(filterInput).toHaveAttribute('disabled');
4848
});
4949

50-
it('Select a filter of each type and ensure chips are present', () => {
50+
it('Select a filter of each type and ensure chips are present contentlistfilters', () => {
5151
const { queryByText, getByRole, getByLabelText } = render(
5252
<ContentListFilters
5353
contentOrigin={ContentOrigin.CUSTOM}
@@ -64,7 +64,7 @@ it('Select a filter of each type and ensure chips are present', () => {
6464
/>,
6565
);
6666

67-
const filterInput = getByRole('textbox');
67+
const filterInput = getByRole('searchbox');
6868
expect(filterInput).not.toHaveAttribute('disabled');
6969
fireEvent.change(filterInput, { target: { value: 'EPEL' } });
7070

src/Pages/Repositories/ContentListTable/components/SnapshotDetailsModal/Tabs/SnapshotErrataFilters.test.tsx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { fireEvent, render, waitFor } from '@testing-library/react';
1+
import { render } from '@testing-library/react';
22
import SnapshotErrataFilters from './SnapshotErrataFilters';
33
import { ContentOrigin } from 'services/Content/ContentApi';
4+
import userEvent from '@testing-library/user-event';
45

56
jest.mock('middleware/AppContext', () => ({
67
useAppContext: () => ({
@@ -25,12 +26,12 @@ it('Render loading state (disabled)', () => {
2526
/>,
2627
);
2728

28-
const filterInput = getByRole('textbox');
29+
const filterInput = getByRole('searchbox');
2930
expect(filterInput).toHaveAttribute('disabled');
3031
});
3132

32-
it('Select a filter of each type and ensure chips are present', () => {
33-
const { queryByText, getByRole } = render(
33+
it('Select a filter of each type and ensure chips are present snapshotErrataFilters', async () => {
34+
const { queryByText, getByRole, queryAllByText } = render(
3435
<SnapshotErrataFilters
3536
isLoading={false}
3637
setFilterData={() => null}
@@ -43,40 +44,45 @@ it('Select a filter of each type and ensure chips are present', () => {
4344
);
4445

4546
// Filter on Name / Synopsis
46-
const nameOrSynopsisFilter = getByRole('textbox');
47+
const nameOrSynopsisFilter = getByRole('searchbox');
4748
expect(nameOrSynopsisFilter).not.toHaveAttribute('disabled');
48-
fireEvent.change(nameOrSynopsisFilter, { target: { value: 'EPEL' } });
49+
await userEvent.type(nameOrSynopsisFilter, 'EPEL');
4950

5051
// Select a Type item
51-
const optionMenu = document.getElementsByClassName('pf-v5-c-menu-toggle')[0]; // TODO: pf5 > pf6
52-
waitFor(() => fireEvent.click(optionMenu));
52+
const optionMenu = getByRole('button', { name: 'Name/Synopsis' });
53+
await userEvent.click(optionMenu);
54+
5355
const typeOption = queryByText('Type') as HTMLElement;
5456
expect(typeOption).toBeInTheDocument();
55-
fireEvent.click(typeOption);
57+
await userEvent.click(typeOption);
58+
59+
const typeSelector = queryByText('Filter by type') as HTMLElement;
60+
expect(typeSelector).toBeInTheDocument();
61+
62+
await userEvent.click(typeSelector);
5663

57-
expect(queryByText('Filter by type')).toBeInTheDocument();
58-
const typeSelector = document.getElementsByClassName('pf-v5-c-menu-toggle')[1];
59-
fireEvent.click(typeSelector);
6064
const typeItem = queryByText('Security') as Element;
6165
expect(typeItem).toBeInTheDocument();
62-
fireEvent.click(typeItem);
66+
await userEvent.click(typeItem);
6367

6468
// Select a Severity item
65-
fireEvent.click(optionMenu);
69+
await userEvent.click(optionMenu);
6670
const severityOption = queryByText('Severity') as Element;
6771
expect(severityOption).toBeInTheDocument();
68-
fireEvent.click(severityOption);
72+
await userEvent.click(severityOption);
6973

70-
expect(queryByText('Filter by severity')).toBeInTheDocument();
71-
const severitySelector = document.getElementsByClassName('pf-v5-c-menu-toggle')[1];
72-
fireEvent.click(severitySelector);
74+
const severitySelector = queryByText('Filter by severity') as Element;
75+
expect(severitySelector).toBeInTheDocument();
76+
77+
await userEvent.click(severitySelector);
7378
const severityItem = queryByText('Critical') as Element;
79+
7480
expect(severityItem).toBeInTheDocument();
75-
fireEvent.click(severityItem);
76-
fireEvent.click(optionMenu);
81+
await userEvent.click(severityItem);
82+
await userEvent.click(severitySelector);
7783

7884
// Check all the chips are there
7985
expect(queryByText('EPEL')).toBeInTheDocument();
8086
expect(queryByText('Security')).toBeInTheDocument();
81-
expect(queryByText('Critical')).toBeInTheDocument();
87+
expect(queryAllByText('Critical')).toHaveLength(2);
8288
});

src/Pages/Repositories/ContentListTable/components/SnapshotListModal/SnapshotListModal.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render } from '@testing-library/react';
1+
import { getByText, render } from '@testing-library/react';
22
import SnapshotListModal from './SnapshotListModal';
33
import {
44
ReactQueryTestWrapper,
@@ -41,17 +41,16 @@ it('Render 1 item', () => {
4141
isLoading: false,
4242
isFetching: false,
4343
}));
44-
const { queryByText } = render(
44+
const { getByText } = render(
4545
<ReactQueryTestWrapper>
4646
<SnapshotListModal />
4747
</ReactQueryTestWrapper>,
4848
);
4949

50-
expect(queryByText(defaultContentItemWithSnapshot.name)).toBeInTheDocument();
51-
expect(queryByText('Latest Snapshot Config:')).toBeInTheDocument();
52-
50+
expect(getByText('View list of snapshots for AwesomeNamewwyylse12.')).toBeInTheDocument();
51+
expect(getByText('Latest Snapshot Config:')).toBeInTheDocument();
5352
expect(
54-
queryByText((defaultSnapshotItem.content_counts['rpm.package'] as number)?.toString()),
53+
getByText((defaultSnapshotItem.content_counts['rpm.package'] as number)?.toString()),
5554
).toBeInTheDocument();
5655
});
5756

@@ -76,16 +75,17 @@ it('Render 20 items', () => {
7675
isLoading: false,
7776
isFetching: false,
7877
}));
79-
const { queryByText } = render(
78+
79+
const { getByText } = render(
8080
<ReactQueryTestWrapper>
8181
<SnapshotListModal />
8282
</ReactQueryTestWrapper>,
8383
);
8484

85-
expect(queryByText(defaultContentItemWithSnapshot.name)).toBeInTheDocument();
86-
expect(queryByText('Latest Snapshot Config:')).toBeInTheDocument();
85+
expect(getByText('View list of snapshots for AwesomeNamewwyylse12.')).toBeInTheDocument();
86+
expect(getByText('Latest Snapshot Config:')).toBeInTheDocument();
8787

8888
expect(
89-
queryByText((defaultSnapshotItem.content_counts['rpm.package'] as number)?.toString()),
89+
getByText((defaultSnapshotItem.content_counts['rpm.package'] as number)?.toString()),
9090
).toBeInTheDocument();
9191
});
Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { fireEvent, render } from '@testing-library/react';
22
import { AddRepo } from './AddRepo';
3-
import { useAppContext } from 'middleware/AppContext';
43
import { useChrome } from '@redhat-cloud-services/frontend-components/useChrome';
54

65
jest.mock('middleware/AppContext', () => ({
@@ -17,15 +16,6 @@ jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({
1716
}));
1817

1918
it('Render enabled with snapshots enabled', () => {
20-
(useAppContext as jest.Mock).mockImplementation(() => ({
21-
features: {
22-
snapshots: {
23-
enabled: true,
24-
accessible: true,
25-
},
26-
},
27-
}));
28-
2919
const addRepo = jest.fn();
3020

3121
const { queryByText } = render(<AddRepo isDisabled={false} addRepo={addRepo} />);
@@ -52,57 +42,9 @@ it('Render enabled with snapshots enabled', () => {
5242
});
5343

5444
it('Render disabled with snapshots enabled', () => {
55-
(useAppContext as jest.Mock).mockImplementation(() => ({
56-
features: {
57-
snapshots: {
58-
enabled: true,
59-
accessible: true,
60-
},
61-
},
62-
}));
63-
6445
const addRepo = jest.fn();
6546

6647
render(<AddRepo isDisabled={true} addRepo={addRepo} />);
6748

6849
expect(document.getElementById('toggle-add') as Element).toHaveAttribute('disabled');
6950
});
70-
71-
it('Render enabled with snapshots disabled', () => {
72-
(useAppContext as jest.Mock).mockImplementation(() => ({
73-
features: {
74-
snapshots: {
75-
enabled: false,
76-
accessible: false,
77-
},
78-
},
79-
}));
80-
81-
const addRepo = jest.fn();
82-
83-
const { queryByText } = render(<AddRepo isDisabled={false} addRepo={addRepo} />);
84-
85-
const addWithSnapshot = queryByText('Add') as Element;
86-
expect(addWithSnapshot).toBeInTheDocument();
87-
fireEvent.click(addWithSnapshot);
88-
89-
expect(addRepo.mock.calls).toHaveLength(1);
90-
expect(addRepo.mock.calls[0][0]).toBe(false);
91-
});
92-
93-
it('Render disabled with snapshots disabld', () => {
94-
(useAppContext as jest.Mock).mockImplementation(() => ({
95-
features: {
96-
snapshots: {
97-
enabled: false,
98-
accessible: false,
99-
},
100-
},
101-
}));
102-
103-
const addRepo = jest.fn();
104-
105-
const { queryByText } = render(<AddRepo isDisabled={true} addRepo={addRepo} />);
106-
107-
expect(queryByText('Add')).toHaveAttribute('disabled');
108-
});

src/Pages/Templates/TemplateDetails/components/Tabs/TemplateSystemsTab.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ it('expect TemplateSystemsTab to render 15 items, read-only', async () => {
7979
expect(queryByRole('checkbox', { name: 'Select row 0' })).not.toBeInTheDocument();
8080

8181
// Ensure top kebab is disabled.
82-
expect(getByRole('button', { name: 'Actions' })).toHaveAttribute('disabled');
82+
expect(getByRole('button', { name: 'plain kebab' })).toHaveAttribute('disabled');
8383

8484
// Ensure the row kebab is disabled
8585
expect(getAllByRole('button', { name: 'Kebab toggle' })[0]).toHaveAttribute('disabled');

0 commit comments

Comments
 (0)