Skip to content

Commit

Permalink
Merge pull request #232 from effector/fix-and-or-simple-store
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova authored Aug 29, 2022
2 parents ea6d54b + c89ca76 commit 7d3ada4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/and/and.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ test('When at least one store has falsy value result must be false', async () =>
`);
expect(scope.getState($result)).toBe(false);
});

test('Returns boolean value for single store', async () => {
const $storeOne = createStore('hello');

const $andIncorrect = and($storeOne);

const scope = fork();

expect(scope.getState($andIncorrect)).toBe(true);
});
5 changes: 4 additions & 1 deletion src/and/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { combine, Store } from 'effector';

export function and(...stores: Array<Store<any>>): Store<boolean> {
return combine(stores, (values) =>
values.reduce((all, current) => Boolean(all) && Boolean(current)),
values.reduce(
(all, current) => Boolean(all) && Boolean(current),
Boolean(values[0]),
),
) as Store<boolean>;
}
5 changes: 4 additions & 1 deletion src/or/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { combine, Store } from 'effector';

export function or(...stores: Array<Store<any>>): Store<boolean> {
return combine(stores, (values) =>
values.reduce((all, current) => Boolean(all) || Boolean(current)),
values.reduce(
(all, current) => Boolean(all) || Boolean(current),
Boolean(values[0]),
),
) as Store<boolean>;
}
10 changes: 10 additions & 0 deletions src/or/or.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ it('should be true when at least one store has truthy value', async () => {
`);
expect(scope.getState($result)).toBe(true);
});

test('Returns boolean value for single store', async () => {
const $storeOne = createStore('hello');

const $andIncorrect = or($storeOne);

const scope = fork();

expect(scope.getState($andIncorrect)).toBe(true);
});

0 comments on commit 7d3ada4

Please sign in to comment.