Skip to content

Commit c4c6bea

Browse files
committed
test: provider & index
1 parent 2daa59b commit c4c6bea

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

test/index.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { createStore, createModel } from '../src'
2+
import { Store } from '../src/core/Store'
3+
import { config, defaultStoreOptions } from './helper/shared'
4+
5+
describe('entry test', () => {
6+
it('createStore should be defined', () => {
7+
expect(createStore).toBeDefined()
8+
})
9+
10+
it('should return the instance of Store when call createStore', () => {
11+
const store = createStore(
12+
{
13+
counter: config,
14+
},
15+
defaultStoreOptions
16+
)
17+
18+
expect(store).toBeInstanceOf(Store)
19+
})
20+
21+
it('createModel should be defined', () => {
22+
expect(createModel).toBeDefined()
23+
})
24+
25+
it('should return the default value when config is invalid', () => {
26+
const store = createModel()({
27+
state: {
28+
count: 0,
29+
},
30+
})
31+
32+
expect(store.state).toEqual({ count: 0 })
33+
expect(store.reducers).toEqual({})
34+
expect(typeof store.effects).toBe('function')
35+
})
36+
})

test/provider.spec.tsx

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import React from 'react'
2+
import { render } from '@testing-library/react'
3+
import { act } from '@testing-library/react-hooks'
4+
import { createStore } from '../src/index'
5+
import { counter } from './helper/model'
6+
import { createHook } from './helper/createHook'
7+
import { Counter } from './helper/CountClassComponent'
8+
9+
describe('Provider test', () => {
10+
it('should render correct when use provider in component', () => {
11+
const store = createStore({
12+
counter,
13+
})
14+
const { Provider, withProvider } = store
15+
16+
const Component: React.FC = () => <div></div>
17+
18+
expect(() => {
19+
render(
20+
<Provider>
21+
<Component />
22+
</Provider>
23+
)
24+
}).not.toThrow()
25+
26+
const WithProvider = withProvider(Component)
27+
28+
expect(() => {
29+
render(<WithProvider />)
30+
}).not.toThrow()
31+
})
32+
33+
it('should throw error when call useModel without Provider wrapper', () => {
34+
const originalError = console.error
35+
console.error = jest.fn()
36+
37+
const store = createStore({
38+
counter,
39+
})
40+
const { useModel } = store
41+
42+
const ErrorCounter: React.FC = () => {
43+
const { state } = useModel('counter')
44+
return <div>{state.count}</div>
45+
}
46+
47+
expect(() => render(<ErrorCounter />)).toThrow()
48+
49+
console.error = originalError
50+
})
51+
52+
it('should add the store to function component context', () => {
53+
const store = createStore({
54+
counter,
55+
})
56+
const { Provider, useModel } = store
57+
58+
const {
59+
result: { current },
60+
} = createHook(Provider, useModel, 'counter')
61+
62+
expect(current.state.count).toBe(0)
63+
expect(current.reducers).toBeDefined()
64+
expect(current.effects).toBeDefined()
65+
})
66+
67+
it('should add the store to class component context', () => {
68+
const store = createStore({
69+
counter,
70+
})
71+
const { Provider, withModel } = store
72+
73+
const Component = withModel('counter')(Counter)
74+
75+
const wrapper = render(
76+
<Provider>
77+
<Component />
78+
</Provider>
79+
)
80+
81+
expect(wrapper.getByTestId('count').innerHTML).toBe('0')
82+
})
83+
84+
it('should not reset store when component unmount', async () => {
85+
const store = createStore({
86+
counter,
87+
})
88+
const { Provider, useModel } = store
89+
const { result, unmount } = createHook(Provider, useModel, 'counter')
90+
91+
act(() => {
92+
expect(result.current.state.count).toBe(0)
93+
result.current.reducers.increase()
94+
})
95+
96+
expect(result.current.state.count).toBe(1)
97+
98+
unmount()
99+
100+
const {
101+
result: { current },
102+
} = createHook(Provider, useModel, 'counter')
103+
104+
expect(current.state.count).toBe(1)
105+
})
106+
107+
it('setting autoReset to true, model should be reset when the component unmount', async () => {
108+
const store = createStore({
109+
counter,
110+
}, {
111+
autoReset: true
112+
})
113+
const { Provider, useModel } = store
114+
const { result, unmount } = createHook(Provider, useModel, 'counter')
115+
116+
act(() => {
117+
expect(result.current.state.count).toBe(0)
118+
result.current.reducers.increase()
119+
})
120+
121+
expect(result.current.state.count).toBe(1)
122+
123+
unmount()
124+
125+
const {
126+
result: { current },
127+
} = createHook(Provider, useModel, 'counter')
128+
129+
expect(current.state.count).toBe(0)
130+
})
131+
132+
it('setting autoReset to specify model, should be reset when the component unmount', async () => {
133+
const store = createStore({
134+
counter,
135+
}, {
136+
autoReset: ['counter']
137+
})
138+
const { Provider, useModel } = store
139+
const { result, unmount } = createHook(Provider, useModel, 'counter')
140+
141+
act(() => {
142+
expect(result.current.state.count).toBe(0)
143+
result.current.reducers.increase()
144+
})
145+
146+
expect(result.current.state.count).toBe(1)
147+
148+
unmount()
149+
150+
const {
151+
result: { current },
152+
} = createHook(Provider, useModel, 'counter')
153+
154+
expect(current.state.count).toBe(0)
155+
})
156+
})

0 commit comments

Comments
 (0)