-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #11
- Loading branch information
Mohammad Hasani
committed
Feb 14, 2019
1 parent
d8c85f0
commit 63fb590
Showing
8 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ofType of(a(), b(), c()).pipe(ofType('a')) (type) should match snapshot: ofType of(a(), b(), c()).pipe(ofType('a')) 1`] = `"Observable<{ type: \\"a\\"; } | { type: \\"b\\"; } | { type: \\"c\\"; }>"`; | ||
|
||
exports[`ofType of(a(), b(), c()).pipe(ofType(['a', 'b'])) (type) should match snapshot: ofType of(a(), b(), c()).pipe(ofType(['a', 'b'])) 1`] = `"Observable<{ type: \\"a\\"; } | { type: \\"b\\"; } | { type: \\"c\\"; }>"`; | ||
|
||
exports[`ofType of(a(), b(), c()).pipe(ofType([a(), b()])) (type) should match snapshot: ofType of(a(), b(), c()).pipe(ofType([a(), b()])) 1`] = `"Observable<{ type: \\"a\\"; } | { type: \\"b\\"; }>"`; | ||
|
||
exports[`ofType of(a(), b(), c()).pipe(ofType([a, b])) (type) should match snapshot: ofType of(a(), b(), c()).pipe(ofType([a, b])) 1`] = `"Observable<{ type: \\"a\\"; } | { type: \\"b\\"; }>"`; | ||
|
||
exports[`ofType of(a(), b(), c()).pipe(ofType(a())) (type) should match snapshot: ofType of(a(), b(), c()).pipe(ofType(a())) 1`] = `"Observable<{ type: \\"a\\"; }>"`; | ||
|
||
exports[`ofType of(a(), b(), c()).pipe(ofType(a)) (type) should match snapshot: ofType of(a(), b(), c()).pipe(ofType(a)) 1`] = `"Observable<{ type: \\"a\\"; }>"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { of } from 'rxjs' | ||
|
||
import { createAction } from '../create-action' | ||
import { ofType } from '../of-type' | ||
|
||
// @dts-jest:group ofType | ||
|
||
const a = createAction('a') | ||
const b = createAction('b') | ||
const c = createAction('c') | ||
|
||
// @dts-jest:pass:snap | ||
of(a(), b(), c()).pipe(ofType(a)) | ||
|
||
// @dts-jest:pass:snap | ||
of(a(), b(), c()).pipe(ofType([a, b])) | ||
|
||
// @dts-jest:pass:snap | ||
of(a(), b(), c()).pipe(ofType(a())) | ||
|
||
// @dts-jest:pass:snap | ||
of(a(), b(), c()).pipe(ofType([a(), b()])) | ||
|
||
// @dts-jest:pass:snap | ||
of(a(), b(), c()).pipe(ofType('a')) | ||
|
||
// @dts-jest:pass:snap | ||
of(a(), b(), c()).pipe(ofType(['a', 'b'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { marbles } from 'rxjs-marbles/jest' | ||
|
||
import { createAction } from '../create-action' | ||
import { ofType } from '../of-type' | ||
|
||
describe('ofType', () => { | ||
const a = createAction('a') | ||
const b = createAction('b') | ||
const c = createAction('c') | ||
const d = createAction('d') | ||
const e = createAction('e') | ||
const f = createAction('f') | ||
const g = createAction('g') | ||
const h = createAction('h') | ||
|
||
const values = { | ||
a: a(), | ||
b: b(), | ||
c: c(), | ||
d: d(), | ||
e: e(), | ||
f: f(), | ||
g: g(), | ||
h: h(), | ||
} | ||
|
||
const test = ({ action, subs, expected, keys }: any) => | ||
marbles(m => { | ||
const action$ = m.hot(action, values) | ||
const expected$ = m.cold(expected, values) | ||
|
||
m.expect(action$.pipe(ofType(keys))).toBeObservable(expected$) | ||
m.expect(action$).toHaveSubscriptions(subs) | ||
})() | ||
|
||
it.each([ | ||
{ | ||
action: ' -a--b-^-c--d--e--f--c--h--|', | ||
subs: ' ^-------------------!', | ||
expected: ' --c-----------c-----|', | ||
keys: c, | ||
}, | ||
{ | ||
action: ' -a--b-^-c--d--e--f--c--h--|', | ||
subs: ' ^-------------------!', | ||
expected: ' --c--d-----f--c-----|', | ||
keys: [c, d, f], | ||
}, | ||
])('should filter in with action creator(s)', test) | ||
|
||
it.each([ | ||
{ | ||
action: ' -a--b-^-c--d--e--f--c--h--|', | ||
subs: ' ^-------------------!', | ||
expected: ' --c-----------c-----|', | ||
keys: c(), | ||
}, | ||
{ | ||
action: ' -a--b-^-c--d--e--f--c--h--|', | ||
subs: ' ^-------------------!', | ||
expected: ' --c--d-----f--c-----|', | ||
keys: [c(), d(), f()], | ||
}, | ||
])('should filter in with action(s)', test) | ||
|
||
it.each([ | ||
{ | ||
action: ' -a--b-^-c--d--e--f--c--h--|', | ||
subs: ' ^-------------------!', | ||
expected: ' --c-----------c-----|', | ||
keys: 'c', | ||
}, | ||
{ | ||
action: ' -a--b-^-c--d--e--f--c--h--|', | ||
subs: ' ^-------------------!', | ||
expected: ' --c--d-----f--c-----|', | ||
keys: ['c', 'd', 'f'], | ||
}, | ||
])('should filter in with action type(s)', test) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { filter } from 'rxjs/operators' | ||
|
||
import { ActionCreator } from './create-action' | ||
import { AnyAction } from './action' | ||
import { getType } from './get-type' | ||
|
||
/** | ||
* Filter actions emitted by the source Observable by only emitting those that | ||
* are compatible with specified action(s) or action creator(s) or action type(s). | ||
* | ||
* @example | ||
* action$.pipe( | ||
* ofType(foo), | ||
* ... | ||
* ) | ||
* @example | ||
* action$.pipe( | ||
* ofType([foo, bar]), | ||
* ... | ||
* ) | ||
*/ | ||
export function ofType< | ||
Source extends AnyAction, | ||
Key extends ActionCreator<Source> | Source | Source['type'], | ||
Sink extends Source = Key extends (...args: any[]) => infer U | ||
? (U extends Source ? U : never) | ||
: (Key extends Source ? Key : never) | ||
>(keys: Key | Key[]) { | ||
const types: string[] = (Array.isArray(keys) ? keys : [keys]).map(key => | ||
typeof key === 'string' ? key : getType(key) | ||
) | ||
|
||
return filter<Source, Sink>( | ||
(action): action is Sink => types.includes(action.type) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters