Skip to content

Commit 3129147

Browse files
committed
add test for #17342
1 parent d36462e commit 3129147

File tree

1 file changed

+40
-1
lines changed
  • packages/svelte/tests/signals

1 file changed

+40
-1
lines changed

packages/svelte/tests/signals/test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { proxy } from '../../src/internal/client/proxy';
1616
import { derived } from '../../src/internal/client/reactivity/deriveds';
1717
import { snapshot } from '../../src/internal/shared/clone.js';
1818
import { SvelteSet } from '../../src/reactivity/set';
19-
import { CONNECTED, DESTROYED } from '../../src/internal/client/constants';
19+
import { CLEAN, CONNECTED, DESTROYED, MAYBE_DIRTY } from '../../src/internal/client/constants';
2020
import { noop } from 'svelte/internal/client';
2121
import { disable_async_mode_flag, enable_async_mode_flag } from '../../src/internal/flags';
2222
import { branch } from '../../src/internal/client/reactivity/effects';
@@ -1524,4 +1524,43 @@ describe('signals', () => {
15241524
assert.ok(countReactions.includes(d!), 'derived should be in source reactions');
15251525
};
15261526
});
1527+
1528+
// Test that deriveds with no dependencies are always CLEAN
1529+
test('deriveds with no deps should be CLEAN and not re-evaluate', () => {
1530+
let evalCount = 0;
1531+
let d: Derived<number> | null = null;
1532+
1533+
render_effect(() => {
1534+
branch(() => {
1535+
if (!d) {
1536+
d = derived(() => {
1537+
evalCount++;
1538+
return 42;
1539+
});
1540+
}
1541+
1542+
$.get(d);
1543+
});
1544+
});
1545+
1546+
return () => {
1547+
flushSync();
1548+
1549+
const initialEvalCount = evalCount;
1550+
assert.equal(initialEvalCount, 1, 'derived should evaluate once initially');
1551+
1552+
for (let i = 0; i < 100; i++) {
1553+
$.get(d!);
1554+
}
1555+
1556+
assert.equal(
1557+
evalCount,
1558+
initialEvalCount,
1559+
'derived with no deps should not re-evaluate on subsequent reads'
1560+
);
1561+
1562+
const isClean = (d!.f & CLEAN) !== 0;
1563+
assert.ok(isClean, 'derived with no deps should be CLEAN');
1564+
};
1565+
});
15271566
});

0 commit comments

Comments
 (0)