diff --git a/src/index.spec.ts b/src/index.spec.ts index 2778671..833ec75 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -144,6 +144,23 @@ describe('stores', () => { ]); }); + it('should work to use subscribe only', () => { + const store1 = writable(0); + const store2 = asReadable({ subscribe: store1.subscribe }); + expect(store2()).toBe(0); + store1.set(1); + expect(store2()).toBe(1); + }); + + it('should work to use subscribe only and use it in a computed', () => { + const store1 = writable(0); + const store2 = asReadable({ subscribe: store1.subscribe }); + const store3 = computed(() => store2()); + expect(store3()).toBe(0); + store1.set(1); + expect(store3()).toBe(1); + }); + it('should allow overriding notEqual', () => { const notEqualCalls: [number, number][] = []; class ModuloStore extends Store { @@ -765,6 +782,34 @@ describe('stores', () => { expect(callCounter).toBe(0); }); + it('should call onUse when reading value and using it in computed', () => { + const calls: (-1 | 1)[] = []; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const readableWithSet = readable(0, (set) => { + calls.push(1); + return () => { + calls.push(-1); + }; + }); + const dependingStore = computed(() => readableWithSet()); + + expect(calls).toEqual([]); + expect(readableWithSet()).toBe(0); + expect(calls).toEqual([1, -1]); + expect(dependingStore()).toBe(0); + expect(calls).toEqual([1, -1, 1, -1]); + expect(dependingStore()).toBe(0); + expect(calls).toEqual([1, -1, 1, -1, 1, -1]); + const unsubscribe = dependingStore.subscribe(() => {}); + expect(calls).toEqual([1, -1, 1, -1, 1, -1, 1]); + const unsubscribe2 = dependingStore.subscribe(() => {}); + unsubscribe(); + expect(dependingStore()).toBe(0); + expect(calls).toEqual([1, -1, 1, -1, 1, -1, 1]); + unsubscribe2(); + expect(calls).toEqual([1, -1, 1, -1, 1, -1, 1, -1]); + }); + it('should give access to both set and update function in the second argument of the readable shorthand', () => { const store = readable(-1, ({ set, update }) => { set(0); @@ -1671,8 +1716,19 @@ describe('stores', () => { [visible$, transitioning$], ([visible, transitioning]) => !visible && !transitioning ); + const visibleChange = derived([currentlyVisible$, visible$], { + equal: () => true, + derive([currentlyVisible, visible]) { + if (currentlyVisible !== visible) { + batch(() => { + transitioning$.set(true); + currentlyVisible$.set(visible); + }); + } + }, + }); const state$ = derived( - [visible$, transitioning$, hidden$], + [visible$, transitioning$, hidden$, visibleChange], ([visible, transitioning, hidden]) => { if (hidden !== (!visible && !transitioning)) { // inconsistent state!! @@ -1681,17 +1737,6 @@ describe('stores', () => { return `visible=${visible},transitioning=${transitioning},hidden=${hidden}`; } ); - const visibleChange = derived( - [currentlyVisible$, visible$], - ([currentlyVisible, visible]) => { - if (currentlyVisible !== visible) { - batch(() => { - transitioning$.set(true); - currentlyVisible$.set(visible); - }); - } - } - ); const unsubscribeState = state$.subscribe((state) => { events.push(`state:${state}`); });