-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
68 lines (56 loc) · 1.64 KB
/
example.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
// Use React development.
process.env.NODE_ENV = 'development';
// Dependencies.
const { act, render } = require('@testing-library/react');
const globalJsdom = require('global-jsdom');
// eslint-disable-next-line import/no-extraneous-dependencies,no-unused-vars
const JSDOM = require('jsdom');
const React = require('react');
// eslint-disable-next-line no-unused-vars
const ReactDOM = require('react-dom');
const {
isEmpty,
useStore,
useStores,
withStore,
} = require('react-store-context-hooks');
// eslint-disable-next-line no-console
['', 'value'].forEach((value) => console.log(`isEmpty('${value}') => ${isEmpty(value)}`));
const response = { renderedA: 0, renderedB: 0 };
const App = withStore(() => {
response.setStores = useStores().setStores;
return (
<React.Fragment>
<CompA />
<CompB />
</React.Fragment>
);
});
const CompA = () => {
response.renderedA += 1;
[response.getA, response.setA, response.delA] = useStore('key');
// eslint-disable-next-line no-console
console.log(`Component A value => ${response.getA}`);
return null;
};
const CompB = () => {
response.renderedB += 1;
[response.getB, response.setB, response.delB] = useStore('key', 'defaultB');
// eslint-disable-next-line no-console
console.log(`Component B value => ${response.getB}`);
return null;
};
const cleanup = globalJsdom();
const { unmount } = render(<App />);
// useStores.setStores.
act(() => response.setStores({
key: 'default',
}));
// useStore.set.
act(() => response.setA('set-from-A'));
act(() => response.setA('set-from-B'));
// useStore.del.
act(() => response.delA());
act(() => response.delB());
unmount();
cleanup();