Skip to content

Commit b731fef

Browse files
committed
up
1 parent af7c21b commit b731fef

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tests/derived.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ describe("derived", () => {
4545
expectSpy(spyDerived, 3, { result: 12 });
4646
});
4747

48+
test("derived should not update even if the effect updates", async () => {
49+
const state = reactive({ a: 1, b: 2 });
50+
const spyDerived = jest.fn(() => state.a);
51+
const d = derived(spyDerived);
52+
const spyEffect = jest.fn(() => state.b + d());
53+
effect(spyEffect);
54+
expectSpy(spyEffect, 1);
55+
expectSpy(spyDerived, 1, { result: 1 });
56+
// change unrelated state
57+
state.b = 3;
58+
await waitScheduler();
59+
expectSpy(spyEffect, 2);
60+
expectSpy(spyDerived, 1, { result: 1 });
61+
});
62+
4863
test("derived does not update when unrelated property changes, but updates when dependencies change", async () => {
4964
const state = reactive({ a: 1, b: 2, c: 3 });
5065
const spyDerived = jest.fn(() => state.a + state.b);
@@ -231,4 +246,33 @@ describe("nested derived", () => {
231246
expectSpy(spyDerived1, 2, { result: 5 });
232247
expectSpy(spyDerived2, 2, { result: 10 });
233248
});
249+
test("nested derived should not recompute if none of its sources changed", async () => {
250+
/**
251+
* s1
252+
* ↓
253+
* d1 = s1 * 0
254+
* ↓
255+
* d2 = d1
256+
* ↓
257+
* e1
258+
*
259+
* change s1
260+
* -> d1 should recomputes but d2 should not
261+
*/
262+
const state = reactive({ a: 1 });
263+
const spyDerived1 = jest.fn(() => state.a);
264+
const d1 = derived(spyDerived1);
265+
const spyDerived2 = jest.fn(() => d1() * 0);
266+
const d2 = derived(spyDerived2);
267+
const spyEffect = jest.fn(() => d2());
268+
effect(spyEffect);
269+
expectSpy(spyEffect, 1);
270+
expectSpy(spyDerived1, 1, { result: 1 });
271+
expectSpy(spyDerived2, 1, { result: 0 });
272+
state.a = 3;
273+
await waitScheduler();
274+
expectSpy(spyEffect, 2);
275+
expectSpy(spyDerived1, 2, { result: 3 });
276+
expectSpy(spyDerived2, 2, { result: 0 });
277+
});
234278
});

0 commit comments

Comments
 (0)