forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
computed_spec.ts
188 lines (147 loc) · 4.86 KB
/
computed_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {computed, signal, watch} from '@angular/core/src/signals';
describe('computed', () => {
it('should create computed', () => {
const counter = signal(0);
let computedRunCount = 0;
const double = computed(() => `${counter() * 2}:${++computedRunCount}`);
expect(double()).toEqual('0:1');
counter.set(1);
expect(double()).toEqual('2:2');
expect(double()).toEqual('2:2');
counter.set(2);
expect(double()).toEqual('4:3');
expect(double()).toEqual('4:3');
});
it('should not re-compute if there are no dependencies', () => {
let tick = 0;
const c = computed(() => ++tick);
expect(c()).toEqual(1);
expect(c()).toEqual(1);
});
it('should not re-compute if the dependency is a primitive value and the value did not change',
() => {
const counter = signal(0);
let computedRunCount = 0;
const double = computed(() => `${counter() * 2}:${++computedRunCount}`);
expect(double()).toEqual('0:1');
counter.set(0);
expect(double()).toEqual('0:1');
});
it('should chain computed', () => {
const name = signal('abc');
const reverse = computed(() => name().split('').reverse().join(''));
const upper = computed(() => reverse().toUpperCase());
expect(upper()).toEqual('CBA');
name.set('foo');
expect(upper()).toEqual('OOF');
});
it('should evaluate computed only when subscribing', () => {
const name = signal('John');
const age = signal(25);
const show = signal(true);
let computeCount = 0;
const displayName =
computed(() => `${show() ? `${name()} aged ${age()}` : 'anonymous'}:${++computeCount}`);
expect(displayName()).toEqual('John aged 25:1');
show.set(false);
expect(displayName()).toEqual('anonymous:2');
name.set('Bob');
expect(displayName()).toEqual('anonymous:2');
});
it('should detect simple dependency cycles', () => {
const a: () => unknown = computed(() => a());
expect(() => a()).toThrowError('Detected cycle in computations.');
});
it('should detect deep dependency cycles', () => {
const a: () => unknown = computed(() => b());
const b = computed(() => c());
const c = computed(() => d());
const d = computed(() => a());
expect(() => a()).toThrowError('Detected cycle in computations.');
});
it('should cache exceptions thrown until computed gets dirty again', () => {
const toggle = signal('KO');
const c = computed(() => {
const val = toggle();
if (val === 'KO') {
throw new Error('KO');
} else {
return val;
}
});
expect(() => c()).toThrowError('KO');
expect(() => c()).toThrowError('KO');
toggle.set('OK');
expect(c()).toEqual('OK');
});
it('should not update dependencies of computations when dependencies don\'t change', () => {
const source = signal(0);
const isEven = computed(() => source() % 2 === 0);
let updateCounter = 0;
const updateTracker = computed(() => {
isEven();
return updateCounter++;
});
updateTracker();
expect(updateCounter).toEqual(1);
source.set(1);
updateTracker();
expect(updateCounter).toEqual(2);
// Setting the counter to another odd value should not trigger `updateTracker` to update.
source.set(3);
updateTracker();
expect(updateCounter).toEqual(2);
source.set(4);
updateTracker();
expect(updateCounter).toEqual(3);
});
it('should not mark dirty computed signals that are dirty already', () => {
const source = signal('a');
const derived = computed(() => source().toUpperCase());
let watchCount = 0;
const w = watch(
() => {
derived();
},
() => {
watchCount++;
},
false);
w.run();
expect(watchCount).toEqual(0);
// change signal, mark downstream dependencies dirty
source.set('b');
expect(watchCount).toEqual(1);
// change signal again, downstream dependencies should be dirty already and not marked again
source.set('c');
expect(watchCount).toEqual(1);
// resetting dependencies back to clean
w.run();
expect(watchCount).toEqual(1);
// expecting another notification at this point
source.set('d');
expect(watchCount).toEqual(2);
});
it('should allow signal creation within computed', () => {
const doubleCounter = computed(() => {
const counter = signal(1);
return counter() * 2;
});
expect(doubleCounter()).toBe(2);
});
it('should disallow writing to signals within computed', () => {
const source = signal(0);
const illegal = computed(() => {
source.set(1);
return 0;
});
expect(illegal).toThrow();
});
});