@@ -2,14 +2,22 @@ import { createSlice } from '@reduxjs/toolkit'
22import { createApi } from '@reduxjs/toolkit/query'
33import { setupApiStore } from './helpers'
44
5+ let shouldApiResponseSuccess = true
6+
7+ function delay ( ms : number ) {
8+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
9+ }
10+
511const baseQuery = ( args ?: any ) => ( { data : args } )
612const api = createApi ( {
713 baseQuery,
14+ tagTypes : [ 'SUCCEED' , 'FAILED' ] ,
815 endpoints : ( build ) => ( {
9- getUser : build . query < unknown , number > ( {
16+ getUser : build . query < { url : string ; success : boolean } , number > ( {
1017 query ( id ) {
11- return { url : `user/${ id } ` }
18+ return { url : `user/${ id } ` , success : shouldApiResponseSuccess }
1219 } ,
20+ providesTags : ( result ) => ( result ?. success ? [ 'SUCCEED' ] : [ 'FAILED' ] ) ,
1321 } ) ,
1422 } ) ,
1523} )
@@ -29,60 +37,86 @@ const authSlice = createSlice({
2937
3038const storeRef = setupApiStore ( api , { auth : authSlice . reducer } )
3139
32- function delay ( ms : number ) {
33- return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
34- }
40+ describe ( 'buildSlice' , ( ) => {
41+ beforeEach ( ( ) => {
42+ shouldApiResponseSuccess = true
43+ } )
3544
36- it ( 'only resets the api state when resetApiState is dispatched' , async ( ) => {
37- storeRef . store . dispatch ( { type : 'unrelated' } ) // trigger "registered middleware" into place
38- const initialState = storeRef . store . getState ( )
39-
40- await storeRef . store . dispatch (
41- getUser . initiate ( 1 , { subscriptionOptions : { pollingInterval : 10 } } )
42- )
43-
44- expect ( storeRef . store . getState ( ) ) . toEqual ( {
45- api : {
46- config : {
47- focused : true ,
48- keepUnusedDataFor : 60 ,
49- middlewareRegistered : true ,
50- online : true ,
51- reducerPath : 'api' ,
52- refetchOnFocus : false ,
53- refetchOnMountOrArgChange : false ,
54- refetchOnReconnect : false ,
55- } ,
56- mutations : { } ,
57- provided : { } ,
58- queries : {
59- 'getUser(1)' : {
60- data : {
61- url : 'user/1' ,
45+ it ( 'only resets the api state when resetApiState is dispatched' , async ( ) => {
46+ storeRef . store . dispatch ( { type : 'unrelated' } ) // trigger "registered middleware" into place
47+ const initialState = storeRef . store . getState ( )
48+
49+ await storeRef . store . dispatch (
50+ getUser . initiate ( 1 , { subscriptionOptions : { pollingInterval : 10 } } )
51+ )
52+
53+ expect ( storeRef . store . getState ( ) ) . toEqual ( {
54+ api : {
55+ config : {
56+ focused : true ,
57+ keepUnusedDataFor : 60 ,
58+ middlewareRegistered : true ,
59+ online : true ,
60+ reducerPath : 'api' ,
61+ refetchOnFocus : false ,
62+ refetchOnMountOrArgChange : false ,
63+ refetchOnReconnect : false ,
64+ } ,
65+ mutations : { } ,
66+ provided : { } ,
67+ queries : {
68+ 'getUser(1)' : {
69+ data : {
70+ url : 'user/1' ,
71+ } ,
72+ endpointName : 'getUser' ,
73+ fulfilledTimeStamp : expect . any ( Number ) ,
74+ originalArgs : 1 ,
75+ requestId : expect . any ( String ) ,
76+ startedTimeStamp : expect . any ( Number ) ,
77+ status : 'fulfilled' ,
6278 } ,
63- endpointName : 'getUser' ,
64- fulfilledTimeStamp : expect . any ( Number ) ,
65- originalArgs : 1 ,
66- requestId : expect . any ( String ) ,
67- startedTimeStamp : expect . any ( Number ) ,
68- status : 'fulfilled' ,
79+ } ,
80+ subscriptions : {
81+ 'getUser(1)' : expect . any ( Object ) ,
6982 } ,
7083 } ,
71- subscriptions : {
72- 'getUser(1)' : expect . any ( Object ) ,
84+ auth : {
85+ token : '1234' ,
7386 } ,
74- } ,
75- auth : {
76- token : '1234' ,
77- } ,
87+ } )
88+
89+ storeRef . store . dispatch ( api . util . resetApiState ( ) )
90+
91+ expect ( storeRef . store . getState ( ) ) . toEqual ( initialState )
7892 } )
7993
80- storeRef . store . dispatch ( api . util . resetApiState ( ) )
94+ it ( 'replaces previous tags with new provided tags' , async ( ) => {
95+ await storeRef . store . dispatch ( getUser . initiate ( 1 ) )
96+
97+ expect (
98+ api . util . selectInvalidatedBy ( storeRef . store . getState ( ) , [ 'SUCCEED' ] )
99+ ) . toHaveLength ( 1 )
100+ expect (
101+ api . util . selectInvalidatedBy ( storeRef . store . getState ( ) , [ 'FAILED' ] )
102+ ) . toHaveLength ( 0 )
81103
82- expect ( storeRef . store . getState ( ) ) . toEqual ( initialState )
104+ shouldApiResponseSuccess = false
105+
106+ storeRef . store . dispatch ( getUser . initiate ( 1 ) ) . refetch ( )
107+
108+ await delay ( 10 )
109+
110+ expect (
111+ api . util . selectInvalidatedBy ( storeRef . store . getState ( ) , [ 'SUCCEED' ] )
112+ ) . toHaveLength ( 0 )
113+ expect (
114+ api . util . selectInvalidatedBy ( storeRef . store . getState ( ) , [ 'FAILED' ] )
115+ ) . toHaveLength ( 1 )
116+ } )
83117} )
84118
85- describe . only ( '`merge` callback' , ( ) => {
119+ describe ( '`merge` callback' , ( ) => {
86120 const baseQuery = ( args ?: any ) => ( { data : args } )
87121
88122 interface Todo {
0 commit comments