Skip to content

Commit

Permalink
test: adding new tests and improving the reliability of an existing t…
Browse files Browse the repository at this point in the history
…est (#135)
  • Loading branch information
divdavem authored Jun 14, 2024
1 parent fcae275 commit debafe4
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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!!
Expand All @@ -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}`);
});
Expand Down

0 comments on commit debafe4

Please sign in to comment.