Skip to content

Commit

Permalink
Adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
divdavem committed Nov 26, 2024
1 parent 15ac44f commit ef4c03d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3098,6 +3098,30 @@ describe('stores', () => {
expect(cHasListeners).toBe(false);
});

it('should not re-subscribe to stores that should no longer be used', () => {
const events: string[] = [];
const a = writable(true);
const b = writable(0, () => {
events.push('b used');
return () => {
events.push('b unused');
};
});
const c = writable(1, () => {
events.push('c used');
return () => {
events.push('c unused');
};
});
const computedFn = vi.fn(() => (a() ? b() : c()));
const d = computed(computedFn);
expect(d()).toBe(0);
events.push('changing a');
a.set(false);
expect(d()).toBe(1);
expect(events).toEqual(['b used', 'b unused', 'changing a', 'c used', 'c unused']);
});

it('should not recompute if an untracked store changed', () => {
const a = writable(1);
const b = writable(2);
Expand Down

0 comments on commit ef4c03d

Please sign in to comment.