Skip to content

Commit 231dd7c

Browse files
committed
test: add tests
1 parent e85da3d commit 231dd7c

File tree

7 files changed

+165
-21
lines changed

7 files changed

+165
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ node_modules
77
npm-*.log
88
yarn.lock
99

10+
tests-build
11+
1012
# OS X
1113
.DS_Store

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_js:
66
script:
77
- npm run lint
88
- npm run build
9+
- npm test

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export interface AsyncActionCreators<P, S, E> {
2929
}
3030
export interface ActionCreatorFactory {
3131
(type: string, commonMeta?: Object, error?: boolean): EmptyActionCreator;
32-
<P>(type: string, commonMeta?: Object, isError?: ((payload: P) => boolean) | boolean): ActionCreator<P>;
32+
<P>(type: string, commonMeta?: Object, isError?: boolean): ActionCreator<P>;
33+
<P>(type: string, commonMeta?: Object, isError?: (payload: P) => boolean): ActionCreator<P>;
3334
async<P, S>(type: string, commonMeta?: Object): AsyncActionCreators<P, S, any>;
3435
async<P, S, E>(type: string, commonMeta?: Object): AsyncActionCreators<P, S, E>;
3536
}

lib/index.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,14 @@ function isType(action, actionCreator) {
33
return action.type === actionCreator.type;
44
}
55
exports.isType = isType;
6-
var isDev = (process && process.env && process.env.NODE_ENV) !== 'production';
7-
function wrapIsError(default_, isError) {
8-
if (isError === undefined) {
9-
return default_;
10-
}
11-
if (typeof isError === "boolean") {
12-
return function () { return isError; };
13-
}
14-
return isError;
15-
}
166
function actionCreatorFactory(prefix, defaultIsError) {
177
if (defaultIsError === void 0) { defaultIsError = function (p) { return p instanceof Error; }; }
188
var actionTypes = {};
199
var base = prefix ? prefix + "/" : "";
20-
function baseActionCreator(isError, type, commonMeta) {
21-
var fullType = "" + base + type;
22-
if (isDev) {
10+
function actionCreator(type, commonMeta, isError) {
11+
if (isError === void 0) { isError = defaultIsError; }
12+
var fullType = base + type;
13+
if (process.env.NODE_ENV !== 'production') {
2314
if (actionTypes[fullType])
2415
throw new Error("Duplicate action type: " + fullType);
2516
actionTypes[fullType] = true;
@@ -28,20 +19,19 @@ function actionCreatorFactory(prefix, defaultIsError) {
2819
var action = {
2920
type: fullType,
3021
payload: payload,
31-
meta: Object.assign({}, commonMeta, meta),
3222
};
33-
if (isError(payload)) {
23+
if (commonMeta || meta) {
24+
action.meta = Object.assign({}, commonMeta, meta);
25+
}
26+
if (isError && (typeof isError === 'boolean' || isError(payload))) {
3427
action.error = true;
3528
}
3629
return action;
3730
}, { type: fullType });
3831
}
39-
var actionCreator = function (type, commonMeta, isError) {
40-
return baseActionCreator(wrapIsError(defaultIsError, isError), type, commonMeta);
41-
};
4232
function asyncActionCreators(type, commonMeta) {
4333
return {
44-
type: prefix ? prefix + "/" + type : type,
34+
type: base + type,
4535
started: actionCreator(type + "_STARTED", commonMeta, false),
4636
done: actionCreator(type + "_DONE", commonMeta, false),
4737
failed: actionCreator(type + "_FAILED", commonMeta, true),

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
"repository": "aikoven/redux-typescript-actions",
1515
"scripts": {
1616
"clean": "rimraf es6 lib",
17-
"lint": "tslint -c tslint.json src/**/*.ts",
17+
"lint": "tslint -c tslint.json src/**/*.ts tests/**/*.ts",
18+
"test": "tsc -p tsconfig.tests.json && tape tests-build/tests/*.js",
1819
"build": "tsc",
1920
"prepublish": "npm run clean && npm run build"
2021
},
2122
"author": "Daniel Lytkin <dan.lytkin@gmail.com>",
2223
"license": "MIT",
2324
"devDependencies": {
25+
"@types/tape": "^4.2.28",
2426
"redux": "^3.6.0",
2527
"rimraf": "^2.5.4",
28+
"tape": "^4.6.2",
2629
"tslint": "^3.15.1",
2730
"typescript": "^2.0.3"
2831
}

tests/index.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import test = require('tape');
2+
import actionCreatorFactory, {isType} from "../src/index";
3+
4+
test('isType', assert => {
5+
const actionCreator = actionCreatorFactory();
6+
7+
const action1 = actionCreator('ACTION_1');
8+
const action2 = actionCreator('ACTION_2');
9+
10+
const action = action1();
11+
12+
assert.true(isType(action, action1));
13+
assert.false(isType(action, action2));
14+
15+
assert.end();
16+
});
17+
18+
test('basic', assert => {
19+
const actionCreator = actionCreatorFactory();
20+
21+
const someAction = actionCreator<{foo: string}>('ACTION_TYPE');
22+
23+
assert.throws(() => actionCreator('ACTION_TYPE'),
24+
'Duplicate action type ACTION_TYPE');
25+
26+
assert.equal(someAction.type, 'ACTION_TYPE');
27+
28+
const action = someAction({foo: 'bar'});
29+
30+
assert.equal(action.type, 'ACTION_TYPE');
31+
assert.equal(action.error, undefined);
32+
assert.equal(action.meta, undefined);
33+
assert.deepEqual(action.payload, {foo: 'bar'});
34+
35+
assert.end();
36+
});
37+
38+
test('meta', assert => {
39+
const actionCreator = actionCreatorFactory();
40+
41+
const someAction = actionCreator('ACTION_TYPE');
42+
43+
const action = someAction(undefined, {foo: 'bar'});
44+
45+
assert.deepEqual(action.meta, {foo: 'bar'});
46+
47+
const someActionWithMeta = actionCreator('ACTION_WITH_META', {foo: 'bar'});
48+
49+
const actionWithMeta = someActionWithMeta(undefined);
50+
51+
assert.deepEqual(actionWithMeta.meta, {foo: 'bar'});
52+
53+
const actionWithExtraMeta = someActionWithMeta(undefined, {fizz: 'buzz'});
54+
55+
assert.deepEqual(actionWithExtraMeta.meta, {foo: 'bar', fizz: 'buzz'});
56+
57+
assert.end();
58+
});
59+
60+
test('error actions', assert => {
61+
const actionCreator = actionCreatorFactory();
62+
63+
const errorAction = actionCreator('ERROR_ACTION', null, true);
64+
65+
const action = errorAction();
66+
67+
assert.true(action.error);
68+
69+
const inferredErrorAction = actionCreator<any>('INF_ERROR_ACTION', null);
70+
71+
assert.false(inferredErrorAction({}).error);
72+
assert.true(inferredErrorAction(new Error()).error);
73+
74+
const customErrorAction = actionCreator<{
75+
isError: boolean;
76+
}>('CUSTOM_ERROR_ACTION', null, payload => payload.isError);
77+
78+
assert.false(customErrorAction({isError: false}).error);
79+
assert.true(customErrorAction({isError: true}).error);
80+
81+
const actionCreator2 = actionCreatorFactory(null,
82+
payload => payload.isError);
83+
84+
const customErrorAction2 = actionCreator2<{
85+
isError: boolean;
86+
}>('CUSTOM_ERROR_ACTION');
87+
88+
assert.false(customErrorAction2({isError: false}).error);
89+
assert.true(customErrorAction2({isError: true}).error);
90+
91+
assert.end();
92+
});
93+
94+
test('prefix', assert => {
95+
const actionCreator = actionCreatorFactory('somePrefix');
96+
97+
const someAction = actionCreator('SOME_ACTION');
98+
99+
assert.equal(someAction.type, 'somePrefix/SOME_ACTION');
100+
101+
const action = someAction();
102+
103+
assert.equal(action.type, 'somePrefix/SOME_ACTION');
104+
105+
assert.end();
106+
});
107+
108+
test('async', assert => {
109+
const actionCreator = actionCreatorFactory('prefix');
110+
111+
const asyncActions = actionCreator.async<
112+
{foo: string},
113+
{bar: string}
114+
>('DO_SOMETHING', {baz: 'baz'});
115+
116+
assert.equal(asyncActions.type, 'prefix/DO_SOMETHING');
117+
assert.equal(asyncActions.started.type, 'prefix/DO_SOMETHING_STARTED');
118+
assert.equal(asyncActions.done.type, 'prefix/DO_SOMETHING_DONE');
119+
assert.equal(asyncActions.failed.type, 'prefix/DO_SOMETHING_FAILED');
120+
121+
const started = asyncActions.started({foo: 'foo'});
122+
assert.equal(started.type, 'prefix/DO_SOMETHING_STARTED');
123+
assert.deepEqual(started.payload, {foo: 'foo'});
124+
assert.deepEqual(started.meta, {baz: 'baz'});
125+
assert.true(!started.error);
126+
127+
const done = asyncActions.done({params: {foo: 'foo'}, result: {bar: 'bar'}});
128+
assert.true(!done.error);
129+
130+
const failed = asyncActions.failed({params: {foo: 'foo'}, error: 'error'});
131+
assert.true(failed.error);
132+
133+
assert.end();
134+
});

tsconfig.tests.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES5",
4+
"lib": ["es5", "es2015.core"],
5+
"moduleResolution": "node",
6+
"outDir": "tests-build",
7+
"noImplicitReturns": true,
8+
"module": "commonjs"
9+
},
10+
"files": [
11+
"tests/index.ts"
12+
]
13+
}

0 commit comments

Comments
 (0)