Skip to content

Commit c284fef

Browse files
committed
Add tests
1 parent c0d55b7 commit c284fef

File tree

6 files changed

+304
-16
lines changed

6 files changed

+304
-16
lines changed

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@
1212
"watch": "tsc --watch"
1313
},
1414
"devDependencies": {
15+
"@types/flux-standard-action": "^0.5.31",
1516
"@types/jest": "^21.1.8",
17+
"flux-standard-action": "^2.0.0",
1618
"jest": "^21.2.1",
1719
"redux": "^3.7.2",
20+
"redux-thunk": "^2.2.0",
21+
"ts-jest": "^21.2.4",
1822
"typescript": "^2.6.2"
1923
},
2024
"peerDependencies": {
2125
"jest": "^21.2.1",
2226
"redux": "^3.7.2"
27+
},
28+
"jest": {
29+
"transform": {
30+
"^.+\\.tsx?$": "ts-jest"
31+
},
32+
"testRegex": "/test/.+\\.test\\.(tsx?)$",
33+
"moduleFileExtensions": [
34+
"ts",
35+
"tsx",
36+
"js",
37+
"jsx",
38+
"json"
39+
]
2340
}
2441
}

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Reducer, Store, createStore } from "redux";
22

3-
export interface Action {
4-
type: any;
5-
payload: any;
6-
};
3+
export type Action = {
4+
type: any,
5+
payload: any
6+
}
77

8-
export default class ReducerTestContext<S> {
8+
export class ReducerTestContext<S> {
99
constructor(
1010
private readonly reducer: Reducer<S>,
1111
private readonly defaultState: S
@@ -55,7 +55,7 @@ export default class ReducerTestContext<S> {
5555
}
5656
}
5757

58-
export function reducerTest<S>(
58+
export default function reducerTest<S>(
5959
reducer: Reducer<S | undefined>,
6060
defaultState?: S
6161
) {

test/__snapshots__/index.test.ts.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`reducer SET_A("other a") matches snapshot 1`] = `
4+
Object {
5+
"a": "other a",
6+
"d": 0,
7+
}
8+
`;
9+
10+
exports[`reducer matches snapshot 1`] = `
11+
Array [
12+
Object {
13+
"a": "a",
14+
"b": Object {
15+
"c": true,
16+
},
17+
"d": 0,
18+
},
19+
Object {
20+
"a": "some other",
21+
"b": Object {
22+
"c": true,
23+
},
24+
"d": 0,
25+
},
26+
]
27+
`;

test/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import reducer, {setA, setB } from "./reducer";
2+
import reducerTest from "../src/index";
3+
4+
describe("reducer", reducerTest(reducer)(function () {
5+
this.snapshotAction(setA("other a"));
6+
this.snapshotActionSequence(setB({ c: true }), setA("some other"));
7+
}));

test/reducer.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Dispatch } from "redux";
2+
3+
export interface State {
4+
a: string;
5+
b?: {
6+
c: boolean;
7+
};
8+
d: number;
9+
}
10+
11+
const DEFAULT_STATE = {
12+
a: "a",
13+
d: 0
14+
}
15+
16+
type Action = { type: "SET_A", payload: State["a"] } | { type: "SET_B", payload: State["b"] } | { type: "SET_D", payload: State["d"] };
17+
18+
export default function reducer(state: State = DEFAULT_STATE, action: Action): State {
19+
switch (action.type) {
20+
case "SET_A":
21+
return { ...state, a: action.payload };
22+
case "SET_B":
23+
return { ...state, b: action.payload };
24+
case "SET_D":
25+
return { ...state, d: action.payload };
26+
}
27+
}
28+
29+
export const setA = (a: State["a"]) => ({ type: "SET_A", payload: a });
30+
export const setB = (b: State["b"]) => ({ type: "SET_B", payload: b });

0 commit comments

Comments
 (0)