diff --git a/examples/namespace.tests.ts b/examples/namespace.tests.ts index 8991ffe3..ccfa5905 100644 --- a/examples/namespace.tests.ts +++ b/examples/namespace.tests.ts @@ -5,15 +5,9 @@ import { actionCreator, namespaceActionCreator, namespaceActionDispatcher, - ActionWithPayload, } from 'rxbeach'; import { withNamespace } from 'rxbeach/operators'; -import { AnyAction, actionWithPayload } from 'rxbeach/internal'; - -const sumOp = reduce( - (a, b) => a + ((b as ActionWithPayload).payload || 0), - 0 -); +import { AnyAction, mockAction } from 'rxbeach/internal'; export default function namespaceExamples() { describe('namespaces', function() { @@ -21,41 +15,94 @@ export default function namespaceExamples() { const namespaceA = 'A'; const namespaceB = 'B'; - it('can namespace the action creators', async function() { + describe('namespacing action creators', function() { const testActionA = namespaceActionCreator(namespaceA, testAction); const testActionB = namespaceActionCreator(namespaceB, testAction); + const actionObjectA = testActionA(1); + const actionObjectB = testActionB(2); + + let lastActionNamespaceA: AnyAction | undefined; + let lastActionNamespaceB: AnyAction | undefined; + let sumAllNamespaces: number | undefined; + this.afterEach(async function() { + lastActionNamespaceA = undefined; + lastActionNamespaceB = undefined; + sumAllNamespaces = undefined; + }); - const action$ = of(testActionA(1), testActionB(2)); + this.beforeEach(async function() { + const action$ = of(actionObjectA, actionObjectB); + const actionA$ = action$.pipe(withNamespace(namespaceA)); + const actionB$ = action$.pipe(withNamespace(namespaceB)); + const sum$ = action$.pipe(reduce((a, b) => a + (b.payload || 0), 0)); - const a = await action$.pipe(withNamespace(namespaceA)).toPromise(); - const b = await action$.pipe(withNamespace(namespaceB)).toPromise(); - const sum = await action$.pipe(sumOp).toPromise(); + lastActionNamespaceA = await actionA$.toPromise(); + lastActionNamespaceB = await actionB$.toPromise(); + sumAllNamespaces = await sum$.toPromise(); + }); - deepEqual(a, actionWithPayload(testAction.type, 1, namespaceA)); - deepEqual(b, actionWithPayload(testAction.type, 2, namespaceB)); - equal(sum, 3); + it('can filter namespace A', async function() { + equal(lastActionNamespaceA, actionObjectA); + }); + it('can filter namespace B', async function() { + equal(lastActionNamespaceB, actionObjectB); + }); + it('dispatches to main action$', async function() { + equal(sumAllNamespaces, 3); + }); }); - it('can namespace dispatchAction', async function() { - const action$ = new Subject(); - const dispatchAction = action$.next.bind(action$); + describe('namespacing action dispatchers', function() { + let lastActionNamespaceA: AnyAction | undefined; + let lastActionNamespaceB: AnyAction | undefined; + let sumAllNamespaces: number | undefined; + this.afterEach(function() { + lastActionNamespaceA = undefined; + lastActionNamespaceB = undefined; + sumAllNamespaces = undefined; + }); + + this.beforeEach(async function() { + const action$ = new Subject(); + const dispatchAction = action$.next.bind(action$); + + const dispatchA = namespaceActionDispatcher(namespaceA, dispatchAction); + const dispatchB = namespaceActionDispatcher(namespaceB, dispatchAction); + + const a_p = action$.pipe(withNamespace(namespaceA)).toPromise(); + const b_p = action$.pipe(withNamespace(namespaceB)).toPromise(); + const sum_p = action$ + .pipe(reduce((a: any, b: any) => a + (b.payload || 0), 0)) + .toPromise(); + + dispatchA(testAction(1)); + dispatchB(testAction(2)); + action$.complete(); - const dispatchA = namespaceActionDispatcher(namespaceA, dispatchAction); - const dispatchB = namespaceActionDispatcher(namespaceB, dispatchAction); + const [a, b, sum] = await Promise.all([a_p, b_p, sum_p]); - const a_p = action$.pipe(withNamespace(namespaceA)).toPromise(); - const b_p = action$.pipe(withNamespace(namespaceB)).toPromise(); - const sum_p = action$.pipe(sumOp).toPromise(); + lastActionNamespaceA = a; + lastActionNamespaceB = b; + sumAllNamespaces = sum; + }); - dispatchA(testAction(1)); - dispatchB(testAction(2)); - action$.complete(); + it('applies namespace A', function() { + deepEqual( + lastActionNamespaceA, + mockAction(testAction.type, namespaceA, 1) + ); + }); - const [a, b, sum] = await Promise.all([a_p, b_p, sum_p]); + it('applies namespace B', function() { + deepEqual( + lastActionNamespaceB, + mockAction(testAction.type, namespaceB, 2) + ); + }); - deepEqual(a, actionWithPayload(testAction.type, 1, namespaceA)); - deepEqual(b, actionWithPayload(testAction.type, 2, namespaceB)); - equal(sum, 3); + it('dispatches to root action$', function() { + equal(sumAllNamespaces, 3); + }); }); }); } diff --git a/src/internal/index.ts b/src/internal/index.ts index 20abdc8c..8a506433 100644 --- a/src/internal/index.ts +++ b/src/internal/index.ts @@ -1,4 +1,4 @@ -export { actionWithoutPayload, actionWithPayload } from './testUtils'; +export { mockAction } from './testUtils'; export { VoidPayload, AnyAction, diff --git a/src/internal/testUtils.ts b/src/internal/testUtils.ts index 50d9c17a..3fff6802 100644 --- a/src/internal/testUtils.ts +++ b/src/internal/testUtils.ts @@ -1,18 +1,13 @@ -import { ActionWithPayload, ActionWithoutPayload } from 'types/Action'; +import { Action } from 'rxbeach'; +import { VoidPayload } from 'rxbeach/internal'; -export const actionWithoutPayload = ( +export const mockAction =

( type: string, - namespace?: string -): ActionWithoutPayload => ({ - meta: { namespace }, - type, -}); - -export const actionWithPayload =

( - type: string, - payload: P, - namespace?: string -): ActionWithPayload

=> ({ - ...actionWithoutPayload(type, namespace), - payload, -}); + namespace?: string, + payload?: P +): Action

=> + ({ + meta: { namespace }, + type, + payload, + } as Action

); diff --git a/src/namespace.tests.ts b/src/namespace.tests.ts index aec6dff3..8e91090e 100644 --- a/src/namespace.tests.ts +++ b/src/namespace.tests.ts @@ -4,51 +4,47 @@ import { ActionDispatcher, namespaceActionDispatcher, } from 'rxbeach'; -import { - actionWithPayload, - AnyAction, - actionWithoutPayload, -} from 'rxbeach/internal'; +import { mockAction, AnyAction } from 'rxbeach/internal'; describe('namespace', function() { describe('namespaceActionCreator', function() { - it('Should create actions with namespace', function() { - const type = 'action type'; - const namespace = 'new namespace'; - const actionCreator = (payload: number) => - actionWithPayload(type, payload, 'old namespace'); - actionCreator.type = type; + const type = 'action type'; + const namespace = 'new namespace'; + const actionCreator = (payload: number) => + mockAction(type, 'old namespace', payload); + actionCreator.type = type; - const namespacedActionCreator = namespaceActionCreator( - namespace, - actionCreator - ); + const namespacedActionCreator = namespaceActionCreator( + namespace, + actionCreator + ); - const action = namespacedActionCreator(12); + const actionObject = namespacedActionCreator(12); - deepEqual(action, actionWithPayload(type, 12, namespace)); + it('Should create actions with namespace', function() { + deepEqual(actionObject, mockAction(type, namespace, 12)); }); }); describe('namespaceActionDispatcher', function() { - it('Should invoke the parent dispatcher with namespaced actions', function() { - let dispatchedAction: AnyAction | undefined; - const parentDispatcher: ActionDispatcher = action => - (dispatchedAction = action); + let dispatchedAction: AnyAction | undefined; + const parentDispatcher: ActionDispatcher = action => + (dispatchedAction = action); - const namespace = 'new namespace'; - const childDispatcher = namespaceActionDispatcher( - namespace, - parentDispatcher - ); + const namespace = 'new namespace'; + const childDispatcher = namespaceActionDispatcher( + namespace, + parentDispatcher + ); - const action = actionWithoutPayload('action', 'old namespace'); + const actionObject = mockAction('action', 'old namespace'); - childDispatcher(action); + childDispatcher(actionObject); + it('Should invoke the parent dispatcher with namespaced actions', function() { deepEqual(dispatchedAction, { payload: undefined, - ...actionWithoutPayload(action.type, namespace), + ...mockAction(actionObject.type, namespace), }); }); }); diff --git a/src/operators/operators.tests.ts b/src/operators/operators.tests.ts index 1b361f02..4f5fac8b 100644 --- a/src/operators/operators.tests.ts +++ b/src/operators/operators.tests.ts @@ -3,14 +3,14 @@ import { of, OperatorFunction } from 'rxjs'; import { reduce } from 'rxjs/operators'; import { ActionWithPayload, ActionWithoutPayload } from 'rxbeach'; import { extractPayload, ofType } from 'rxbeach/operators'; -import { actionWithPayload, actionWithoutPayload } from 'rxbeach/internal'; +import { mockAction } from 'rxbeach/internal'; import { withNamespace } from './operators'; const pipeActionWithPayload = ( payload: P, pipe: OperatorFunction, R> ): Promise => - of(actionWithPayload('', payload)) + of(mockAction('', '', payload) as ActionWithPayload

) .pipe(pipe) .toPromise(); @@ -37,9 +37,9 @@ describe('operators', function() { const otherType = 'Wrong type'; const res = await of( - actionWithoutPayload(targetType), - actionWithoutPayload(otherType), - actionWithoutPayload(targetType) + mockAction(targetType), + mockAction(otherType), + mockAction(targetType) ) .pipe(ofType(targetType)) .toPromise(); @@ -53,9 +53,9 @@ describe('operators', function() { const otherType = 'Wrong type'; const collectedTypes = await of( - actionWithoutPayload(targetType1), - actionWithoutPayload(otherType), - actionWithoutPayload(targetType2) + mockAction(targetType1), + mockAction(otherType), + mockAction(targetType2) ) .pipe( ofType(targetType1, targetType2), @@ -73,14 +73,14 @@ describe('operators', function() { const namespace = 'namespace'; const res = await of( - actionWithoutPayload(actionType), - actionWithoutPayload(actionType, namespace), - actionWithoutPayload(actionType) + mockAction(actionType), + mockAction(actionType, namespace), + mockAction(actionType) ) .pipe(withNamespace(namespace)) .toPromise(); - deepEqual(res, actionWithoutPayload(actionType, namespace)); + deepEqual(res, mockAction(actionType, namespace)); }); }); });