1
1
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' ;
4
3
import * as UseQueryParams from '@console/shared/src/hooks/useQueryParams' ;
4
+ import { renderWithProviders } from '../../../test-utils/unit-test-utils' ;
5
5
import CatalogController from '../CatalogController' ;
6
6
7
7
jest . mock ( 'react-router-dom-v5-compat' , ( ) => ( {
8
8
...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
+ } ) ,
12
15
} ) ) ;
13
16
14
- describe ( 'Catalog Controller' , ( ) => {
15
- const spyUseMemo = jest . spyOn ( React , 'useMemo' ) ;
17
+ describe ( 'CatalogController' , ( ) => {
16
18
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 ( ) => {
18
29
const catalogControllerProps : React . ComponentProps < typeof CatalogController > = {
19
30
type : 'HelmChart' ,
20
31
title : null ,
@@ -33,80 +44,38 @@ describe('Catalog Controller', () => {
33
44
} ,
34
45
] ,
35
46
items : [ ] ,
36
- itemsMap : null ,
47
+ itemsMap : { HelmChart : [ ] } ,
37
48
loaded : true ,
38
49
loadError : null ,
39
50
searchCatalog : jest . fn ( ) ,
40
51
} ;
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
- } ) ;
57
52
58
- const catalogController = shallow ( < CatalogController { ...catalogControllerProps } /> ) ;
53
+ renderWithProviders ( < CatalogController { ...catalogControllerProps } /> ) ;
59
54
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 ( ) ;
64
59
} ) ;
65
60
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 ( ) => {
67
62
const catalogControllerProps : React . ComponentProps < typeof CatalogController > = {
68
63
type : 'HelmChart' ,
69
64
title : 'Default title' ,
70
65
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 : [ ] ,
84
67
items : [ ] ,
85
- itemsMap : null ,
68
+ itemsMap : { HelmChart : [ ] } ,
86
69
loaded : true ,
87
70
loadError : null ,
88
71
searchCatalog : jest . fn ( ) ,
89
72
} ;
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
- } ) ;
106
73
107
- const catalogController = shallow ( < CatalogController { ...catalogControllerProps } /> ) ;
74
+ renderWithProviders ( < CatalogController { ...catalogControllerProps } /> ) ;
108
75
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 ( ) ;
111
80
} ) ;
112
81
} ) ;
0 commit comments