-
Notifications
You must be signed in to change notification settings - Fork 61
/
03_startup_spec.js
71 lines (67 loc) · 1.89 KB
/
03_startup_spec.js
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
import React, { StrictMode } from 'react';
import { render, cleanup } from '@testing-library/react';
import { createGlobalState, createStore } from '../src/index';
describe('startup spec', () => {
afterEach(cleanup);
it('should setSetGlobalState at start up', () => {
const initialState = {
count1: 0,
count2: 0,
};
const { setGlobalState, useGlobalState } = createGlobalState(initialState);
const Counter = ({ name }) => {
setGlobalState(name, 9);
const [value] = useGlobalState(name);
return (
<div>
<div>{name}</div>
<div data-testid={name}>{value}</div>
</div>
);
};
const App = () => (
<StrictMode>
<Counter name="count1" />
<Counter name="count2" />
</StrictMode>
);
const { getByTestId } = render(<App />);
expect(getByTestId('count1').innerHTML).toBe('9');
expect(getByTestId('count2').innerHTML).toBe('9');
});
it('should dispatch at start up', () => {
const initialState = {
count1: 0,
count2: 0,
};
const reducer = (state, action) => {
if (action.type === 'setCounter') {
return {
...state,
[action.name]: action.value,
};
}
return state;
};
const { dispatch, useGlobalState } = createStore(reducer, initialState);
const Counter = ({ name }) => {
dispatch({ type: 'setCounter', name, value: 9 });
const [value] = useGlobalState(name);
return (
<div>
<div>{name}</div>
<div data-testid={name}>{value}</div>
</div>
);
};
const App = () => (
<StrictMode>
<Counter name="count1" />
<Counter name="count2" />
</StrictMode>
);
const { getByTestId } = render(<App />);
expect(getByTestId('count1').innerHTML).toBe('9');
expect(getByTestId('count2').innerHTML).toBe('9');
});
});