Skip to content

Commit

Permalink
feat(core): ✅ handle async conditions in and operator
Browse files Browse the repository at this point in the history
  • Loading branch information
yjaaidi committed Apr 22, 2020
1 parent 63a32e9 commit d0574a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
36 changes: 34 additions & 2 deletions libs/core/src/lib/matchers/operators/and.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readAll } from '@nrwl/angular/testing';
import { of } from 'rxjs';
import { createRequest, RequestArgs } from '../../request';
import { RequestCondition } from '../../plugin';
import { matchOrigin, matchMethod } from '..';
Expand Down Expand Up @@ -60,8 +62,38 @@ describe('operator: and', () => {
expected: false,
},
],
])('should %s', (name, { requestArgs, operatorArgs, expected }) => {
[
'match with async conditions',
{
requestArgs: {
url: 'https://test.com',
method: 'GET',
},
operatorArgs: [
(...args) => of(true),
(...args) => Promise.resolve(true),
],
expected: true,
},
],
[
'mismatch with async conditions',
{
requestArgs: {
url: 'https://test.com',
method: 'GET',
},
operatorArgs: [
(...args) => of(true),
(...args) => Promise.resolve(false),
],
expected: false,
},
],
])('should %s', async (name, { requestArgs, operatorArgs, expected }) => {
const request = createRequest({ ...requestArgs });
expect(and(...operatorArgs)({ request })).toBe(expected);
expect(await readAll(and(...operatorArgs)({ request }))).toEqual([
expected,
]);
});
});
15 changes: 12 additions & 3 deletions libs/core/src/lib/matchers/operators/and.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { combineLatest } from 'rxjs';
import { map, take } from 'rxjs/operators';
import { RequestCondition } from '../../plugin';
import { fromSyncOrAsync } from '../../utils/from-sync-or-async';

export const and = (...predicates: RequestCondition[]): RequestCondition => (
request
) => predicates.every((predicate) => predicate(request));
export const and = (...predicates: RequestCondition[]) => ({ request }) => {
const observableList = predicates.map((predicate) =>
fromSyncOrAsync(predicate({ request }))
);
return combineLatest(observableList).pipe(
take(1),
map((resultList) => resultList.every((value) => value === true))
);
};

0 comments on commit d0574a0

Please sign in to comment.