1
- /* eslint-disable react/ prop-types */
1
+ import PropTypes from ' prop-types' ;
2
2
import { renderWithRouter } from '@edx/frontend-enterprise-utils' ;
3
- import {
4
- fireEvent , screen , waitFor , act ,
5
- } from '@testing-library/react' ;
3
+ import { fireEvent , screen , waitFor } from '@testing-library/react' ;
6
4
import ProvisioningFormCustomCatalogDropdown from '../ProvisioningFormCustomCatalogDropdown' ;
7
5
import { initialStateValue , ProvisioningContext } from '../../../../testData/Provisioning' ;
8
6
import PROVISIONING_PAGE_TEXT from '../../../data/constants' ;
@@ -22,126 +20,167 @@ jest.mock('../../../../../data/services/EnterpriseApiService', () => ({
22
20
fetchEnterpriseCustomerCatalogs : jest . fn ( ( ) => Promise . resolve ( mockData ) ) ,
23
21
} ) ) ;
24
22
25
- // Patch frontend-platform to serve a custom version of PREDEFINED_CATALOG_QUERIES.
26
23
jest . mock ( '@edx/frontend-platform' , ( ) => ( {
27
24
...jest . requireActual ( '@edx/frontend-platform' ) ,
28
25
getConfig : jest . fn ( ( ) => ( {
29
26
PREDEFINED_CATALOG_QUERIES : MOCK_PREDEFINED_CATALOG_QUERIES ,
30
27
} ) ) ,
31
28
} ) ) ;
32
29
33
- const ProvisioningFormCustomCatalogDropdownWrapper = ( {
34
- value = {
30
+ const ProvisioningFormCustomCatalogDropdownWrapper = ( { value } ) => (
31
+ < ProvisioningContext value = { value } >
32
+ < ProvisioningFormCustomCatalogDropdown />
33
+ </ ProvisioningContext >
34
+ ) ;
35
+
36
+ ProvisioningFormCustomCatalogDropdownWrapper . propTypes = {
37
+ value : PropTypes . shape ( {
38
+ existingEnterpriseCatalogs : PropTypes . shape ( {
39
+ data : PropTypes . arrayOf (
40
+ PropTypes . shape ( {
41
+ title : PropTypes . string ,
42
+ enterpriseCatalogQuery : PropTypes . string ,
43
+ uuid : PropTypes . string ,
44
+ } ) ,
45
+ ) ,
46
+ isLoading : PropTypes . bool ,
47
+ } ) ,
48
+ isEditMode : PropTypes . bool ,
49
+ formData : PropTypes . shape ( {
50
+ policies : PropTypes . arrayOf (
51
+ PropTypes . shape ( {
52
+ oldCustomCatalog : PropTypes . bool ,
53
+ oldCatalogTitle : PropTypes . string ,
54
+ customCatalog : PropTypes . bool ,
55
+ catalogTitle : PropTypes . string ,
56
+ catalogUuid : PropTypes . string ,
57
+ } ) ,
58
+ ) ,
59
+ } ) ,
60
+ } ) ,
61
+ } ;
62
+
63
+ ProvisioningFormCustomCatalogDropdownWrapper . defaultProps = {
64
+ value : {
35
65
...initialStateValue ,
36
66
existingEnterpriseCatalogs : {
37
67
data : sampleCatalogs ,
38
68
isLoading : false ,
39
69
} ,
40
70
} ,
41
- } ) => (
42
- < ProvisioningContext value = { value } >
43
- < ProvisioningFormCustomCatalogDropdown />
44
- </ ProvisioningContext >
45
- ) ;
71
+ } ;
46
72
47
73
describe ( 'ProvisioningFormCustomCatalogDropdown' , ( ) => {
48
74
beforeEach ( ( ) => {
49
75
jest . clearAllMocks ( ) ;
50
76
} ) ;
51
- it ( 'renders the custom catalog dropdown' , ( ) => {
52
- renderWithRouter (
53
- < ProvisioningFormCustomCatalogDropdownWrapper /> ,
54
- ) ;
55
77
78
+ it ( 'renders the custom catalog dropdown' , ( ) => {
79
+ renderWithRouter ( < ProvisioningFormCustomCatalogDropdownWrapper /> ) ;
56
80
expect ( screen . getByText ( CUSTOM_CATALOG . OPTIONS . enterpriseCatalog . title ) ) . toBeTruthy ( ) ;
57
81
expect ( screen . getByText ( CUSTOM_CATALOG . OPTIONS . enterpriseCatalog . subtitle ) ) . toBeTruthy ( ) ;
58
82
} ) ;
83
+
59
84
it ( 'renders the custom catalog dropdown options' , ( ) => {
60
- renderWithRouter (
61
- < ProvisioningFormCustomCatalogDropdownWrapper /> ,
62
- ) ;
85
+ renderWithRouter ( < ProvisioningFormCustomCatalogDropdownWrapper /> ) ;
63
86
64
87
const autoSuggestInput = screen . getByTestId ( 'custom-catalog-dropdown-autosuggest' ) ;
65
88
const autoSuggestButton = screen . getAllByRole ( 'button' ) [ 0 ] ;
66
- // open dropdown
67
89
fireEvent . click ( autoSuggestButton ) ;
68
- // Check values are populating
90
+
69
91
const { queryIdToQueryType } = getPredefinedCatalogQueryMappings ( ) ;
70
92
sampleCatalogs . forEach ( ( { title, enterpriseCatalogQuery } ) => {
71
93
if ( ! ( enterpriseCatalogQuery in queryIdToQueryType ) ) {
72
94
expect ( screen . getByText ( title , { exact : false } ) ) . toBeTruthy ( ) ;
73
95
}
74
96
} ) ;
97
+
75
98
const autoSuggestDropdownButtons = screen . getAllByRole ( 'button' ) ;
76
99
const filteredDropdowns = autoSuggestDropdownButtons . filter ( ( element ) => element . textContent . includes ( '73cb6181' ) ) ;
77
- // close dropdown
100
+
78
101
fireEvent . click ( autoSuggestButton ) ;
79
102
80
103
for ( let i = 0 ; i < filteredDropdowns . length ; i ++ ) {
81
104
fireEvent . click ( autoSuggestButton ) ;
82
105
fireEvent . click ( filteredDropdowns [ i ] ) ;
83
- waitFor ( ( ) => expect ( autoSuggestInput . getAttribute ( 'value' ) ) . toContain ( filteredDropdowns [ i ] . textContent ) ) ;
106
+ waitFor ( ( ) => {
107
+ expect ( autoSuggestInput ) . toHaveValue ( expect . stringContaining ( filteredDropdowns [ i ] . textContent ) ) ;
108
+ } ) ;
84
109
}
85
110
} ) ;
111
+
86
112
it ( 'renders correct dropdown when catalogs list is still loading' , ( ) => {
87
113
LmsApiService . fetchEnterpriseCustomerCatalogs . mockResolvedValueOnce ( { data : { results : [ ] } } ) ;
88
- renderWithRouter (
89
- < ProvisioningFormCustomCatalogDropdownWrapper
90
- value = { initialStateValue }
91
- /> ,
92
- ) ;
93
-
114
+ renderWithRouter ( < ProvisioningFormCustomCatalogDropdownWrapper value = { initialStateValue } /> ) ;
94
115
const autoSuggestButton = screen . getAllByRole ( 'button' ) [ 0 ] ;
95
116
fireEvent . click ( autoSuggestButton ) ;
96
117
expect ( screen . getByText ( 'Loading...' ) ) . toBeTruthy ( ) ;
97
118
} ) ;
119
+
98
120
it ( 'renders correct dropdown when the selected customer has no custom catalogs' , ( ) => {
99
121
LmsApiService . fetchEnterpriseCustomerCatalogs . mockResolvedValueOnce ( { data : { results : [ ] } } ) ;
100
122
renderWithRouter (
101
123
< ProvisioningFormCustomCatalogDropdownWrapper
102
124
value = { {
103
125
...initialStateValue ,
104
126
existingEnterpriseCatalogs : {
105
- data : [ ] , // This customer has no custom/unique/curated catalogs!
127
+ data : [ ] ,
106
128
isLoading : false ,
107
129
} ,
108
130
} }
109
131
/> ,
110
132
) ;
111
-
112
133
const autoSuggestButton = screen . getAllByRole ( 'button' ) [ 0 ] ;
113
134
fireEvent . click ( autoSuggestButton ) ;
114
135
expect ( screen . getByText ( 'No catalogs found for customer.' ) ) . toBeTruthy ( ) ;
115
136
} ) ;
137
+
116
138
it ( 'renders default catalog query title when isEditMode is true' , async ( ) => {
117
- LmsApiService . fetchEnterpriseCustomerCatalogs . mockResolvedValue ( { data : { results : sampleCatalogs } } ) ;
139
+ const customSampleCatalogs = [
140
+ ...sampleCatalogs ,
141
+ {
142
+ title : 'Snoopy gang' ,
143
+ enterpriseCatalogQuery : 'custom-query' ,
144
+ uuid : '4ev3r' ,
145
+ } ,
146
+ ] ;
147
+
148
+ LmsApiService . fetchEnterpriseCustomerCatalogs . mockResolvedValue ( {
149
+ data : { results : customSampleCatalogs } ,
150
+ } ) ;
151
+
118
152
renderWithRouter (
119
153
< ProvisioningFormCustomCatalogDropdownWrapper
120
154
value = { {
121
155
...initialStateValue ,
122
156
isEditMode : true ,
123
157
existingEnterpriseCatalogs : {
124
- data : sampleCatalogs ,
158
+ data : customSampleCatalogs ,
125
159
isLoading : false ,
126
160
} ,
127
161
formData : {
128
- policies : [ {
129
- oldCustomCatalog : true ,
130
- oldCatalogTitle : 'Snoopy gang' ,
131
- customCatalog : true ,
132
- catalogTitle : 'Snoopy gang' ,
133
- catalogUuid : '4ev3r' ,
134
- } ] ,
162
+ policies : [
163
+ {
164
+ oldCustomCatalog : true ,
165
+ oldCatalogTitle : 'Snoopy gang' ,
166
+ customCatalog : true ,
167
+ catalogTitle : 'Snoopy gang' ,
168
+ catalogUuid : '4ev3r' ,
169
+ } ,
170
+ ] ,
135
171
} ,
136
172
} }
137
173
/> ,
138
174
) ;
139
- act ( async ( ) => {
140
- expect ( await screen . findByRole ( 'list' , {
141
- name : 'Enterprise Catalog' ,
142
- } ) . value ) . toBe ( 'Snoopy gang --- 4ev3r' ) ;
143
- } ) ;
175
+
176
+ const input = await screen . findByTestId ( 'custom-catalog-dropdown-autosuggest' ) ;
177
+ const toggleButton = screen . getAllByRole ( 'button' ) [ 0 ] ;
178
+ fireEvent . click ( toggleButton ) ;
179
+ const option = await screen . findByText ( / S n o o p y g a n g - - - 4 e v 3 r / i) ;
180
+ fireEvent . click ( option ) ;
181
+ expect ( input ) . toHaveValue ( 'Snoopy gang --- 4ev3r' ) ;
144
182
} ) ;
183
+
145
184
it ( 'renders empty string title when isEditMode is false' , async ( ) => {
146
185
renderWithRouter (
147
186
< ProvisioningFormCustomCatalogDropdownWrapper
@@ -153,19 +192,18 @@ describe('ProvisioningFormCustomCatalogDropdown', () => {
153
192
isLoading : false ,
154
193
} ,
155
194
formData : {
156
- policies : [ {
157
- customCatalog : true ,
158
- catalogTitle : 'Snoopy gang' ,
159
- catalogUuid : '4ev3r' ,
160
- } ] ,
195
+ policies : [
196
+ {
197
+ customCatalog : true ,
198
+ catalogTitle : 'Snoopy gang' ,
199
+ catalogUuid : '4ev3r' ,
200
+ } ,
201
+ ] ,
161
202
} ,
162
203
} }
163
204
/> ,
164
205
) ;
165
- act ( async ( ) => {
166
- expect ( await screen . findByRole ( 'list' , {
167
- name : 'Enterprise Catalog' ,
168
- } ) . value ) . toBe ( '' ) ;
169
- } ) ;
206
+ const input = await screen . findByTestId ( 'custom-catalog-dropdown-autosuggest' ) ;
207
+ expect ( input ) . toHaveValue ( '' ) ;
170
208
} ) ;
171
209
} ) ;
0 commit comments