1
1
import { AxiosError } from 'axios'
2
2
import { cloneDeep , set } from 'lodash'
3
+ import { MOCK_RECOMMENDATIONS } from 'uiSrc/constants/mocks/mock-recommendations'
3
4
import { Vote } from 'uiSrc/constants/recommendations'
4
- import { apiService } from 'uiSrc/services'
5
+ import { apiService , resourcesService } from 'uiSrc/services'
5
6
import { addErrorNotification } from 'uiSrc/slices/app/notifications'
6
7
import reducer , {
7
8
initialState ,
@@ -19,7 +20,12 @@ import reducer, {
19
20
updateLiveRecommendation ,
20
21
updateRecommendation ,
21
22
setTotalUnread ,
22
- deleteLiveRecommendations , deleteRecommendations ,
23
+ deleteLiveRecommendations ,
24
+ deleteRecommendations ,
25
+ getContentRecommendations ,
26
+ getContentRecommendationsSuccess ,
27
+ getContentRecommendationsFailure ,
28
+ fetchContentRecommendations ,
23
29
} from 'uiSrc/slices/recommendations/recommendations'
24
30
import { cleanup , initialStateDefault , mockStore , mockedStore } from 'uiSrc/utils/test-utils'
25
31
@@ -261,6 +267,67 @@ describe('recommendations slice', () => {
261
267
} )
262
268
expect ( recommendationsSelector ( rootState ) ) . toEqual ( state )
263
269
} )
270
+
271
+ describe ( 'getContentRecommendations' , ( ) => {
272
+ it ( 'should properly set loading: true' , ( ) => {
273
+ // Arrange
274
+ const state = {
275
+ ...initialState ,
276
+ loading : true ,
277
+ error : '' ,
278
+ }
279
+
280
+ // Act
281
+ const nextState = reducer ( initialState , getContentRecommendations ( ) )
282
+
283
+ // Assert
284
+ const rootState = Object . assign ( initialStateDefault , {
285
+ recommendations : nextState ,
286
+ } )
287
+ expect ( recommendationsSelector ( rootState ) ) . toEqual ( state )
288
+ } )
289
+ } )
290
+
291
+ describe ( 'getContentRecommendationsFailure' , ( ) => {
292
+ it ( 'should properly set error' , ( ) => {
293
+ // Arrange
294
+ const error = 'Some error'
295
+ const state = {
296
+ ...initialState ,
297
+ loading : false ,
298
+ }
299
+
300
+ // Act
301
+ const nextState = reducer ( initialState , getContentRecommendationsFailure ( ) )
302
+
303
+ // Assert
304
+ const rootState = Object . assign ( initialStateDefault , {
305
+ recommendations : nextState ,
306
+ } )
307
+ expect ( recommendationsSelector ( rootState ) ) . toEqual ( state )
308
+ } )
309
+ } )
310
+
311
+ describe ( 'getContentRecommendationsSuccess' , ( ) => {
312
+ it ( 'should properly set loading: true' , ( ) => {
313
+ const payload = MOCK_RECOMMENDATIONS
314
+ // Arrange
315
+ const state = {
316
+ ...initialState ,
317
+ loading : false ,
318
+ content : MOCK_RECOMMENDATIONS
319
+ }
320
+
321
+ // Act
322
+ const nextState = reducer ( initialState , getContentRecommendationsSuccess ( payload ) )
323
+
324
+ // Assert
325
+ const rootState = Object . assign ( initialStateDefault , {
326
+ recommendations : nextState ,
327
+ } )
328
+ expect ( recommendationsSelector ( rootState ) ) . toEqual ( state )
329
+ } )
330
+ } )
264
331
} )
265
332
266
333
// thunks
@@ -495,5 +562,48 @@ describe('recommendations slice', () => {
495
562
expect ( store . getActions ( ) ) . toEqual ( expectedActions )
496
563
} )
497
564
} )
565
+
566
+ describe ( 'fetchContentRecommendations' , ( ) => {
567
+ it ( 'succeed to get content recommendations' , async ( ) => {
568
+ const data = MOCK_RECOMMENDATIONS
569
+ const responsePayload = { status : 200 , data }
570
+
571
+ resourcesService . get = jest . fn ( ) . mockResolvedValue ( responsePayload )
572
+
573
+ // Act
574
+ await store . dispatch < any > ( fetchContentRecommendations ( ) )
575
+
576
+ // Assert
577
+ const expectedActions = [
578
+ getContentRecommendations ( ) ,
579
+ getContentRecommendationsSuccess ( data ) ,
580
+ ]
581
+
582
+ expect ( store . getActions ( ) ) . toEqual ( expectedActions )
583
+ } )
584
+
585
+ it ( 'failed to get content recommendations' , async ( ) => {
586
+ const errorMessage = 'Something was wrong!'
587
+ const responsePayload = {
588
+ response : {
589
+ status : 500 ,
590
+ data : { message : errorMessage } ,
591
+ } ,
592
+ }
593
+
594
+ resourcesService . get = jest . fn ( ) . mockRejectedValue ( responsePayload )
595
+
596
+ // Act
597
+ await store . dispatch < any > ( fetchContentRecommendations ( ) )
598
+
599
+ // Assert
600
+ const expectedActions = [
601
+ getContentRecommendations ( ) ,
602
+ getContentRecommendationsFailure ( )
603
+ ]
604
+
605
+ expect ( store . getActions ( ) ) . toEqual ( expectedActions )
606
+ } )
607
+ } )
498
608
} )
499
609
} )
0 commit comments