1
- import { computed , deferredComputed , effect , ref } from '../src'
1
+ import { computed , effect , ref } from '../src'
2
2
3
3
describe ( 'deferred computed' , ( ) => {
4
- const tick = Promise . resolve ( )
5
-
6
- test ( 'should only trigger once on multiple mutations' , async ( ) => {
4
+ test ( 'should not trigger if value did not change' , ( ) => {
7
5
const src = ref ( 0 )
8
- const c = deferredComputed ( ( ) => src . value )
6
+ const c = computed ( ( ) => src . value % 2 )
9
7
const spy = vi . fn ( )
10
8
effect ( ( ) => {
11
9
spy ( c . value )
12
10
} )
13
11
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
14
- src . value = 1
15
12
src . value = 2
16
- src . value = 3
17
- // not called yet
18
- expect ( spy ) . toHaveBeenCalledTimes ( 1 )
19
- await tick
20
- // should only trigger once
21
- expect ( spy ) . toHaveBeenCalledTimes ( 2 )
22
- expect ( spy ) . toHaveBeenCalledWith ( c . value )
23
- } )
24
13
25
- test ( 'should not trigger if value did not change' , async ( ) => {
26
- const src = ref ( 0 )
27
- const c = deferredComputed ( ( ) => src . value % 2 )
28
- const spy = vi . fn ( )
29
- effect ( ( ) => {
30
- spy ( c . value )
31
- } )
32
- expect ( spy ) . toHaveBeenCalledTimes ( 1 )
33
- src . value = 1
34
- src . value = 2
35
-
36
- await tick
37
14
// should not trigger
38
15
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
39
16
40
17
src . value = 3
41
- src . value = 4
42
18
src . value = 5
43
- await tick
44
19
// should trigger because latest value changes
45
20
expect ( spy ) . toHaveBeenCalledTimes ( 2 )
46
21
} )
47
22
48
- test ( 'chained computed trigger' , async ( ) => {
23
+ test ( 'chained computed trigger' , ( ) => {
49
24
const effectSpy = vi . fn ( )
50
25
const c1Spy = vi . fn ( )
51
26
const c2Spy = vi . fn ( )
52
27
53
28
const src = ref ( 0 )
54
- const c1 = deferredComputed ( ( ) => {
29
+ const c1 = computed ( ( ) => {
55
30
c1Spy ( )
56
31
return src . value % 2
57
32
} )
@@ -69,19 +44,18 @@ describe('deferred computed', () => {
69
44
expect ( effectSpy ) . toHaveBeenCalledTimes ( 1 )
70
45
71
46
src . value = 1
72
- await tick
73
47
expect ( c1Spy ) . toHaveBeenCalledTimes ( 2 )
74
48
expect ( c2Spy ) . toHaveBeenCalledTimes ( 2 )
75
49
expect ( effectSpy ) . toHaveBeenCalledTimes ( 2 )
76
50
} )
77
51
78
- test ( 'chained computed avoid re-compute' , async ( ) => {
52
+ test ( 'chained computed avoid re-compute' , ( ) => {
79
53
const effectSpy = vi . fn ( )
80
54
const c1Spy = vi . fn ( )
81
55
const c2Spy = vi . fn ( )
82
56
83
57
const src = ref ( 0 )
84
- const c1 = deferredComputed ( ( ) => {
58
+ const c1 = computed ( ( ) => {
85
59
c1Spy ( )
86
60
return src . value % 2
87
61
} )
@@ -98,26 +72,24 @@ describe('deferred computed', () => {
98
72
src . value = 2
99
73
src . value = 4
100
74
src . value = 6
101
- await tick
102
- // c1 should re-compute once.
103
- expect ( c1Spy ) . toHaveBeenCalledTimes ( 2 )
75
+ expect ( c1Spy ) . toHaveBeenCalledTimes ( 4 )
104
76
// c2 should not have to re-compute because c1 did not change.
105
77
expect ( c2Spy ) . toHaveBeenCalledTimes ( 1 )
106
78
// effect should not trigger because c2 did not change.
107
79
expect ( effectSpy ) . toHaveBeenCalledTimes ( 1 )
108
80
} )
109
81
110
- test ( 'chained computed value invalidation' , async ( ) => {
82
+ test ( 'chained computed value invalidation' , ( ) => {
111
83
const effectSpy = vi . fn ( )
112
84
const c1Spy = vi . fn ( )
113
85
const c2Spy = vi . fn ( )
114
86
115
87
const src = ref ( 0 )
116
- const c1 = deferredComputed ( ( ) => {
88
+ const c1 = computed ( ( ) => {
117
89
c1Spy ( )
118
90
return src . value % 2
119
91
} )
120
- const c2 = deferredComputed ( ( ) => {
92
+ const c2 = computed ( ( ) => {
121
93
c2Spy ( )
122
94
return c1 . value + 1
123
95
} )
@@ -139,17 +111,17 @@ describe('deferred computed', () => {
139
111
expect ( c2Spy ) . toHaveBeenCalledTimes ( 2 )
140
112
} )
141
113
142
- test ( 'sync access of invalidated chained computed should not prevent final effect from running' , async ( ) => {
114
+ test ( 'sync access of invalidated chained computed should not prevent final effect from running' , ( ) => {
143
115
const effectSpy = vi . fn ( )
144
116
const c1Spy = vi . fn ( )
145
117
const c2Spy = vi . fn ( )
146
118
147
119
const src = ref ( 0 )
148
- const c1 = deferredComputed ( ( ) => {
120
+ const c1 = computed ( ( ) => {
149
121
c1Spy ( )
150
122
return src . value % 2
151
123
} )
152
- const c2 = deferredComputed ( ( ) => {
124
+ const c2 = computed ( ( ) => {
153
125
c2Spy ( )
154
126
return c1 . value + 1
155
127
} )
@@ -162,14 +134,13 @@ describe('deferred computed', () => {
162
134
src . value = 1
163
135
// sync access c2
164
136
c2 . value
165
- await tick
166
137
expect ( effectSpy ) . toHaveBeenCalledTimes ( 2 )
167
138
} )
168
139
169
- test ( 'should not compute if deactivated before scheduler is called' , async ( ) => {
140
+ test ( 'should not compute if deactivated before scheduler is called' , ( ) => {
170
141
const c1Spy = vi . fn ( )
171
142
const src = ref ( 0 )
172
- const c1 = deferredComputed ( ( ) => {
143
+ const c1 = computed ( ( ) => {
173
144
c1Spy ( )
174
145
return src . value % 2
175
146
} )
@@ -179,7 +150,6 @@ describe('deferred computed', () => {
179
150
c1 . effect . stop ( )
180
151
// trigger
181
152
src . value ++
182
- await tick
183
153
expect ( c1Spy ) . toHaveBeenCalledTimes ( 1 )
184
154
} )
185
155
} )
0 commit comments