@@ -52,6 +52,9 @@ beforeEach(() => {
52
52
; [ api1 , api1_2 , api2 ] = createApis ( )
53
53
} )
54
54
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
+
55
58
describe ( 'missing middleware' , ( ) => {
56
59
test . each ( [
57
60
[ 'development' , true ] ,
@@ -61,13 +64,14 @@ describe('missing middleware', () => {
61
64
const store = configureStore ( {
62
65
reducer : { [ api1 . reducerPath ] : api1 . reducer } ,
63
66
} )
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
+ }
71
75
} )
72
76
73
77
test ( 'does not warn if middleware is not missing' , ( ) => {
@@ -83,11 +87,12 @@ Features like automatic cache collection, automatic refetching etc. will not be
83
87
const store = configureStore ( {
84
88
reducer : { [ api1 . reducerPath ] : api1 . reducer } ,
85
89
} )
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 ( )
91
96
} )
92
97
93
98
test ( 'warns multiple times for multiple apis' , ( ) => {
@@ -97,13 +102,16 @@ Features like automatic cache collection, automatic refetching etc. will not be
97
102
[ api2 . reducerPath ] : api2 . reducer ,
98
103
} ,
99
104
} )
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
+ )
107
115
} )
108
116
} )
109
117
0 commit comments