// page1/redux.jsx
export const NEXT_COUNT = "NEXT_COUNT";
export const PREV_COUNT = "PREV_COUNT";
export const setCount = (type) => ({
type: type === "next" ? NEXT_COUNT : PREV_COUNT,
});
export const countReducer = (state = 0, action) => {
if (action.type === NEXT_COUNT) {
return state + 1;
}
if (action.type === PREV_COUNT) {
return state - 1;
}
return state;
};
// page1/redux.test.js
import { NEXT_COUNT, PREV_COUNT, setCount, countReducer } from "./redux";
describe("PAGE1 测试redux", () => {
test("action creator test", () => {
expect(setCount("next")).toEqual({ type: NEXT_COUNT });
expect(setCount("prev")).toEqual({ type: PREV_COUNT });
});
test("reducer test", () => {
expect(countReducer(0, { type: NEXT_COUNT })).toEqual(1);
expect(countReducer(10, { type: NEXT_COUNT })).toEqual(11);
expect(countReducer(-10, { type: NEXT_COUNT })).toEqual(-9);
expect(countReducer(0, { type: PREV_COUNT })).toEqual(-1);
expect(countReducer(10, { type: PREV_COUNT })).toEqual(9);
expect(countReducer(-10, { type: PREV_COUNT })).toEqual(-11);
});
});