-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppContext.test.tsx
80 lines (68 loc) · 2.25 KB
/
AppContext.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React from 'react';
import { renderHook, act } from '@testing-library/react-hooks';
import { ActionType, useApp, AppProvider } from './AppContext';
const DEFAULT_STATE_MOCK = {
items: [
{ id: '1', detail: 'buy milk', checked: true },
{ id: '2', detail: 'book hotel', checked: false },
{ id: '3', detail: 'buy bananas', checked: true },
],
};
describe('<AppContext>', () => {
it('should add tasks', async () => {
const itemMock = { id: '1', detail: 'buy milk', checked: false };
const item2Mock = { id: '2', detail: 'book hotel', checked: false };
const { result } = renderHook(() => useApp(), { wrapper: AppProvider });
act(() => {
result.current.dispatch({
type: ActionType.ADD_ITEM,
payload: itemMock,
});
});
expect(result.current.state.items).toEqual([itemMock]);
act(() => {
result.current.dispatch({
type: ActionType.ADD_ITEM,
payload: item2Mock,
});
});
expect(result.current.state.items).toEqual([itemMock, item2Mock]);
});
it('should remove checked tasks', async () => {
const { result } = renderHook(() => useApp(), {
wrapper: AppProvider,
initialProps: { defaultState: DEFAULT_STATE_MOCK },
});
act(() => {
result.current.dispatch({
type: ActionType.REMOVE_CHECKED_ITEMS,
});
});
expect(result.current.state.items).toEqual([DEFAULT_STATE_MOCK.items[1]]);
});
it('should toggle task checked value', async () => {
const { result } = renderHook(() => useApp(), {
wrapper: AppProvider,
initialProps: { defaultState: DEFAULT_STATE_MOCK },
});
act(() => {
result.current.dispatch({
type: ActionType.TOGGLE_ITEM,
payload: DEFAULT_STATE_MOCK.items[1].id,
});
});
expect(result.current.state.items).toHaveLength(
DEFAULT_STATE_MOCK.items.length,
);
expect(result.current.state.items[1].checked).toBe(true);
// change back to initial state
act(() => {
result.current.dispatch({
type: ActionType.TOGGLE_ITEM,
payload: DEFAULT_STATE_MOCK.items[1].id,
});
});
expect(result.current.state.items).toEqual(DEFAULT_STATE_MOCK.items);
expect(result.current.state.items[1].checked).toBe(false);
});
});