@@ -3,26 +3,22 @@ import { mount } from '@vue/test-utils'
3
3
import { nextTick } from 'vue'
4
4
5
5
describe ( 'Subscriptions' , ( ) => {
6
- const useStore = ( ) => {
7
- // create a new store
8
- setActivePinia ( createPinia ( ) )
9
- return defineStore ( {
10
- id : 'main' ,
11
- state : ( ) => ( {
12
- name : 'Eduardo' ,
13
- } ) ,
14
- } ) ( )
15
- }
6
+ const useStore = defineStore ( {
7
+ id : 'main' ,
8
+ state : ( ) => ( {
9
+ user : 'Eduardo' ,
10
+ } ) ,
11
+ } )
16
12
17
- let store : ReturnType < typeof useStore >
18
13
beforeEach ( ( ) => {
19
- store = useStore ( )
14
+ setActivePinia ( createPinia ( ) )
20
15
} )
21
16
22
17
it ( 'fires callback when patch is applied' , ( ) => {
18
+ const store = useStore ( )
23
19
const spy = jest . fn ( )
24
20
store . $subscribe ( spy , { flush : 'sync' } )
25
- store . $state . name = 'Cleiton'
21
+ store . $state . user = 'Cleiton'
26
22
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
27
23
expect ( spy ) . toHaveBeenCalledWith (
28
24
expect . objectContaining ( {
@@ -38,7 +34,7 @@ describe('Subscriptions', () => {
38
34
const spy = jest . fn ( )
39
35
store . $subscribe ( spy , { flush : 'sync' } )
40
36
41
- const patch = { name : 'Cleiton' }
37
+ const patch = { user : 'Cleiton' }
42
38
store . $patch ( patch )
43
39
44
40
expect ( spy ) . toHaveBeenCalledWith (
@@ -53,34 +49,28 @@ describe('Subscriptions', () => {
53
49
54
50
it ( 'unsubscribes callback when unsubscribe is called' , ( ) => {
55
51
const spy = jest . fn ( )
52
+ const store = useStore ( )
56
53
const unsubscribe = store . $subscribe ( spy , { flush : 'sync' } )
57
54
unsubscribe ( )
58
- store . $state . name = 'Cleiton'
55
+ store . $state . user = 'Cleiton'
59
56
expect ( spy ) . not . toHaveBeenCalled ( )
60
57
} )
61
58
62
59
it ( 'listeners are not affected when unsubscribe is called multiple times' , ( ) => {
63
60
const func1 = jest . fn ( )
64
61
const func2 = jest . fn ( )
62
+ const store = useStore ( )
65
63
const unsubscribe1 = store . $subscribe ( func1 , { flush : 'sync' } )
66
64
store . $subscribe ( func2 , { flush : 'sync' } )
67
65
unsubscribe1 ( )
68
66
unsubscribe1 ( )
69
- store . $state . name = 'Cleiton'
67
+ store . $state . user = 'Cleiton'
70
68
expect ( func1 ) . not . toHaveBeenCalled ( )
71
69
expect ( func2 ) . toHaveBeenCalledTimes ( 1 )
72
70
} )
73
71
74
72
describe ( 'multiple' , ( ) => {
75
- const useStore = defineStore ( {
76
- id : 'main' ,
77
- state : ( ) => ( {
78
- name : 'Eduardo' ,
79
- } ) ,
80
- } )
81
-
82
73
it ( 'triggers subscribe only once' , async ( ) => {
83
- setActivePinia ( createPinia ( ) )
84
74
const s1 = useStore ( )
85
75
const s2 = useStore ( )
86
76
@@ -93,14 +83,15 @@ describe('Subscriptions', () => {
93
83
expect ( spy1 ) . toHaveBeenCalledTimes ( 0 )
94
84
expect ( spy2 ) . toHaveBeenCalledTimes ( 0 )
95
85
96
- s1 . name = 'Edu'
86
+ s1 . user = 'Edu'
97
87
98
88
expect ( spy1 ) . toHaveBeenCalledTimes ( 1 )
99
89
expect ( spy2 ) . toHaveBeenCalledTimes ( 1 )
100
90
} )
101
91
102
92
it ( 'removes on unmount' , async ( ) => {
103
93
const pinia = createPinia ( )
94
+ setActivePinia ( pinia )
104
95
const spy1 = jest . fn ( )
105
96
const spy2 = jest . fn ( )
106
97
@@ -123,41 +114,42 @@ describe('Subscriptions', () => {
123
114
expect ( spy1 ) . toHaveBeenCalledTimes ( 0 )
124
115
expect ( spy2 ) . toHaveBeenCalledTimes ( 0 )
125
116
126
- s1 . name = 'Edu'
117
+ s1 . user = 'Edu'
127
118
expect ( spy1 ) . toHaveBeenCalledTimes ( 1 )
128
119
expect ( spy2 ) . toHaveBeenCalledTimes ( 1 )
129
120
130
- s1 . $patch ( { name : 'a' } )
121
+ s1 . $patch ( { user : 'a' } )
131
122
expect ( spy1 ) . toHaveBeenCalledTimes ( 2 )
132
123
expect ( spy2 ) . toHaveBeenCalledTimes ( 2 )
133
124
134
125
s1 . $patch ( ( state ) => {
135
- state . name = 'other'
126
+ state . user = 'other'
136
127
} )
137
128
expect ( spy1 ) . toHaveBeenCalledTimes ( 3 )
138
129
expect ( spy2 ) . toHaveBeenCalledTimes ( 3 )
139
130
140
131
wrapper . unmount ( )
141
132
await nextTick ( )
142
133
143
- s1 . $patch ( { name : 'b' } )
134
+ s1 . $patch ( { user : 'b' } )
144
135
expect ( spy1 ) . toHaveBeenCalledTimes ( 3 )
145
136
expect ( spy2 ) . toHaveBeenCalledTimes ( 4 )
146
137
s1 . $patch ( ( state ) => {
147
- state . name = 'c'
138
+ state . user = 'c'
148
139
} )
149
140
expect ( spy1 ) . toHaveBeenCalledTimes ( 3 )
150
141
expect ( spy2 ) . toHaveBeenCalledTimes ( 5 )
151
- s1 . name = 'd'
142
+ s1 . user = 'd'
152
143
expect ( spy1 ) . toHaveBeenCalledTimes ( 3 )
153
144
expect ( spy2 ) . toHaveBeenCalledTimes ( 6 )
154
145
} )
155
146
} )
156
147
157
148
it ( 'subscribe is post by default' , async ( ) => {
158
149
const spy = jest . fn ( )
150
+ const store = useStore ( )
159
151
store . $subscribe ( spy )
160
- store . $state . name = 'Cleiton'
152
+ store . $state . user = 'Cleiton'
161
153
expect ( spy ) . toHaveBeenCalledTimes ( 0 )
162
154
await nextTick ( )
163
155
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
0 commit comments