@@ -4,26 +4,27 @@ import {
4
4
effect ,
5
5
// stop,
6
6
ref ,
7
- WritableComputedRef ,
7
+ watchEffect ,
8
8
} from '../../../src' ;
9
9
import { mockWarn } from '../../helpers/mockWarn' ;
10
- import { waitForUpdate } from '../../helpers/utils' ;
10
+ import '../../helpers/wait-for-update' ;
11
+ import { nextTick } from '../../helpers/utils' ;
11
12
12
13
describe ( 'reactivity/computed' , ( ) => {
13
14
mockWarn ( ) ;
14
15
15
- it ( 'should return updated value' , done => {
16
- const value = reactive < { foo ?: number } > ( { } ) ;
16
+ it ( 'should return updated value' , async ( ) => {
17
+ const value = reactive < { foo ?: number } > ( { foo : undefined } ) ;
17
18
const cValue = computed ( ( ) => value . foo ) ;
18
19
expect ( cValue . value ) . toBe ( undefined ) ;
19
20
value . foo = 1 ;
20
- waitForUpdate ( ( ) => {
21
- expect ( cValue . value ) . toBe ( 1 ) ;
22
- } ) . then ( done ) ;
21
+ await nextTick ( ) ;
22
+
23
+ expect ( cValue . value ) . toBe ( 1 ) ;
23
24
} ) ;
24
25
25
26
it ( 'should compute lazily' , ( ) => {
26
- const value = reactive < { foo ?: number } > ( { } ) ;
27
+ const value = reactive < { foo ?: number } > ( { foo : undefined } ) ;
27
28
const getter = jest . fn ( ( ) => value . foo ) ;
28
29
const cValue = computed ( getter ) ;
29
30
@@ -51,7 +52,7 @@ describe('reactivity/computed', () => {
51
52
} ) ;
52
53
53
54
it ( 'should trigger effect' , ( ) => {
54
- const value = reactive < { foo ?: number } > ( { } ) ;
55
+ const value = reactive < { foo ?: number } > ( { foo : undefined } ) ;
55
56
const cValue = computed ( ( ) => value . foo ) ;
56
57
let dummy ;
57
58
effect ( ( ) => {
@@ -96,7 +97,7 @@ describe('reactivity/computed', () => {
96
97
expect ( getter2 ) . toHaveBeenCalledTimes ( 2 ) ;
97
98
} ) ;
98
99
99
- it ( 'should trigger effect when chained (mixed invocations)' , ( ) => {
100
+ it ( 'should trigger effect when chained (mixed invocations)' , async ( ) => {
100
101
const value = reactive ( { foo : 0 } ) ;
101
102
const getter1 = jest . fn ( ( ) => value . foo ) ;
102
103
const getter2 = jest . fn ( ( ) => {
@@ -106,14 +107,18 @@ describe('reactivity/computed', () => {
106
107
const c2 = computed ( getter2 ) ;
107
108
108
109
let dummy ;
109
- effect ( ( ) => {
110
+ watchEffect ( ( ) => {
110
111
dummy = c1 . value + c2 . value ;
111
112
} ) ;
113
+ await nextTick ( ) ;
112
114
expect ( dummy ) . toBe ( 1 ) ;
113
115
114
116
expect ( getter1 ) . toHaveBeenCalledTimes ( 1 ) ;
115
117
expect ( getter2 ) . toHaveBeenCalledTimes ( 1 ) ;
116
118
value . foo ++ ;
119
+
120
+ await nextTick ( ) ;
121
+
117
122
expect ( dummy ) . toBe ( 3 ) ;
118
123
// should not result in duplicate calls
119
124
expect ( getter1 ) . toHaveBeenCalledTimes ( 2 ) ;
@@ -171,11 +176,12 @@ describe('reactivity/computed', () => {
171
176
expect ( dummy ) . toBe ( - 1 ) ;
172
177
} ) ;
173
178
174
- it ( 'should warn if trying to set a readonly computed' , ( ) => {
175
- const n = ref ( 1 ) ;
176
- const plusOne = computed ( ( ) => n . value + 1 ) ;
177
- ( plusOne as WritableComputedRef < number > ) . value ++ ; // Type cast to prevent TS from preventing the error
179
+ // it('should warn if trying to set a readonly computed', async () => {
180
+ // const n = ref(1);
181
+ // const plusOne = computed(() => n.value + 1);
182
+ // (plusOne as WritableComputedRef<number>).value++; // Type cast to prevent TS from preventing the error
183
+ // await nextTick();
178
184
179
- expect ( 'Write operation failed: computed value is readonly' ) . toHaveBeenWarnedLast ( ) ;
180
- } ) ;
185
+ // expect('Write operation failed: computed value is readonly').toHaveBeenWarnedLast();
186
+ // });
181
187
} ) ;
0 commit comments