@@ -207,55 +207,60 @@ describe('createStore', () => {
207
207
] )
208
208
} )
209
209
210
+ it ( `doesn't notify listeners when state didn't change after action` , ( ) => {
211
+ const store = createStore ( reducers . todos )
212
+ const listener = vi . fn ( )
213
+
214
+ store . subscribe ( listener )
215
+ // @ts -expect-error
216
+ store . dispatch ( unknownAction ( ) )
217
+
218
+ expect ( listener . mock . calls . length ) . toBe ( 0 )
219
+ } )
220
+
210
221
it ( 'supports multiple subscriptions' , ( ) => {
211
222
const store = createStore ( reducers . todos )
212
223
const listenerA = vi . fn ( )
213
224
const listenerB = vi . fn ( )
214
225
215
226
let unsubscribeA = store . subscribe ( listenerA )
216
- // @ts -expect-error
217
- store . dispatch ( unknownAction ( ) )
227
+ store . dispatch ( addTodo ( '' ) )
218
228
expect ( listenerA . mock . calls . length ) . toBe ( 1 )
219
229
expect ( listenerB . mock . calls . length ) . toBe ( 0 )
220
230
221
- // @ts -expect-error
222
- store . dispatch ( unknownAction ( ) )
231
+ store . dispatch ( addTodo ( '' ) )
223
232
expect ( listenerA . mock . calls . length ) . toBe ( 2 )
224
233
expect ( listenerB . mock . calls . length ) . toBe ( 0 )
225
234
226
235
const unsubscribeB = store . subscribe ( listenerB )
227
236
expect ( listenerA . mock . calls . length ) . toBe ( 2 )
228
237
expect ( listenerB . mock . calls . length ) . toBe ( 0 )
229
238
230
- // @ts -expect-error
231
- store . dispatch ( unknownAction ( ) )
239
+ store . dispatch ( addTodo ( '' ) )
232
240
expect ( listenerA . mock . calls . length ) . toBe ( 3 )
233
241
expect ( listenerB . mock . calls . length ) . toBe ( 1 )
234
242
235
243
unsubscribeA ( )
236
244
expect ( listenerA . mock . calls . length ) . toBe ( 3 )
237
245
expect ( listenerB . mock . calls . length ) . toBe ( 1 )
238
246
239
- // @ts -expect-error
240
- store . dispatch ( unknownAction ( ) )
247
+ store . dispatch ( addTodo ( '' ) )
241
248
expect ( listenerA . mock . calls . length ) . toBe ( 3 )
242
249
expect ( listenerB . mock . calls . length ) . toBe ( 2 )
243
250
244
251
unsubscribeB ( )
245
252
expect ( listenerA . mock . calls . length ) . toBe ( 3 )
246
253
expect ( listenerB . mock . calls . length ) . toBe ( 2 )
247
254
248
- // @ts -expect-error
249
- store . dispatch ( unknownAction ( ) )
255
+ store . dispatch ( addTodo ( '' ) )
250
256
expect ( listenerA . mock . calls . length ) . toBe ( 3 )
251
257
expect ( listenerB . mock . calls . length ) . toBe ( 2 )
252
258
253
259
unsubscribeA = store . subscribe ( listenerA )
254
260
expect ( listenerA . mock . calls . length ) . toBe ( 3 )
255
261
expect ( listenerB . mock . calls . length ) . toBe ( 2 )
256
262
257
- // @ts -expect-error
258
- store . dispatch ( unknownAction ( ) )
263
+ store . dispatch ( addTodo ( '' ) )
259
264
expect ( listenerA . mock . calls . length ) . toBe ( 4 )
260
265
expect ( listenerB . mock . calls . length ) . toBe ( 2 )
261
266
} )
@@ -271,8 +276,7 @@ describe('createStore', () => {
271
276
unsubscribeA ( )
272
277
unsubscribeA ( )
273
278
274
- // @ts -expect-error
275
- store . dispatch ( unknownAction ( ) )
279
+ store . dispatch ( addTodo ( '' ) )
276
280
expect ( listenerA . mock . calls . length ) . toBe ( 0 )
277
281
expect ( listenerB . mock . calls . length ) . toBe ( 1 )
278
282
} )
@@ -287,8 +291,7 @@ describe('createStore', () => {
287
291
unsubscribeSecond ( )
288
292
unsubscribeSecond ( )
289
293
290
- // @ts -expect-error
291
- store . dispatch ( unknownAction ( ) )
294
+ store . dispatch ( addTodo ( '' ) )
292
295
expect ( listener . mock . calls . length ) . toBe ( 1 )
293
296
} )
294
297
@@ -305,10 +308,8 @@ describe('createStore', () => {
305
308
} )
306
309
store . subscribe ( listenerC )
307
310
308
- // @ts -expect-error
309
- store . dispatch ( unknownAction ( ) )
310
- // @ts -expect-error
311
- store . dispatch ( unknownAction ( ) )
311
+ store . dispatch ( addTodo ( '' ) )
312
+ store . dispatch ( addTodo ( '' ) )
312
313
313
314
expect ( listenerA . mock . calls . length ) . toBe ( 2 )
314
315
expect ( listenerB . mock . calls . length ) . toBe ( 1 )
@@ -335,14 +336,12 @@ describe('createStore', () => {
335
336
)
336
337
unsubscribeHandles . push ( store . subscribe ( ( ) => listener3 ( ) ) )
337
338
338
- // @ts -expect-error
339
- store . dispatch ( unknownAction ( ) )
339
+ store . dispatch ( addTodo ( '' ) )
340
340
expect ( listener1 . mock . calls . length ) . toBe ( 1 )
341
341
expect ( listener2 . mock . calls . length ) . toBe ( 1 )
342
342
expect ( listener3 . mock . calls . length ) . toBe ( 1 )
343
343
344
- // @ts -expect-error
345
- store . dispatch ( unknownAction ( ) )
344
+ store . dispatch ( addTodo ( '' ) )
346
345
expect ( listener1 . mock . calls . length ) . toBe ( 1 )
347
346
expect ( listener2 . mock . calls . length ) . toBe ( 1 )
348
347
expect ( listener3 . mock . calls . length ) . toBe ( 1 )
@@ -369,14 +368,12 @@ describe('createStore', () => {
369
368
maybeAddThirdListener ( )
370
369
} )
371
370
372
- // @ts -expect-error
373
- store . dispatch ( unknownAction ( ) )
371
+ store . dispatch ( addTodo ( '' ) )
374
372
expect ( listener1 . mock . calls . length ) . toBe ( 1 )
375
373
expect ( listener2 . mock . calls . length ) . toBe ( 1 )
376
374
expect ( listener3 . mock . calls . length ) . toBe ( 0 )
377
375
378
- // @ts -expect-error
379
- store . dispatch ( unknownAction ( ) )
376
+ store . dispatch ( addTodo ( '' ) )
380
377
expect ( listener1 . mock . calls . length ) . toBe ( 2 )
381
378
expect ( listener2 . mock . calls . length ) . toBe ( 2 )
382
379
expect ( listener3 . mock . calls . length ) . toBe ( 1 )
@@ -400,8 +397,7 @@ describe('createStore', () => {
400
397
401
398
unsubscribe1 ( )
402
399
unsubscribe4 = store . subscribe ( listener4 )
403
- // @ts -expect-error
404
- store . dispatch ( unknownAction ( ) )
400
+ store . dispatch ( addTodo ( '' ) )
405
401
406
402
expect ( listener1 . mock . calls . length ) . toBe ( 1 )
407
403
expect ( listener2 . mock . calls . length ) . toBe ( 1 )
@@ -411,16 +407,14 @@ describe('createStore', () => {
411
407
store . subscribe ( listener2 )
412
408
store . subscribe ( listener3 )
413
409
414
- // @ts -expect-error
415
- store . dispatch ( unknownAction ( ) )
410
+ store . dispatch ( addTodo ( '' ) )
416
411
expect ( listener1 . mock . calls . length ) . toBe ( 1 )
417
412
expect ( listener2 . mock . calls . length ) . toBe ( 2 )
418
413
expect ( listener3 . mock . calls . length ) . toBe ( 2 )
419
414
expect ( listener4 . mock . calls . length ) . toBe ( 1 )
420
415
421
416
unsubscribe4 ( )
422
- // @ts -expect-error
423
- store . dispatch ( unknownAction ( ) )
417
+ store . dispatch ( addTodo ( '' ) )
424
418
expect ( listener1 . mock . calls . length ) . toBe ( 1 )
425
419
expect ( listener2 . mock . calls . length ) . toBe ( 3 )
426
420
expect ( listener3 . mock . calls . length ) . toBe ( 3 )
0 commit comments