1
- import {
2
- reactive ,
3
- effect ,
4
- effectScope ,
5
- stop ,
6
- isEffectScope ,
7
- extendScope ,
8
- computed ,
9
- ref ,
10
- onScopeStopped
11
- } from '../src'
1
+ import { reactive , effect , EffectScope , onDispose } from '../src'
12
2
13
3
describe ( 'reactivity/effect/scope' , ( ) => {
14
4
it ( 'should run the passed function once (wrapped by a effect)' , ( ) => {
15
5
const fnSpy = jest . fn ( ( ) => { } )
16
- effectScope ( fnSpy )
6
+ new EffectScope ( fnSpy )
17
7
expect ( fnSpy ) . toHaveBeenCalledTimes ( 1 )
18
8
} )
19
9
20
10
it ( 'should accept zero argument' , ( ) => {
21
- const scope = effectScope ( )
22
- expect ( scope . _scope . effects . length ) . toBe ( 0 )
11
+ const scope = new EffectScope ( )
12
+ expect ( scope . effects . length ) . toBe ( 0 )
23
13
} )
24
14
25
15
it ( 'should collect the effects' , ( ) => {
26
- const scope = effectScope ( ( ) => {
16
+ const scope = new EffectScope ( ( ) => {
27
17
let dummy
28
18
const counter = reactive ( { num : 0 } )
29
19
effect ( ( ) => ( dummy = counter . num ) )
@@ -33,26 +23,26 @@ describe('reactivity/effect/scope', () => {
33
23
expect ( dummy ) . toBe ( 7 )
34
24
} )
35
25
36
- expect ( scope . _scope . effects . length ) . toBe ( 1 )
26
+ expect ( scope . effects . length ) . toBe ( 1 )
37
27
} )
38
28
39
29
it ( 'stop' , ( ) => {
40
30
let dummy , doubled
41
31
const counter = reactive ( { num : 0 } )
42
32
43
- const scope = effectScope ( ( ) => {
33
+ const scope = new EffectScope ( ( ) => {
44
34
effect ( ( ) => ( dummy = counter . num ) )
45
35
effect ( ( ) => ( doubled = counter . num * 2 ) )
46
36
} )
47
37
48
- expect ( scope . _scope . effects . length ) . toBe ( 2 )
38
+ expect ( scope . effects . length ) . toBe ( 2 )
49
39
50
40
expect ( dummy ) . toBe ( 0 )
51
41
counter . num = 7
52
42
expect ( dummy ) . toBe ( 7 )
53
43
expect ( doubled ) . toBe ( 14 )
54
44
55
- stop ( scope )
45
+ scope . stop ( )
56
46
57
47
counter . num = 6
58
48
expect ( dummy ) . toBe ( 7 )
@@ -63,24 +53,24 @@ describe('reactivity/effect/scope', () => {
63
53
let dummy , doubled
64
54
const counter = reactive ( { num : 0 } )
65
55
66
- const scope = effectScope ( ( ) => {
56
+ const scope = new EffectScope ( ( ) => {
67
57
effect ( ( ) => ( dummy = counter . num ) )
68
58
// nested scope
69
- effectScope ( ( ) => {
59
+ new EffectScope ( ( ) => {
70
60
effect ( ( ) => ( doubled = counter . num * 2 ) )
71
61
} )
72
62
} )
73
63
74
- expect ( scope . _scope . effects . length ) . toBe ( 2 )
75
- expect ( isEffectScope ( scope . _scope . effects [ 1 ] ) ) . toBe ( true )
64
+ expect ( scope . effects . length ) . toBe ( 2 )
65
+ expect ( scope . effects [ 1 ] ) . toBeInstanceOf ( EffectScope )
76
66
77
67
expect ( dummy ) . toBe ( 0 )
78
68
counter . num = 7
79
69
expect ( dummy ) . toBe ( 7 )
80
70
expect ( doubled ) . toBe ( 14 )
81
71
82
72
// stop the nested scope as well
83
- stop ( scope )
73
+ scope . stop ( )
84
74
85
75
counter . num = 6
86
76
expect ( dummy ) . toBe ( 7 )
@@ -91,25 +81,22 @@ describe('reactivity/effect/scope', () => {
91
81
let dummy , doubled
92
82
const counter = reactive ( { num : 0 } )
93
83
94
- const scope = effectScope ( ( ) => {
84
+ const scope = new EffectScope ( ( ) => {
95
85
effect ( ( ) => ( dummy = counter . num ) )
96
86
// nested scope
97
- effectScope (
98
- ( ) => {
99
- effect ( ( ) => ( doubled = counter . num * 2 ) )
100
- } ,
101
- { detached : true }
102
- )
87
+ new EffectScope ( ( ) => {
88
+ effect ( ( ) => ( doubled = counter . num * 2 ) )
89
+ } , true )
103
90
} )
104
91
105
- expect ( scope . _scope . effects . length ) . toBe ( 1 )
92
+ expect ( scope . effects . length ) . toBe ( 1 )
106
93
107
94
expect ( dummy ) . toBe ( 0 )
108
95
counter . num = 7
109
96
expect ( dummy ) . toBe ( 7 )
110
97
expect ( doubled ) . toBe ( 14 )
111
98
112
- stop ( scope )
99
+ scope . stop ( )
113
100
114
101
counter . num = 6
115
102
expect ( dummy ) . toBe ( 7 )
@@ -122,44 +109,46 @@ describe('reactivity/effect/scope', () => {
122
109
let dummy , doubled
123
110
const counter = reactive ( { num : 0 } )
124
111
125
- const scope = effectScope ( ( ) => {
112
+ const scope = new EffectScope ( ( ) => {
126
113
effect ( ( ) => ( dummy = counter . num ) )
127
114
} )
128
115
129
- expect ( scope . _scope . effects . length ) . toBe ( 1 )
116
+ expect ( scope . effects . length ) . toBe ( 1 )
130
117
131
- extendScope ( scope , ( ) => {
118
+ scope . extend ( ( ) => {
132
119
effect ( ( ) => ( doubled = counter . num * 2 ) )
133
120
} )
134
121
135
- expect ( scope . _scope . effects . length ) . toBe ( 2 )
122
+ expect ( scope . effects . length ) . toBe ( 2 )
136
123
137
124
counter . num = 7
138
125
expect ( dummy ) . toBe ( 7 )
139
126
expect ( doubled ) . toBe ( 14 )
140
127
141
- stop ( scope )
128
+ scope . stop ( )
142
129
} )
143
130
144
131
it ( 'can not extend an inactive scope' , ( ) => {
145
132
let dummy , doubled
146
133
const counter = reactive ( { num : 0 } )
147
134
148
- const scope = effectScope ( ( ) => {
135
+ const scope = new EffectScope ( ( ) => {
149
136
effect ( ( ) => ( dummy = counter . num ) )
150
137
} )
151
138
152
- expect ( scope . _scope . effects . length ) . toBe ( 1 )
139
+ expect ( scope . effects . length ) . toBe ( 1 )
153
140
154
- stop ( scope )
141
+ scope . stop ( )
155
142
156
- extendScope ( scope , ( ) => {
143
+ scope . extend ( ( ) => {
157
144
effect ( ( ) => ( doubled = counter . num * 2 ) )
158
145
} )
159
146
160
- expect ( '[Vue warn] can not extend on an inactive scope.' ) . toHaveBeenWarned ( )
147
+ expect (
148
+ '[Vue warn] cannot extend an inactive effect scope.'
149
+ ) . toHaveBeenWarned ( )
161
150
162
- expect ( scope . _scope . effects . length ) . toBe ( 1 )
151
+ expect ( scope . effects . length ) . toBe ( 1 )
163
152
164
153
counter . num = 7
165
154
expect ( dummy ) . toBe ( 0 )
@@ -169,80 +158,36 @@ describe('reactivity/effect/scope', () => {
169
158
it ( 'should fire onStop hook' , ( ) => {
170
159
let dummy = 0
171
160
172
- const scope = effectScope ( onStop => {
161
+ const scope = new EffectScope ( onStop => {
173
162
onStop ( ( ) => ( dummy += 1 ) )
174
163
onStop ( ( ) => ( dummy += 2 ) )
175
164
} )
176
165
177
- extendScope ( scope , onStop => {
166
+ scope . extend ( onStop => {
178
167
onStop ( ( ) => ( dummy += 4 ) )
179
168
} )
180
169
181
170
expect ( dummy ) . toBe ( 0 )
182
171
183
- stop ( scope )
172
+ scope . stop ( )
184
173
expect ( dummy ) . toBe ( 7 )
185
174
} )
186
175
187
176
it ( 'should fire onScopeStopped hook' , ( ) => {
188
177
let dummy = 0
189
178
190
- const scope = effectScope ( ( ) => {
191
- onScopeStopped ( ( ) => ( dummy += 1 ) )
192
- onScopeStopped ( ( ) => ( dummy += 2 ) )
179
+ const scope = new EffectScope ( ( ) => {
180
+ onDispose ( ( ) => ( dummy += 1 ) )
181
+ onDispose ( ( ) => ( dummy += 2 ) )
193
182
} )
194
183
195
- extendScope ( scope , ( ) => {
196
- onScopeStopped ( ( ) => ( dummy += 4 ) )
184
+ scope . extend ( ( ) => {
185
+ onDispose ( ( ) => ( dummy += 4 ) )
197
186
} )
198
187
199
188
expect ( dummy ) . toBe ( 0 )
200
189
201
- stop ( scope )
190
+ scope . stop ( )
202
191
expect ( dummy ) . toBe ( 7 )
203
192
} )
204
-
205
- it ( 'should forward returns' , ( ) => {
206
- let foo = ref ( 1 )
207
-
208
- const scope = effectScope ( ( ) => {
209
- return {
210
- doubled : computed ( ( ) => foo . value * 2 )
211
- }
212
- } )
213
-
214
- const { doubled } = scope
215
-
216
- expect ( doubled . value ) . toBe ( 2 )
217
-
218
- foo . value += 1
219
- expect ( doubled . value ) . toBe ( 4 )
220
-
221
- stop ( scope )
222
-
223
- foo . value += 1
224
- expect ( doubled . value ) . toBe ( 4 )
225
- } )
226
-
227
- it ( 'should forward returns on extending' , ( ) => {
228
- let foo = ref ( 1 )
229
-
230
- const scope = effectScope ( )
231
-
232
- const { tripled } = extendScope ( scope , ( ) => {
233
- return {
234
- tripled : computed ( ( ) => foo . value * 3 )
235
- }
236
- } )
237
-
238
- expect ( tripled . value ) . toBe ( 3 )
239
-
240
- foo . value += 1
241
- expect ( tripled . value ) . toBe ( 6 )
242
-
243
- stop ( scope )
244
-
245
- foo . value += 1
246
- expect ( tripled . value ) . toBe ( 6 )
247
- } )
248
193
} )
0 commit comments