-
Notifications
You must be signed in to change notification settings - Fork 10
/
readFromTwoStores.test.tsx
60 lines (48 loc) · 1.57 KB
/
readFromTwoStores.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
import React from 'react';
import { store as globalStore, WithStoreProp } from '../..';
import * as testUtils from '../testUtils';
const getTitle = () => globalStore.meta.title;
it('should not matter which store I read/write from', () => {
globalStore.meta = { title: 'Hello' };
const { getByText } = testUtils.collectAndRenderStrict(
({ store }: WithStoreProp) => (
<div>
<p>{`${store.meta.title} from the store`}</p>
<p>{`${globalStore.meta.title} from the globalStore`}</p>
<button
onClick={() => {
store.meta.title += '1';
globalStore.meta.title += '2';
store.meta.title += '3';
globalStore.meta.title = `${getTitle()}4`;
}}
>
Change things
</button>
</div>
)
);
getByText('Change things').click();
getByText('Hello1234 from the store');
getByText('Hello1234 from the globalStore');
});
it('should subscribe to changes from the global store', () => {
globalStore.meta = { title: 'Hello' };
// This is wrapped in `collect` but doesn't reference props at all
const { getByText } = testUtils.collectAndRenderStrict(() => (
<div>
<p>{`${globalStore.meta.title} from the globalStore`}</p>
<button
onClick={() => {
globalStore.meta.title += '2';
globalStore.meta.title = `${getTitle()}4`;
}}
>
Change things
</button>
</div>
));
getByText('Hello from the globalStore');
getByText('Change things').click();
getByText('Hello24 from the globalStore');
});