Skip to content

Commit

Permalink
Converted tests to marble tests using rxjs-marbles #14
Browse files Browse the repository at this point in the history
  • Loading branch information
anton164 committed Oct 8, 2019
1 parent f73f52f commit a78a884
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 73 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"mocha": "6.2.0",
"prettier": "1.18.2",
"react-test-renderer": "16.9.0",
"rimraf": "^3.0.0",
"rimraf": "3.0.0",
"rxjs-marbles": "5.0.3",
"rxjs-subscription-count": "1.0.1",
"ts-mocha": "6.0.0",
"ts-node": "8.3.0",
Expand Down
145 changes: 74 additions & 71 deletions src/operators/operators.tests.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { equal, deepEqual } from 'assert';
import { of, OperatorFunction, Scheduler } from 'rxjs';
import { reduce } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { marbles } from 'rxjs-marbles/mocha';
import { ActionWithPayload, ActionWithoutPayload } from 'rxbeach';
import { extractPayload, ofType } from 'rxbeach/operators';
import { mockAction } from 'rxbeach/internal';
import { withNamespace } from './operators';

const testScheduler = new TestScheduler((actual, expected) => {
deepEqual(actual, expected);
});

const pipeActionWithPayload = <P, R>(
payload: P,
pipe: OperatorFunction<ActionWithPayload<P>, R>
): Promise<R> =>
of(mockAction('', '', payload) as ActionWithPayload<P>)
.pipe(pipe)
.toPromise();

describe('operators', function() {
describe('extractPayload', function() {
const tests = [
Expand All @@ -28,68 +13,86 @@ describe('operators', function() {
];

for (const [name, payload] of tests) {
it(`Should extract ${name} payload`, async function() {
const res = await pipeActionWithPayload(payload, extractPayload());
it(
`Should extract ${name} payload`,
marbles(m => {
const inputs = {
a: mockAction('', '', payload),
};
const outputs = {
p: payload,
};

const source = m.hot<ActionWithPayload<any>>('aa', inputs);
const expected = m.hot('pp', outputs);

equal(res, payload);
});
m.expect(source.pipe(extractPayload())).toBeObservable(expected);
})
);
}
});

describe('ofType', function() {
it('Should filter one action type', function() {
const targetType = 'Correct type';
const otherType = 'Wrong type';

const actionMarbles = {
a: mockAction(targetType),
b: mockAction(otherType),
};

testScheduler.run(({ hot, expectObservable }) => {
const filteredActions = hot<ActionWithoutPayload>(
'aba',
actionMarbles
).pipe(ofType(targetType));

expectObservable(filteredActions).toBe('a-a', actionMarbles);
});
});

it('Should filter multiple action types', async function() {
const targetType1 = 'Correct type one';
const targetType2 = 'Correct type two';
const otherType = 'Wrong type';

const collectedTypes = await of<ActionWithoutPayload>(
mockAction(targetType1),
mockAction(otherType),
mockAction(targetType2)
)
.pipe(
ofType(targetType1, targetType2),
reduce((acc, { type }) => [...acc, type], [] as string[])
)
.toPromise();

deepEqual(collectedTypes, [targetType1, targetType2]);
});
it(
'Should filter one action type',
marbles(m => {
const targetType = 'Correct type';
const otherType = 'Wrong type';

const inputsOutputs = {
a: mockAction(targetType),
b: mockAction(otherType),
};

const source = m.hot('aba', inputsOutputs);
const expected = m.hot('a-a', inputsOutputs);

m.expect(source.pipe(ofType(targetType))).toBeObservable(expected);
})
);

it(
'Should filter multiple action types',
marbles(m => {
const targetType1 = 'Correct type one';
const targetType2 = 'Correct type two';
const otherType = 'Wrong type';

const inputsOutputs = {
a: mockAction(targetType1),
b: mockAction(targetType2),
c: mockAction(otherType),
};

const source = m.hot('acb', inputsOutputs);
const expected = m.hot('a-b', inputsOutputs);

const collectedTypes = source.pipe(ofType(targetType1, targetType2));

m.expect(collectedTypes).toBeObservable(expected);
})
);
});

describe('withNamespace', function() {
it('Should filter actions by namespace', async function() {
const actionType = 'actionType';
const namespace = 'namespace';

const res = await of<ActionWithoutPayload>(
mockAction(actionType),
mockAction(actionType, namespace),
mockAction(actionType)
)
.pipe(withNamespace(namespace))
.toPromise();

deepEqual(res, mockAction(actionType, namespace));
});
it(
'Should filter actions by namespace',
marbles(m => {
const actionType = 'actionType';
const namespace = 'namespace';

const inputsOutputs = {
a: mockAction(actionType),
b: mockAction(actionType, namespace),
};

const source = m.hot('aba', inputsOutputs);
const expected = m.hot('-b-', inputsOutputs);

m.expect(source.pipe(withNamespace(namespace))).toBeObservable(
expected
);
})
);
});
});
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@ fast-diff@^1.1.2:
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==

fast-equals@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-equals/-/fast-equals-2.0.0.tgz#bef2c423af3939f2c54310df54c57e64cd2adefc"
integrity sha512-u6RBd8cSiLLxAiC04wVsLV6GBFDOXcTCgWkd3wEoFXgidPSoAJENqC9m7Jb2vewSvjBIfXV6icKeh3GTKfIaXA==

fast-json-stable-stringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
Expand Down Expand Up @@ -1552,7 +1557,7 @@ rimraf@2.6.3:
dependencies:
glob "^7.1.3"

rimraf@^3.0.0:
rimraf@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
integrity sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==
Expand All @@ -1566,6 +1571,13 @@ run-async@^2.2.0:
dependencies:
is-promise "^2.1.0"

rxjs-marbles@5.0.3:
version "5.0.3"
resolved "https://registry.yarnpkg.com/rxjs-marbles/-/rxjs-marbles-5.0.3.tgz#d3ca62a4e02d032b1b4ffd558e93336ad78fd100"
integrity sha512-JK6EvLe9uReJxBmUgdKrpMB2JswV+fDcKDg97x20LErLQ7Gi0FG3YEr2Uq9hvgHJjgZXGCvonpzcxARLzKsT4A==
dependencies:
fast-equals "^2.0.0"

rxjs-spy@7.5.1:
version "7.5.1"
resolved "https://registry.yarnpkg.com/rxjs-spy/-/rxjs-spy-7.5.1.tgz#1a9ef50bc8d7dd00d9ecf3c54c00929231eaf319"
Expand Down

0 comments on commit a78a884

Please sign in to comment.