@@ -5,8 +5,6 @@ import { LearningResource } from "../../generated/v1"
5
5
import * as factories from "../../test-utils/factories"
6
6
import { setupReactQueryTest } from "../test-utils"
7
7
import { setMockResponse , urls , makeRequest } from "../../test-utils"
8
- import { useFeaturedLearningResourcesList } from "../learningResources"
9
- import { invalidateResourceQueries } from "../learningResources/invalidation"
10
8
import keyFactory from "../learningResources/keyFactory"
11
9
import {
12
10
useLearningPathsDetail ,
@@ -15,24 +13,11 @@ import {
15
13
useLearningPathCreate ,
16
14
useLearningPathDestroy ,
17
15
useLearningPathUpdate ,
18
- useLearningPathRelationshipMove ,
19
- useLearningPathRelationshipCreate ,
20
- useLearningPathRelationshipDestroy ,
21
16
} from "./index"
22
17
import learningPathKeyFactory from "./keyFactory"
23
18
24
19
const factory = factories . learningResources
25
20
26
- jest . mock ( "../learningResources/invalidation" , ( ) => {
27
- const actual = jest . requireActual ( "../learningResources/invalidation" )
28
- return {
29
- __esModule : true ,
30
- ...actual ,
31
- invalidateResourceQueries : jest . fn ( ) ,
32
- invalidateUserListQueries : jest . fn ( ) ,
33
- }
34
- } )
35
-
36
21
/**
37
22
* Assert that `hook` queries the API with the correct `url`, `method`, and
38
23
* exposes the API's data.
@@ -172,6 +157,7 @@ describe("LearningPath CRUD", () => {
172
157
173
158
await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
174
159
expect ( makeRequest ) . toHaveBeenCalledWith ( "post" , url , requestData )
160
+
175
161
expect ( queryClient . invalidateQueries ) . toHaveBeenCalledWith ( [
176
162
"learningPaths" ,
177
163
"list" ,
@@ -184,13 +170,23 @@ describe("LearningPath CRUD", () => {
184
170
setMockResponse . delete ( url , null )
185
171
186
172
const { wrapper, queryClient } = setupReactQueryTest ( )
173
+ jest . spyOn ( queryClient , "invalidateQueries" )
174
+
187
175
const { result } = renderHook ( useLearningPathDestroy , {
188
176
wrapper,
189
177
} )
190
178
result . current . mutate ( { id : path . id } )
191
179
await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
192
180
expect ( makeRequest ) . toHaveBeenCalledWith ( "delete" , url , undefined )
193
- expect ( invalidateResourceQueries ) . toHaveBeenCalledWith ( queryClient , path . id )
181
+
182
+ expect ( queryClient . invalidateQueries ) . toHaveBeenCalledWith ( [
183
+ "learningPaths" ,
184
+ "list" ,
185
+ ] )
186
+ expect ( queryClient . invalidateQueries ) . toHaveBeenCalledWith ( [
187
+ "learningPaths" ,
188
+ "membershipList" ,
189
+ ] )
194
190
} )
195
191
196
192
test ( "useLearningPathUpdate calls correct API" , async ( ) => {
@@ -200,120 +196,22 @@ describe("LearningPath CRUD", () => {
200
196
setMockResponse . patch ( url , path )
201
197
202
198
const { wrapper, queryClient } = setupReactQueryTest ( )
199
+ jest . spyOn ( queryClient , "invalidateQueries" )
200
+
203
201
const { result } = renderHook ( useLearningPathUpdate , { wrapper } )
204
202
result . current . mutate ( patch )
205
203
206
204
await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
207
205
expect ( makeRequest ) . toHaveBeenCalledWith ( "patch" , url , patch )
208
206
209
- expect ( invalidateResourceQueries ) . toHaveBeenCalledWith ( queryClient , path . id )
210
- } )
211
-
212
- test ( "useLearningPathRelationshipMove calls correct API" , async ( ) => {
213
- const { relationship, pathUrls, keys } = makeData ( )
214
- const url = pathUrls . relationshipDetails
215
- setMockResponse . patch ( url , null )
216
-
217
- const { wrapper, queryClient } = setupReactQueryTest ( )
218
- jest . spyOn ( queryClient , "invalidateQueries" )
219
- const { result } = renderHook ( useLearningPathRelationshipMove , { wrapper } )
220
- result . current . mutate ( relationship )
221
-
222
- await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
223
- expect ( makeRequest ) . toHaveBeenCalledWith (
224
- "patch" ,
225
- url ,
226
- expect . objectContaining ( { position : relationship . position } ) ,
227
- )
228
-
229
- expect ( queryClient . invalidateQueries ) . toHaveBeenCalledWith (
230
- keys . relationshipListing ,
231
- )
207
+ expect ( queryClient . invalidateQueries ) . toHaveBeenCalledWith ( [
208
+ "learningPaths" ,
209
+ "list" ,
210
+ ] )
211
+ expect ( queryClient . invalidateQueries ) . toHaveBeenCalledWith ( [
212
+ "learningPaths" ,
213
+ "detail" ,
214
+ path . id ,
215
+ ] )
232
216
} )
233
-
234
- test . each ( [ { isChildFeatured : false } , { isChildFeatured : true } ] ) (
235
- "useLearningPathRelationshipCreate calls correct API and patches featured resources" ,
236
- async ( { isChildFeatured } ) => {
237
- const { relationship, pathUrls, resourceWithoutList } = makeData ( )
238
-
239
- const featured = factory . resources ( { count : 3 } )
240
- if ( isChildFeatured ) {
241
- featured . results [ 0 ] = resourceWithoutList
242
- }
243
- setMockResponse . get ( urls . learningResources . featured ( ) , featured )
244
-
245
- const url = pathUrls . relationshipList
246
- const requestData = {
247
- child : relationship . child ,
248
- parent : relationship . parent ,
249
- position : relationship . position ,
250
- }
251
- setMockResponse . post ( url , relationship )
252
-
253
- const { wrapper, queryClient } = setupReactQueryTest ( )
254
- const { result } = renderHook ( useLearningPathRelationshipCreate , {
255
- wrapper,
256
- } )
257
- const { result : featuredResult } = renderHook (
258
- useFeaturedLearningResourcesList ,
259
- { wrapper } ,
260
- )
261
- await waitFor ( ( ) => expect ( featuredResult . current . data ) . toBe ( featured ) )
262
-
263
- result . current . mutate ( requestData )
264
-
265
- await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
266
- expect ( makeRequest ) . toHaveBeenCalledWith ( "post" , url , requestData )
267
-
268
- expect ( invalidateResourceQueries ) . toHaveBeenCalledWith (
269
- queryClient ,
270
- relationship . child ,
271
- { skipFeatured : false } ,
272
- )
273
- expect ( invalidateResourceQueries ) . toHaveBeenCalledWith (
274
- queryClient ,
275
- relationship . parent ,
276
- )
277
- } ,
278
- )
279
-
280
- test . each ( [ { isChildFeatured : false } , { isChildFeatured : true } ] ) (
281
- "useLearningPathRelationshipDestroy calls correct API and patches child resource cache (isChildFeatured=$isChildFeatured)" ,
282
- async ( { isChildFeatured } ) => {
283
- const { relationship, pathUrls } = makeData ( )
284
- const url = pathUrls . relationshipDetails
285
-
286
- const featured = factory . resources ( { count : 3 } )
287
- if ( isChildFeatured ) {
288
- featured . results [ 0 ] = relationship . resource
289
- }
290
- setMockResponse . get ( urls . learningResources . featured ( ) , featured )
291
-
292
- setMockResponse . delete ( url , null )
293
- const { wrapper, queryClient } = setupReactQueryTest ( )
294
-
295
- const { result } = renderHook ( useLearningPathRelationshipDestroy , {
296
- wrapper,
297
- } )
298
- const { result : featuredResult } = renderHook (
299
- useFeaturedLearningResourcesList ,
300
- { wrapper } ,
301
- )
302
-
303
- await waitFor ( ( ) => expect ( featuredResult . current . data ) . toBe ( featured ) )
304
- result . current . mutate ( relationship )
305
- await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
306
-
307
- expect ( makeRequest ) . toHaveBeenCalledWith ( "delete" , url , undefined )
308
- expect ( invalidateResourceQueries ) . toHaveBeenCalledWith (
309
- queryClient ,
310
- relationship . child ,
311
- { skipFeatured : false } ,
312
- )
313
- expect ( invalidateResourceQueries ) . toHaveBeenCalledWith (
314
- queryClient ,
315
- relationship . parent ,
316
- )
317
- } ,
318
- )
319
217
} )
0 commit comments