Skip to content

Commit 096fc59

Browse files
committed
Migrate enzyme packages/console-shared unit tests to React Testing Library.
1 parent aaaa07d commit 096fc59

File tree

28 files changed

+1162
-968
lines changed

28 files changed

+1162
-968
lines changed
Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
import * as React from 'react';
2-
import { shallow } from 'enzyme';
3-
import { PageHeading } from '@console/shared/src/components/heading/PageHeading';
2+
import { screen, waitFor } from '@testing-library/react';
43
import * as UseQueryParams from '@console/shared/src/hooks/useQueryParams';
4+
import { renderWithProviders } from '../../../test-utils/unit-test-utils';
55
import CatalogController from '../CatalogController';
66

77
jest.mock('react-router-dom-v5-compat', () => ({
88
...jest.requireActual('react-router-dom-v5-compat'),
9-
useLocation: () => {
10-
return 'path';
11-
},
9+
useLocation: () => ({
10+
pathname: '/test-path',
11+
search: '',
12+
hash: '',
13+
state: null,
14+
}),
1215
}));
1316

14-
describe('Catalog Controller', () => {
15-
const spyUseMemo = jest.spyOn(React, 'useMemo');
17+
describe('CatalogController', () => {
1618
const spyUseQueryParams = jest.spyOn(UseQueryParams, 'useQueryParams');
17-
it('should return proper catalog title and description', () => {
19+
20+
beforeEach(() => {
21+
spyUseQueryParams.mockImplementation(() => new URLSearchParams());
22+
});
23+
24+
afterEach(() => {
25+
spyUseQueryParams.mockRestore();
26+
});
27+
28+
it('should render the title and description from the catalog extension', async () => {
1829
const catalogControllerProps: React.ComponentProps<typeof CatalogController> = {
1930
type: 'HelmChart',
2031
title: null,
@@ -33,80 +44,38 @@ describe('Catalog Controller', () => {
3344
},
3445
],
3546
items: [],
36-
itemsMap: null,
47+
itemsMap: { HelmChart: [] },
3748
loaded: true,
3849
loadError: null,
3950
searchCatalog: jest.fn(),
4051
};
41-
spyUseQueryParams.mockImplementation(() => ({
42-
catagory: null,
43-
keyword: null,
44-
sortOrder: null,
45-
}));
46-
spyUseMemo.mockReturnValue({
47-
pluginID: '@console/helm-plugin',
48-
pluginName: '@console/helm-plugin',
49-
properties: {
50-
catalogDescription: 'Helm Catalog description',
51-
title: 'Helm Charts',
52-
type: 'HelmChart',
53-
},
54-
type: 'console.catalog/item-type',
55-
uid: '@console/helm-plugin[9]',
56-
});
5752

58-
const catalogController = shallow(<CatalogController {...catalogControllerProps} />);
53+
renderWithProviders(<CatalogController {...catalogControllerProps} />);
5954

60-
expect(catalogController.find(PageHeading).props().title).toEqual('Helm Charts');
61-
expect(catalogController.find(PageHeading).props().helpText).toEqual(
62-
'Helm Catalog description',
63-
);
55+
await waitFor(() => {
56+
expect(screen.getByRole('heading', { name: 'Helm Charts' })).toBeInTheDocument();
57+
});
58+
expect(screen.getByText('Helm Catalog description')).toBeInTheDocument();
6459
});
6560

66-
it('should return proper catalog title and description when the extension does not have title and description', () => {
61+
it('should fall back to the default title and description if the extension is missing them', async () => {
6762
const catalogControllerProps: React.ComponentProps<typeof CatalogController> = {
6863
type: 'HelmChart',
6964
title: 'Default title',
7065
description: 'Default description',
71-
catalogExtensions: [
72-
{
73-
pluginID: '@console/helm-plugin',
74-
pluginName: '@console/helm-plugin',
75-
properties: {
76-
catalogDescription: null,
77-
title: null,
78-
type: 'HelmChart',
79-
},
80-
type: 'console.catalog/item-type',
81-
uid: '@console/helm-plugin[9]',
82-
},
83-
],
66+
catalogExtensions: [],
8467
items: [],
85-
itemsMap: null,
68+
itemsMap: { HelmChart: [] },
8669
loaded: true,
8770
loadError: null,
8871
searchCatalog: jest.fn(),
8972
};
90-
spyUseQueryParams.mockImplementation(() => ({
91-
catagory: null,
92-
keyword: null,
93-
sortOrder: null,
94-
}));
95-
spyUseMemo.mockReturnValue({
96-
pluginID: '@console/helm-plugin',
97-
pluginName: '@console/helm-plugin',
98-
properties: {
99-
catalogDescription: null,
100-
title: null,
101-
type: 'HelmChart',
102-
},
103-
type: 'console.catalog/item-type',
104-
uid: '@console/helm-plugin[9]',
105-
});
10673

107-
const catalogController = shallow(<CatalogController {...catalogControllerProps} />);
74+
renderWithProviders(<CatalogController {...catalogControllerProps} />);
10875

109-
expect(catalogController.find(PageHeading).props().title).toEqual('Default title');
110-
expect(catalogController.find(PageHeading).props().helpText).toEqual('Default description');
76+
await waitFor(() => {
77+
expect(screen.getByRole('heading', { name: 'Default title' })).toBeInTheDocument();
78+
});
79+
expect(screen.getByText('Default description')).toBeInTheDocument();
11180
});
11281
});
Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,98 @@
1-
import { shallow } from 'enzyme';
1+
import { screen, waitFor } from '@testing-library/react';
2+
import * as UseQueryParams from '@console/shared/src/hooks/useQueryParams';
3+
import { renderWithProviders } from '../../../test-utils/unit-test-utils';
4+
import CatalogController from '../CatalogController';
25
import CatalogDetailsPanel from '../details/CatalogDetailsPanel';
36
import { eventSourceCatalogItems } from './catalog-item-data';
47

5-
describe('Catalog details panel', () => {
6-
it('should show Support as Community', () => {
7-
const wrapper = shallow(<CatalogDetailsPanel item={eventSourceCatalogItems[1]} />);
8-
expect(wrapper.find('PropertyItem[label="Support"]').props().value).toBe('Community');
8+
jest.mock('react-router-dom-v5-compat', () => ({
9+
...jest.requireActual('react-router-dom-v5-compat'),
10+
useLocation: () => ({
11+
pathname: '/test-path',
12+
search: '',
13+
hash: '',
14+
state: null,
15+
}),
16+
}));
17+
18+
describe('CatalogController', () => {
19+
const spyUseQueryParams = jest.spyOn(UseQueryParams, 'useQueryParams');
20+
21+
beforeEach(() => {
22+
spyUseQueryParams.mockImplementation(() => new URLSearchParams());
923
});
1024

11-
it('should show one Support property in properties side panel', () => {
12-
const wrapper = shallow(<CatalogDetailsPanel item={eventSourceCatalogItems[1]} />);
13-
expect(wrapper.find('PropertyItem[label="Support"]').length).toBe(1);
25+
afterEach(() => {
26+
spyUseQueryParams.mockRestore();
27+
});
28+
29+
it('should render the title and description from the catalog extension', async () => {
30+
const catalogControllerProps: React.ComponentProps<typeof CatalogController> = {
31+
type: 'HelmChart',
32+
title: null,
33+
description: null,
34+
catalogExtensions: [
35+
{
36+
pluginID: '@console/helm-plugin',
37+
pluginName: '@console/helm-plugin',
38+
properties: {
39+
catalogDescription: 'Helm Catalog description',
40+
title: 'Helm Charts',
41+
type: 'HelmChart',
42+
},
43+
type: 'console.catalog/item-type',
44+
uid: '@console/helm-plugin[9]',
45+
},
46+
],
47+
items: [],
48+
itemsMap: { HelmChart: [] },
49+
loaded: true,
50+
loadError: null,
51+
searchCatalog: jest.fn(),
52+
};
53+
54+
renderWithProviders(<CatalogController {...catalogControllerProps} />);
55+
56+
await waitFor(() => {
57+
expect(screen.getByRole('heading', { name: 'Helm Charts' })).toBeInTheDocument();
58+
});
59+
expect(screen.getByText('Helm Catalog description')).toBeInTheDocument();
60+
});
61+
62+
it('should fall back to the default title and description if the extension is missing them', async () => {
63+
const catalogControllerProps: React.ComponentProps<typeof CatalogController> = {
64+
type: 'HelmChart',
65+
title: 'Default title',
66+
description: 'Default description',
67+
catalogExtensions: [],
68+
items: [],
69+
itemsMap: { HelmChart: [] },
70+
loaded: true,
71+
loadError: null,
72+
searchCatalog: jest.fn(),
73+
};
74+
75+
renderWithProviders(<CatalogController {...catalogControllerProps} />);
76+
77+
await waitFor(() => {
78+
expect(screen.getByRole('heading', { name: 'Default title' })).toBeInTheDocument();
79+
});
80+
expect(screen.getByText('Default description')).toBeInTheDocument();
81+
});
82+
});
83+
84+
describe('CatalogDetailsPanel', () => {
85+
it('should show the correct support level', () => {
86+
renderWithProviders(<CatalogDetailsPanel item={eventSourceCatalogItems[1]} />);
87+
88+
expect(screen.getByText('Support')).toBeInTheDocument();
89+
expect(screen.getByText('Community')).toBeInTheDocument();
90+
});
91+
92+
it('should show only one "Support" property in the side panel', () => {
93+
renderWithProviders(<CatalogDetailsPanel item={eventSourceCatalogItems[1]} />);
94+
95+
const supportLabels = screen.getAllByText('Support');
96+
expect(supportLabels).toHaveLength(1);
1497
});
1598
});

0 commit comments

Comments
 (0)