@@ -52,6 +52,9 @@ beforeEach(() => {
5252 ; [ api1 , api1_2 , api2 ] = createApis ( )
5353} )
5454
55+ const reMatchMissingMiddlewareError =
56+ / W a r n i n g : M i d d l e w a r e f o r R T K - Q u e r y A P I a t r e d u c e r P a t h " a p i " h a s n o t b e e n a d d e d t o t h e s t o r e /
57+
5558describe ( 'missing middleware' , ( ) => {
5659 test . each ( [
5760 [ 'development' , true ] ,
@@ -61,13 +64,14 @@ describe('missing middleware', () => {
6164 const store = configureStore ( {
6265 reducer : { [ api1 . reducerPath ] : api1 . reducer } ,
6366 } )
64- store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
65- expect ( getLog ( ) . log ) . toBe (
66- shouldWarn
67- ? `Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store.
68- Features like automatic cache collection, automatic refetching etc. will not be available.`
69- : ''
70- )
67+ const doDispatch = ( ) => {
68+ store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
69+ }
70+ if ( shouldWarn ) {
71+ expect ( doDispatch ) . toThrowError ( reMatchMissingMiddlewareError )
72+ } else {
73+ expect ( doDispatch ) . not . toThrowError ( )
74+ }
7175 } )
7276
7377 test ( 'does not warn if middleware is not missing' , ( ) => {
@@ -83,11 +87,12 @@ Features like automatic cache collection, automatic refetching etc. will not be
8387 const store = configureStore ( {
8488 reducer : { [ api1 . reducerPath ] : api1 . reducer } ,
8589 } )
86- store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
87- store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
88- expect ( getLog ( ) . log )
89- . toBe ( `Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store.
90- Features like automatic cache collection, automatic refetching etc. will not be available.` )
90+ const doDispatch = ( ) => {
91+ store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
92+ }
93+
94+ expect ( doDispatch ) . toThrowError ( reMatchMissingMiddlewareError )
95+ expect ( doDispatch ) . not . toThrowError ( )
9196 } )
9297
9398 test ( 'warns multiple times for multiple apis' , ( ) => {
@@ -97,13 +102,16 @@ Features like automatic cache collection, automatic refetching etc. will not be
97102 [ api2 . reducerPath ] : api2 . reducer ,
98103 } ,
99104 } )
100- store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
101- store . dispatch ( api2 . endpoints . q1 . initiate ( undefined ) )
102- expect ( getLog ( ) . log )
103- . toBe ( `Warning: Middleware for RTK-Query API at reducerPath "api" has not been added to the store.
104- Features like automatic cache collection, automatic refetching etc. will not be available.
105- Warning: Middleware for RTK-Query API at reducerPath "api2" has not been added to the store.
106- Features like automatic cache collection, automatic refetching etc. will not be available.` )
105+ const doDispatch1 = ( ) => {
106+ store . dispatch ( api1 . endpoints . q1 . initiate ( undefined ) )
107+ }
108+ const doDispatch2 = ( ) => {
109+ store . dispatch ( api2 . endpoints . q1 . initiate ( undefined ) )
110+ }
111+ expect ( doDispatch1 ) . toThrowError ( reMatchMissingMiddlewareError )
112+ expect ( doDispatch2 ) . toThrowError (
113+ / W a r n i n g : M i d d l e w a r e f o r R T K - Q u e r y A P I a t r e d u c e r P a t h " a p i 2 " h a s n o t b e e n a d d e d t o t h e s t o r e /
114+ )
107115 } )
108116} )
109117
0 commit comments