@@ -52,7 +52,7 @@ test('should update when using async actions', async () => {
5252 expect ( get ( 'count' ) ) . toBe ( 7 )
5353} )
5454
55- test ( 'should always return master record ' , async ( ) => {
55+ test ( 'should only return updated state after emit ' , async ( ) => {
5656 class Store extends TwoAndEight {
5757 count = 0
5858
@@ -67,6 +67,15 @@ test('should always return master record', async () => {
6767 } )
6868 this . count = this . count + 5
6969 }
70+
71+ asyncWithEarlyEmitButtonClicked = async ( ) => {
72+ this . count ++
73+ this . $emit ( )
74+ await new Promise ( ( res ) => {
75+ setTimeout ( res , 1000 )
76+ } )
77+ this . count = this . count + 2
78+ }
7079 }
7180
7281 const { get } = createStore ( new Store ( ) )
@@ -75,7 +84,7 @@ test('should always return master record', async () => {
7584
7685 get ( 'asyncButtonClicked' ) ( )
7786
78- expect ( get ( 'count' ) ) . toBe ( 1 )
87+ expect ( get ( 'count' ) ) . toBe ( 0 )
7988
8089 get ( 'buttonClicked' ) ( )
8190
@@ -84,6 +93,14 @@ test('should always return master record', async () => {
8493 await vi . advanceTimersByTimeAsync ( 3000 )
8594
8695 expect ( get ( 'count' ) ) . toBe ( 7 )
96+
97+ get ( 'asyncWithEarlyEmitButtonClicked' ) ( )
98+
99+ expect ( get ( 'count' ) ) . toBe ( 8 )
100+
101+ await vi . advanceTimersByTimeAsync ( 1000 )
102+
103+ expect ( get ( 'count' ) ) . toBe ( 10 )
87104} )
88105
89106test ( 'should reset all state' , ( ) => {
@@ -467,8 +484,6 @@ test('should return current state', () => {
467484 const { get, subscribe } = createStore ( new Store ( ) )
468485 get ( 'increaseCount' ) ( )
469486
470- expect ( get ( '$emit' ) ) . toStrictEqual ( expect . any ( Function ) )
471- expect ( get ( '$reset' ) ) . toStrictEqual ( expect . any ( Function ) )
472487 expect ( get ( 'increaseCount' ) ) . toStrictEqual ( expect . any ( Function ) )
473488 expect ( get ( 'count' ) ) . toBe ( 1 )
474489 expect ( get ( 'untouched' ) ) . toBe ( 'foo' )
@@ -496,14 +511,18 @@ test('should not call subscriber when state has not changed', () => {
496511
497512 const { get, subscribe } = createStore ( new Store ( ) )
498513 const subscribeSpy = vi . fn < ( ) => void > ( )
499- subscribe ( subscribeSpy )
514+ const subscribe2Spy = vi . fn < ( ) => void > ( )
515+ subscribe ( 'count' , subscribeSpy )
516+ subscribe ( 'obj' , subscribe2Spy )
500517 get ( 'noop' ) ( )
501518
502- expect ( subscribeSpy ) . toHaveBeenCalledOnce ( )
519+ expect ( subscribeSpy ) . not . toHaveBeenCalled ( )
520+ expect ( subscribe2Spy ) . not . toHaveBeenCalled ( )
503521
504522 get ( 'noop2' ) ( )
505523
506- expect ( subscribeSpy ) . toHaveBeenCalledTimes ( 2 )
524+ expect ( subscribeSpy ) . not . toHaveBeenCalled ( )
525+ expect ( subscribe2Spy ) . not . toHaveBeenCalled ( )
507526} )
508527
509528test ( 'should unsubscribe' , ( ) => {
@@ -517,7 +536,7 @@ test('should unsubscribe', () => {
517536
518537 const { get, subscribe } = createStore ( new Store ( ) )
519538 const spy = vi . fn < ( ) => void > ( )
520- const unsubscribe = subscribe ( spy )
539+ const unsubscribe = subscribe ( 'count' , spy )
521540 get ( 'increaseCount' ) ( )
522541
523542 expect ( spy ) . toHaveBeenCalledOnce ( )
@@ -576,8 +595,9 @@ test('should update deep state', () => {
576595 }
577596
578597 const { get, subscribe } = createStore ( new Store ( ) )
579- const subscribeSpy = vi . fn < ( ) => void > ( )
580- subscribe ( subscribeSpy )
598+ // TODO test other fields
599+ const objSubscribeSpy = vi . fn < ( ) => void > ( )
600+ subscribe ( 'obj' , objSubscribeSpy )
581601
582602 expect ( get ( 'obj' ) ) . toStrictEqual ( { foo : { bar : 'baz' } } )
583603 expect ( get ( 'arr' ) ) . toStrictEqual ( [ 'hello' ] )
@@ -587,13 +607,13 @@ test('should update deep state', () => {
587607 get ( 'other' ) ( )
588608 get ( 'push' ) ( )
589609
590- expect ( subscribeSpy ) . toHaveBeenCalledTimes ( 4 )
610+ expect ( objSubscribeSpy ) . toHaveBeenCalledOnce ( )
591611 expect ( get ( 'obj' ) ) . toStrictEqual ( { foo : { bar : 'moo' } } )
592612 expect ( get ( 'arr' ) ) . toStrictEqual ( [ 'hello' , 'bye' ] )
593613
594614 get ( 'delete' ) ( )
595615
596- expect ( subscribeSpy ) . toHaveBeenCalledTimes ( 5 )
616+ expect ( objSubscribeSpy ) . toHaveBeenCalledTimes ( 2 )
597617 expect ( get ( 'obj' ) ) . toStrictEqual ( { foo : { } } )
598618 expect ( get ( 'arr' ) ) . toStrictEqual ( [ 'hello' , 'bye' ] )
599619} )
@@ -616,7 +636,7 @@ test('should not fire subscription until end of action', () => {
616636
617637 const { get, subscribe } = createStore ( new Store ( ) )
618638 const subscribeSpy = vi . fn < ( ) => void > ( )
619- subscribe ( subscribeSpy )
639+ subscribe ( 'count' , subscribeSpy )
620640 get ( 'increment' ) ( )
621641
622642 expect ( subscribeSpy ) . toHaveBeenCalledOnce ( )
@@ -627,22 +647,32 @@ test('should not fire subscription until end of action', () => {
627647 expect ( subscribeSpy ) . toHaveBeenCalledTimes ( 2 )
628648} )
629649
630- test ( 'should not fire subscription if action begins with $ but state still updates ' , ( ) => {
650+ test ( 'should not fire subscription if action begins with $ and not update state until emit-able action called ' , ( ) => {
631651 class Store extends TwoAndEight {
632652 count = 999
633653
634654 $increment = ( ) => {
635655 this . count ++
636656 }
657+
658+ increment = ( ) => {
659+ this . count ++
660+ }
637661 }
638662
639663 const { get, subscribe } = createStore ( new Store ( ) )
640664 const subscribeSpy = vi . fn < ( ) => void > ( )
641- subscribe ( subscribeSpy )
665+ subscribe ( 'count' , subscribeSpy )
666+ // @ts -expect-error -- normally you shouldn't call $ actions directly.
642667 get ( '$increment' ) ( )
643668
644- expect ( get ( 'count' ) ) . toBe ( 1000 )
669+ expect ( get ( 'count' ) ) . toBe ( 999 )
645670 expect ( subscribeSpy ) . not . toHaveBeenCalled ( )
671+
672+ get ( 'increment' ) ( )
673+
674+ expect ( get ( 'count' ) ) . toBe ( 1001 )
675+ expect ( subscribeSpy ) . toHaveBeenCalledOnce ( )
646676} )
647677
648678test ( "should fail types when store hasn't extended super class" , ( ) => {
0 commit comments