-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCounter.test.tsx
104 lines (83 loc) · 2.49 KB
/
SimpleCounter.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Cmd, Sub } from '@foo-x/react-tea';
import { act, render, screen } from '@testing-library/react';
import { init, subscriptions, update, view } from './SimpleCounter';
describe('SimpleCounter', () => {
describe('init', () => {
it('returns default value and none cmd', () => {
const defaultValue = Math.random();
const [initModel, initCmd] = init({ props: { defaultValue } });
expect(initModel).toBe(defaultValue);
expect(initCmd).toBe(Cmd.none());
});
});
describe('update', () => {
it('returns incremented model and none cmd on increment msg', () => {
const model = Math.random();
const [newModel, newCmd] = update({
model,
msg: 'increment',
props: { defaultValue: 0 },
});
expect(newModel).toBe(model + 1);
expect(newCmd).toBe(Cmd.none());
});
it('returns decremented model and none cmd on decrement msg', () => {
const model = Math.random();
const [newModel, newCmd] = update({
model,
msg: 'decrement',
props: { defaultValue: 0 },
});
expect(newModel).toBe(model - 1);
expect(newCmd).toBe(Cmd.none());
});
});
describe('subscriptions', () => {
it('is none', () => {
expect(subscriptions).toBe(Sub.none());
});
});
describe('view', () => {
const View = view;
let props: {
model: number;
dispatch: () => void;
props: {
defaultValue: number;
};
};
beforeEach(() => {
props = {
model: Math.random(),
dispatch: jest.fn(),
props: {
defaultValue: Math.random(),
},
};
});
it('has heading of default value', () => {
render(<View {...props} />);
expect(
screen.getByText(`default: ${props.props.defaultValue}`)
).toBeInTheDocument();
});
it('has text of model', () => {
render(<View {...props} />);
expect(screen.getByText(props.model)).toBeInTheDocument();
});
test('dispatch decrement msg on click decrement button', () => {
render(<View {...props} />);
act(() => {
screen.getByRole('button', { name: '-' }).click();
});
expect(props.dispatch).toBeCalledWith('decrement');
});
test('dispatch increment msg on click increment button', () => {
render(<View {...props} />);
act(() => {
screen.getByRole('button', { name: '+' }).click();
});
expect(props.dispatch).toBeCalledWith('increment');
});
});
});