Skip to content

Commit

Permalink
Consolidate mockAction, clean up namespace tests.
Browse files Browse the repository at this point in the history
Should have been two commits
  • Loading branch information
tlaundal committed Sep 27, 2019
1 parent bfc96ae commit 30c7ca8
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 88 deletions.
107 changes: 77 additions & 30 deletions examples/namespace.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,104 @@ 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<number>).payload || 0),
0
);
import { AnyAction, mockAction } from 'rxbeach/internal';

export default function namespaceExamples() {
describe('namespaces', function() {
const testAction = actionCreator<number>('[test] primitive action');
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<AnyAction>();
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<AnyAction>();
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);
});
});
});
}
2 changes: 1 addition & 1 deletion src/internal/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { actionWithoutPayload, actionWithPayload } from './testUtils';
export { mockAction } from './testUtils';
export {
VoidPayload,
AnyAction,
Expand Down
27 changes: 11 additions & 16 deletions src/internal/testUtils.ts
Original file line number Diff line number Diff line change
@@ -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 = <P = VoidPayload>(
type: string,
namespace?: string
): ActionWithoutPayload => ({
meta: { namespace },
type,
});

export const actionWithPayload = <P>(
type: string,
payload: P,
namespace?: string
): ActionWithPayload<P> => ({
...actionWithoutPayload(type, namespace),
payload,
});
namespace?: string,
payload?: P
): Action<P> =>
({
meta: { namespace },
type,
payload,
} as Action<P>);
54 changes: 25 additions & 29 deletions src/namespace.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
});
});
Expand Down
24 changes: 12 additions & 12 deletions src/operators/operators.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <P, R>(
payload: P,
pipe: OperatorFunction<ActionWithPayload<P>, R>
): Promise<R> =>
of(actionWithPayload('', payload))
of(mockAction('', '', payload) as ActionWithPayload<P>)
.pipe(pipe)
.toPromise();

Expand All @@ -37,9 +37,9 @@ describe('operators', function() {
const otherType = 'Wrong type';

const res = await of<ActionWithoutPayload>(
actionWithoutPayload(targetType),
actionWithoutPayload(otherType),
actionWithoutPayload(targetType)
mockAction(targetType),
mockAction(otherType),
mockAction(targetType)
)
.pipe(ofType(targetType))
.toPromise();
Expand All @@ -53,9 +53,9 @@ describe('operators', function() {
const otherType = 'Wrong type';

const collectedTypes = await of<ActionWithoutPayload>(
actionWithoutPayload(targetType1),
actionWithoutPayload(otherType),
actionWithoutPayload(targetType2)
mockAction(targetType1),
mockAction(otherType),
mockAction(targetType2)
)
.pipe(
ofType(targetType1, targetType2),
Expand All @@ -73,14 +73,14 @@ describe('operators', function() {
const namespace = 'namespace';

const res = await of<ActionWithoutPayload>(
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));
});
});
});

0 comments on commit 30c7ca8

Please sign in to comment.