Skip to content

Commit bdec1e5

Browse files
authored
Updated tests to fix build Closed piotrwitek#95 (piotrwitek#133)
1 parent a4310a8 commit bdec1e5

File tree

4 files changed

+32
-71
lines changed

4 files changed

+32
-71
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,41 +1135,45 @@ export default combineReducers<TodosState, TodosAction>({
11351135
### Testing reducer
11361136
11371137
```tsx
1138-
import { todosReducer as reducer, todosActions as actions } from './';
1138+
import {
1139+
todosReducer as reducer,
1140+
todosActions as actions,
1141+
TodosState,
1142+
} from './';
11391143

11401144
/**
11411145
* FIXTURES
11421146
*/
1143-
const activeTodo = { id: '1', completed: false, title: 'active todo' };
1144-
const completedTodo = { id: '2', completed: true, title: 'completed todo' };
1145-
1146-
const initialState = reducer(undefined, {} as any);
1147+
const getInitialState = (initial?: Partial<TodosState>) =>
1148+
reducer(initial as TodosState, {} as any);
11471149

11481150
/**
11491151
* STORIES
11501152
*/
11511153
describe('Todos Stories', () => {
11521154
describe('initial state', () => {
11531155
it('should match a snapshot', () => {
1156+
const initialState = getInitialState();
11541157
expect(initialState).toMatchSnapshot();
11551158
});
11561159
});
11571160

11581161
describe('adding todos', () => {
11591162
it('should add a new todo as the first element', () => {
1160-
const action = actions.add('new todo');
1161-
const state = reducer(initialState, action);
1163+
const initialState = getInitialState();
1164+
expect(initialState.todos).toHaveLength(0);
1165+
const state = reducer(initialState, actions.add('new todo'));
11621166
expect(state.todos).toHaveLength(1);
1163-
expect(state.todos[0].id).toEqual(action.payload.id);
1167+
expect(state.todos[0].title).toEqual('new todo');
11641168
});
11651169
});
11661170

11671171
describe('toggling completion state', () => {
11681172
it('should mark active todo as complete', () => {
1169-
const action = actions.toggle(activeTodo.id);
1170-
const state0 = { ...initialState, todos: [activeTodo] };
1171-
expect(state0.todos[0].completed).toBeFalsy();
1172-
const state1 = reducer(state0, action);
1173+
const activeTodo = { id: '1', completed: false, title: 'active todo' };
1174+
const initialState = getInitialState({ todos: [activeTodo] });
1175+
expect(initialState.todos[0].completed).toBeFalsy();
1176+
const state1 = reducer(initialState, actions.toggle(activeTodo.id));
11731177
expect(state1.todos[0].completed).toBeTruthy();
11741178
});
11751179
});

playground/src/features/todos-typesafe/__snapshots__/reducer.spec.ts.snap

Lines changed: 0 additions & 8 deletions
This file was deleted.

playground/src/features/todos-typesafe/reducer.spec.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

playground/src/features/todos/reducer.spec.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
1-
import { todosReducer as reducer, todosActions as actions } from './';
1+
import {
2+
todosReducer as reducer,
3+
todosActions as actions,
4+
TodosState,
5+
} from './';
26

37
/**
48
* FIXTURES
59
*/
6-
const activeTodo = { id: '1', completed: false, title: 'active todo' };
7-
const completedTodo = { id: '2', completed: true, title: 'completed todo' };
8-
9-
const initialState = reducer(undefined, {} as any);
10+
const getInitialState = (initial?: Partial<TodosState>) =>
11+
reducer(initial as TodosState, {} as any);
1012

1113
/**
1214
* STORIES
1315
*/
1416
describe('Todos Stories', () => {
1517
describe('initial state', () => {
1618
it('should match a snapshot', () => {
19+
const initialState = getInitialState();
1720
expect(initialState).toMatchSnapshot();
1821
});
1922
});
2023

2124
describe('adding todos', () => {
2225
it('should add a new todo as the first element', () => {
23-
const action = actions.add('new todo');
24-
const state = reducer(initialState, action);
26+
const initialState = getInitialState();
27+
expect(initialState.todos).toHaveLength(0);
28+
const state = reducer(initialState, actions.add('new todo'));
2529
expect(state.todos).toHaveLength(1);
26-
expect(state.todos[0].id).toEqual(action.payload.id);
30+
expect(state.todos[0].title).toEqual('new todo');
2731
});
2832
});
2933

3034
describe('toggling completion state', () => {
3135
it('should mark active todo as complete', () => {
32-
const action = actions.toggle(activeTodo.id);
33-
const state0 = { ...initialState, todos: [activeTodo] };
34-
expect(state0.todos[0].completed).toBeFalsy();
35-
const state1 = reducer(state0, action);
36+
const activeTodo = { id: '1', completed: false, title: 'active todo' };
37+
const initialState = getInitialState({ todos: [activeTodo] });
38+
expect(initialState.todos[0].completed).toBeFalsy();
39+
const state1 = reducer(initialState, actions.toggle(activeTodo.id));
3640
expect(state1.todos[0].completed).toBeTruthy();
3741
});
3842
});

0 commit comments

Comments
 (0)