Skip to content

Commit

Permalink
Make empty operator work with full-void stores
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Oct 12, 2023
1 parent d0a5233 commit 265bd07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/empty/empty.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,31 @@ test('numbers', async () => {
await allSettled(increment, { scope });
expect(scope.getState($result)).toBe(false);
});

test('strings', async () => {
const set = createEvent<string>();
const $str = createStore<string>('').on(set, (_, str) => str);
const $empty = empty($str);

const scope = fork();

expect(scope.getState($empty)).toBe(false);

allSettled(set, { scope, params: 'hello' });

expect(scope.getState($empty)).toBe(false);
})

test('void', async () => {
const set = createEvent<any>();
const $str = createStore<null | undefined>(null, {skipVoid: false}).on(set, (_, str) => str);
const $empty = empty($str);

const scope = fork();

expect(scope.getState($empty)).toBe(true);

allSettled(set, { scope, params: undefined });

expect(scope.getState($empty)).toBe(true);
});
4 changes: 2 additions & 2 deletions src/empty/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store } from 'effector';

export function empty<A>(source: Store<A | null>): Store<boolean> {
return source.map((value) => value === null);
export function empty<A>(source: Store<A | null | undefined>): Store<boolean> {
return source.map((value) => value == null);
}

0 comments on commit 265bd07

Please sign in to comment.