@@ -21,24 +21,11 @@ describe('Testing', () => {
2121 increment ( amount = 1 ) {
2222 this . n += amount
2323 } ,
24- } ,
25- } )
26-
27- const useMultiActionStore = defineStore ( 'multi-action' , {
28- state : ( ) => ( { count : 0 , value : 0 } ) ,
29- actions : {
30- increment ( ) {
31- this . count ++
32- } ,
3324 decrement ( ) {
34- this . count --
25+ this . n --
3526 } ,
3627 setValue ( newValue : number ) {
37- this . value = newValue
38- } ,
39- reset ( ) {
40- this . count = 0
41- this . value = 0
28+ this . n = newValue
4229 } ,
4330 } ,
4431 } )
@@ -54,6 +41,12 @@ describe('Testing', () => {
5441 function increment ( amount = 1 ) {
5542 n . value += amount
5643 }
44+ function decrement ( ) {
45+ n . value --
46+ }
47+ function setValue ( newValue : number ) {
48+ n . value = newValue
49+ }
5750 function $reset ( ) {
5851 n . value = 0
5952 }
@@ -64,6 +57,8 @@ describe('Testing', () => {
6457 double,
6558 doublePlusOne,
6659 increment,
60+ decrement,
61+ setValue,
6762 $reset,
6863 }
6964 } )
@@ -345,6 +340,154 @@ describe('Testing', () => {
345340 storeToRefs ( store )
346341 expect ( store . doubleComputedCallCount ) . toBe ( 0 )
347342 } )
343+
344+ describe ( 'selective action stubbing' , ( ) => {
345+ it ( 'stubs only actions in array' , ( ) => {
346+ setActivePinia (
347+ createTestingPinia ( {
348+ stubActions : [ 'increment' , 'setValue' ] ,
349+ createSpy : vi . fn ,
350+ } )
351+ )
352+
353+ const store = useStore ( )
354+
355+ // Actions in array should be stubbed (not execute)
356+ store . increment ( )
357+ expect ( store . n ) . toBe ( 0 ) // Should not change
358+ expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
359+
360+ store . setValue ( 42 )
361+ expect ( store . n ) . toBe ( 0 ) // Should not change
362+ expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
363+ expect ( store . setValue ) . toHaveBeenLastCalledWith ( 42 )
364+
365+ // Actions not in array should execute normally but still be spied
366+ store . decrement ( )
367+ expect ( store . n ) . toBe ( - 1 ) // Should change
368+ expect ( store . decrement ) . toHaveBeenCalledTimes ( 1 )
369+ } )
370+
371+ it ( 'handles empty array (same as false)' , ( ) => {
372+ setActivePinia (
373+ createTestingPinia ( {
374+ stubActions : [ ] ,
375+ createSpy : vi . fn ,
376+ } )
377+ )
378+
379+ const store = useStore ( )
380+
381+ // All actions should execute normally
382+ store . increment ( )
383+ expect ( store . n ) . toBe ( 1 ) // Should change
384+ expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
385+
386+ store . setValue ( 42 )
387+ expect ( store . n ) . toBe ( 42 ) // Should change
388+ expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
389+ } )
390+
391+ it ( 'handles non-existent action names gracefully' , ( ) => {
392+ setActivePinia (
393+ createTestingPinia ( {
394+ stubActions : [ 'increment' , 'nonExistentAction' ] ,
395+ createSpy : vi . fn ,
396+ } )
397+ )
398+
399+ const store = useStore ( )
400+
401+ // Should work normally despite non-existent action in array
402+ store . increment ( )
403+ expect ( store . n ) . toBe ( 0 ) // Should not change
404+ expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
405+
406+ store . setValue ( 42 )
407+ expect ( store . n ) . toBe ( 42 ) // Should change (not in array)
408+ expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
409+ } )
410+
411+ it ( 'stubs actions based on function predicate' , ( ) => {
412+ setActivePinia (
413+ createTestingPinia ( {
414+ stubActions : ( actionName ) =>
415+ actionName . startsWith ( 'set' ) || actionName === 'decrement' ,
416+ createSpy : vi . fn ,
417+ } )
418+ )
419+
420+ const store = useStore ( )
421+
422+ // setValue should be stubbed (starts with 'set')
423+ store . setValue ( 42 )
424+ expect ( store . n ) . toBe ( 0 ) // Should not change
425+ expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
426+
427+ // increment should execute (doesn't match predicate)
428+ store . increment ( )
429+ expect ( store . n ) . toBe ( 1 ) // Should change
430+ expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
431+
432+ // decrement should be stubbed (matches predicate)
433+ store . decrement ( )
434+ expect ( store . n ) . toBe ( 1 ) // Should not change (stubbed)
435+ expect ( store . decrement ) . toHaveBeenCalledTimes ( 1 )
436+ } )
437+
438+ it ( 'function predicate receives correct store instance' , ( ) => {
439+ const predicateSpy = vi . fn ( ( ) => false )
440+
441+ setActivePinia (
442+ createTestingPinia ( {
443+ stubActions : predicateSpy ,
444+ createSpy : vi . fn ,
445+ } )
446+ )
447+
448+ const store = useStore ( )
449+
450+ expect ( predicateSpy ) . toHaveBeenCalledWith ( 'increment' , store )
451+ } )
452+
453+ it ( 'can stub all actions (default)' , ( ) => {
454+ setActivePinia (
455+ createTestingPinia ( {
456+ stubActions : true ,
457+ createSpy : vi . fn ,
458+ } )
459+ )
460+
461+ const store = useStore ( )
462+
463+ store . increment ( )
464+ expect ( store . n ) . toBe ( 0 ) // Should not change
465+ expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
466+
467+ store . setValue ( 42 )
468+ expect ( store . n ) . toBe ( 0 ) // Should not change
469+ expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
470+ } )
471+
472+ it ( 'can not stub any action' , ( ) => {
473+ setActivePinia (
474+ createTestingPinia ( {
475+ stubActions : false ,
476+ createSpy : vi . fn ,
477+ } )
478+ )
479+
480+ const store = useStore ( )
481+
482+ store . increment ( )
483+ expect ( store . n ) . toBe ( 1 ) // Should change
484+ expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
485+
486+ store . setValue ( 42 )
487+ expect ( store . n ) . toBe ( 42 ) // Should change
488+ expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
489+ } )
490+ } )
348491 }
349492
350493 it ( 'works with no actions' , ( ) => {
@@ -408,188 +551,4 @@ describe('Testing', () => {
408551 b : { n : 0 } ,
409552 } )
410553 } )
411-
412- describe ( 'selective action stubbing' , ( ) => {
413- it ( 'stubs only included actions' , ( ) => {
414- setActivePinia (
415- createTestingPinia ( {
416- stubActions : { include : [ 'increment' , 'setValue' ] } ,
417- createSpy : vi . fn ,
418- } )
419- )
420-
421- const store = useMultiActionStore ( )
422-
423- // Included actions should be stubbed (not execute)
424- store . increment ( )
425- expect ( store . count ) . toBe ( 0 ) // Should not change
426- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
427-
428- store . setValue ( 42 )
429- expect ( store . value ) . toBe ( 0 ) // Should not change
430- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
431- expect ( store . setValue ) . toHaveBeenLastCalledWith ( 42 )
432-
433- // Excluded actions should execute normally but still be spied
434- store . decrement ( )
435- expect ( store . count ) . toBe ( - 1 ) // Should change
436- expect ( store . decrement ) . toHaveBeenCalledTimes ( 1 )
437-
438- store . reset ( )
439- expect ( store . count ) . toBe ( 0 ) // Should change
440- expect ( store . value ) . toBe ( 0 ) // Should change
441- expect ( store . reset ) . toHaveBeenCalledTimes ( 1 )
442- } )
443-
444- it ( 'stubs all actions except excluded ones' , ( ) => {
445- setActivePinia (
446- createTestingPinia ( {
447- stubActions : { exclude : [ 'increment' , 'setValue' ] } ,
448- createSpy : vi . fn ,
449- } )
450- )
451-
452- const store = useMultiActionStore ( )
453-
454- // Excluded actions should execute normally but still be spied
455- store . increment ( )
456- expect ( store . count ) . toBe ( 1 ) // Should change
457- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
458-
459- store . setValue ( 42 )
460- expect ( store . value ) . toBe ( 42 ) // Should change
461- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
462- expect ( store . setValue ) . toHaveBeenLastCalledWith ( 42 )
463-
464- // Non-excluded actions should be stubbed (not execute)
465- store . decrement ( )
466- expect ( store . count ) . toBe ( 1 ) // Should not change
467- expect ( store . decrement ) . toHaveBeenCalledTimes ( 1 )
468-
469- store . reset ( )
470- expect ( store . count ) . toBe ( 1 ) // Should not change
471- expect ( store . value ) . toBe ( 42 ) // Should not change
472- expect ( store . reset ) . toHaveBeenCalledTimes ( 1 )
473- } )
474-
475- it ( 'handles empty include array (stubs all actions)' , ( ) => {
476- setActivePinia (
477- createTestingPinia ( {
478- stubActions : { include : [ ] } ,
479- createSpy : vi . fn ,
480- } )
481- )
482-
483- const store = useMultiActionStore ( )
484-
485- store . increment ( )
486- expect ( store . count ) . toBe ( 0 ) // Should not change
487- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
488-
489- store . setValue ( 42 )
490- expect ( store . value ) . toBe ( 0 ) // Should not change
491- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
492- } )
493-
494- it ( 'handles empty exclude array (stubs all actions)' , ( ) => {
495- setActivePinia (
496- createTestingPinia ( {
497- stubActions : { exclude : [ ] } ,
498- createSpy : vi . fn ,
499- } )
500- )
501-
502- const store = useMultiActionStore ( )
503-
504- store . increment ( )
505- expect ( store . count ) . toBe ( 0 ) // Should not change
506- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
507-
508- store . setValue ( 42 )
509- expect ( store . value ) . toBe ( 0 ) // Should not change
510- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
511- } )
512-
513- it ( 'handles both include and exclude (include takes precedence)' , ( ) => {
514- setActivePinia (
515- createTestingPinia ( {
516- stubActions : {
517- include : [ 'increment' ] ,
518- exclude : [ 'increment' , 'setValue' ] ,
519- } ,
520- createSpy : vi . fn ,
521- } )
522- )
523-
524- const store = useMultiActionStore ( )
525-
526- // Include takes precedence - increment should be stubbed
527- store . increment ( )
528- expect ( store . count ) . toBe ( 0 ) // Should not change
529- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
530-
531- // Not in include list - should execute normally
532- store . setValue ( 42 )
533- expect ( store . value ) . toBe ( 42 ) // Should change
534- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
535- } )
536-
537- it ( 'maintains backward compatibility with boolean true' , ( ) => {
538- setActivePinia (
539- createTestingPinia ( {
540- stubActions : true ,
541- createSpy : vi . fn ,
542- } )
543- )
544-
545- const store = useMultiActionStore ( )
546-
547- store . increment ( )
548- expect ( store . count ) . toBe ( 0 ) // Should not change
549- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
550-
551- store . setValue ( 42 )
552- expect ( store . value ) . toBe ( 0 ) // Should not change
553- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
554- } )
555-
556- it ( 'maintains backward compatibility with boolean false' , ( ) => {
557- setActivePinia (
558- createTestingPinia ( {
559- stubActions : false ,
560- createSpy : vi . fn ,
561- } )
562- )
563-
564- const store = useMultiActionStore ( )
565-
566- store . increment ( )
567- expect ( store . count ) . toBe ( 1 ) // Should change
568- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
569-
570- store . setValue ( 42 )
571- expect ( store . value ) . toBe ( 42 ) // Should change
572- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
573- } )
574-
575- it ( 'handles non-existent action names gracefully' , ( ) => {
576- setActivePinia (
577- createTestingPinia ( {
578- stubActions : { include : [ 'increment' , 'nonExistentAction' ] } ,
579- createSpy : vi . fn ,
580- } )
581- )
582-
583- const store = useMultiActionStore ( )
584-
585- // Should work normally despite non-existent action in include list
586- store . increment ( )
587- expect ( store . count ) . toBe ( 0 ) // Should not change
588- expect ( store . increment ) . toHaveBeenCalledTimes ( 1 )
589-
590- store . setValue ( 42 )
591- expect ( store . value ) . toBe ( 42 ) // Should change (not in include list)
592- expect ( store . setValue ) . toHaveBeenCalledTimes ( 1 )
593- } )
594- } )
595554} )
0 commit comments