dobix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dobix might be problematic. Click here for more details.

package/CHANGELOG.md ADDED
@@ -0,0 +1,246 @@
1
+ # ChangeLog
2
+
3
+ ## [1.5.1](https://github.com/kcfe/dobux/compare/v1.5.0...v1.5.1) (2022-07-13)
4
+
5
+
6
+ ### 🐛 Bug Fixes
7
+
8
+ * auto reset ([df1ab53](https://github.com/kcfe/dobux/commit/df1ab53ba91e2aacce1fbe7410214bda5b15182a))
9
+
10
+
11
+
12
+
13
+
14
+ # [1.5.0](https://github.com/kcfe/dobux/compare/v1.4.3...v1.5.0) (2022-04-25)
15
+
16
+
17
+ ### ✨ Features
18
+
19
+ * support setValue reducer use immer ([73d10a0](https://github.com/kcfe/dobux/commit/73d10a050e0101481a21718a392a491f61e0d53c))
20
+
21
+
22
+ ### 🐛 Bug Fixes
23
+
24
+ * build in reducer ts type error ([692b110](https://github.com/kcfe/dobux/commit/692b1108942bd93adf234e031a1bb0a74ceff7d8))
25
+
26
+
27
+
28
+
29
+
30
+ ## [1.4.3](https://github.com/kcfe/dobux/compare/v1.4.2...v1.4.3) (2022-04-21)
31
+
32
+
33
+ ### 🐛 Bug Fixes
34
+
35
+ * build in set value reducer pass function ([c7cb196](https://github.com/kcfe/dobux/commit/c7cb196f569b1759e4f2c1801c90228e564b39d8))
36
+
37
+
38
+
39
+
40
+
41
+ ## [1.4.2](https://github.com/kcfe/dobux/compare/v1.4.2-beta.0...v1.4.2) (2021-09-09)
42
+
43
+ **Note:** Version bump only for package dobux
44
+
45
+ ## [1.4.2-beta.0](https://github.com/kcfe/dobux/compare/v1.4.1...v1.4.2-beta.0) (2021-09-08)
46
+
47
+
48
+ ### 🐛 Bug Fixes
49
+
50
+ * delete effect subscribe error ([b5ff249](https://github.com/kcfe/dobux/commit/b5ff249fa8ed137b07e24999a09815a961291794))
51
+
52
+
53
+
54
+
55
+
56
+ ## [1.4.1](https://github.com/kcfe/dobux/compare/v1.4.0...v1.4.1) (2021-09-06)
57
+
58
+
59
+ ### 🐛 Bug Fixes
60
+
61
+ * provider rerender ([88ddfef](https://github.com/kcfe/dobux/commit/88ddfefc7e5cb1bd2eb8b2b1d6664e8c86b9312c))
62
+
63
+
64
+
65
+
66
+
67
+ # [1.4.0](https://github.com/kcfe/dobux/compare/v1.3.1...v1.4.0) (2021-09-02)
68
+
69
+
70
+ ### ✨ Features
71
+
72
+ * add get effects ([d1378b1](https://github.com/kcfe/dobux/commit/d1378b1b4e4d7545fbbffbc77f4246599b3a3c84))
73
+ * add get reducers ([a41502f](https://github.com/kcfe/dobux/commit/a41502fc0985fdead603076e3cf37d446c2b61cb))
74
+
75
+
76
+
77
+
78
+
79
+ ## [1.3.1](https://github.com/kcfe/dobux/compare/v1.3.1-beta.0...v1.3.1) (2021-08-05)
80
+
81
+
82
+
83
+
84
+
85
+ ## [1.3.1-beta.0](https://github.com/kcfe/dobux/compare/v1.3.0...v1.3.1-beta.0) (2021-07-27)
86
+
87
+
88
+ ### 🐛 Bug Fixes
89
+
90
+ * modify state ([76c214f](https://github.com/kcfe/dobux/commit/76c214feb7d55e86f587e0c49fb30a40a8c58cd4))
91
+
92
+
93
+
94
+
95
+
96
+ # [1.3.0](https://github.com/kcfe/dobux/compare/v1.2.0...v1.3.0) (2021-07-12)
97
+
98
+
99
+ ### ✨ Features
100
+
101
+ * support setValue & setValues pass function ([7877f83](https://github.com/kcfe/dobux/commit/7877f83c9495ba49ff9c11727253859635e5ca03))
102
+
103
+
104
+
105
+
106
+
107
+ # [1.2.0](https://github.com/kcfe/dobux/compare/v1.1.0...v1.2.0) (2021-07-02)
108
+
109
+
110
+ ### ✨ Features
111
+
112
+ * add store.withModels to support multi models in Class component ([0915dc9](https://github.com/kcfe/dobux/commit/0915dc9076ee8eed147aa29f5cce9f5864bf1ce9))
113
+ * store.withModel support specific [contextName] ([3b1158e](https://github.com/kcfe/dobux/commit/3b1158e1ee24e6d1c5dc7ff44c89cada351d62e5))
114
+
115
+
116
+
117
+
118
+
119
+ # [1.1.0](https://github.com/kcfe/dobux/compare/v1.0.6...v1.1.0) (2021-06-04)
120
+
121
+
122
+ ### ⚡ Performance Improvements
123
+
124
+ * add hocs display name ([e96ddf3](https://github.com/kcfe/dobux/commit/e96ddf3487ae6c68ebc6d0c0d5e08d82b0ced35a))
125
+
126
+
127
+ ### ✨ Features
128
+
129
+ * support with provider forward ref, closes [#4](https://github.com/kcfe/dobux/issues/4) ([08d8bb0](https://github.com/kcfe/dobux/commit/08d8bb0ce40dfc94db577303e9105c01c76e3b35))
130
+
131
+
132
+ ### 🐛 Bug Fixes
133
+
134
+ * create model ts type ([118add3](https://github.com/kcfe/dobux/commit/118add310cecccccf677dff4e720cbf00e2a6992))
135
+ * with provider generic ([bdaf332](https://github.com/kcfe/dobux/commit/bdaf3321628ebfdc9825c0245de3aea7807d2115))
136
+
137
+
138
+
139
+
140
+
141
+ ## [1.0.6](https://github.com/kcfe/dobux/compare/v1.0.5...v1.0.6) (2021-04-14)
142
+
143
+
144
+ ### 🐛 Bug Fixes
145
+
146
+ * immer return value ([f3b3005](https://github.com/kcfe/dobux/commit/f3b3005ef4eb1c47c3d483fa38ef6f3bbc60f460))
147
+
148
+
149
+
150
+
151
+
152
+ ## [1.0.5](https://github.com/kcfe/dobux/compare/v1.0.5-beta.0...v1.0.5) (2021-04-09)
153
+
154
+
155
+
156
+
157
+
158
+ ## [1.0.5-beta.0](https://github.com/kcfe/dobux/compare/v1.0.4...v1.0.5-beta.0) (2021-04-01)
159
+
160
+
161
+
162
+
163
+
164
+ ## [1.0.4](https://github.com/kcfe/dobux/compare/v1.0.3...v1.0.4) (2021-03-31)
165
+
166
+
167
+ ### 🐛 Bug Fixes
168
+
169
+ * peer dependency ([978f1b4](https://github.com/kcfe/dobux/commit/978f1b4012bea884ae4e438e9f85aebe59925494))
170
+
171
+
172
+
173
+
174
+
175
+ ## [1.0.3](https://github.com/kcfe/dobux/compare/v1.0.2...v1.0.3) (2021-02-04)
176
+
177
+
178
+ ### 🐛 Bug Fixes
179
+
180
+ * redux devtool ([9559dea](https://github.com/kcfe/dobux/commit/9559dea790022bb1714a06b5d357919ad591e07b))
181
+ * ts overload ([81b5eef](https://github.com/kcfe/dobux/commit/81b5eef14ecc59aaaa575599c75063c4f00835a7))
182
+ * ts overload ([66a6b42](https://github.com/kcfe/dobux/commit/66a6b42f753e4e81dc6579ca7053a2d1ee21f1a8))
183
+
184
+
185
+
186
+
187
+
188
+ ## [1.0.2](https://github.com/kcfe/dobux/compare/v1.0.1...v1.0.2) (2021-01-15)
189
+
190
+
191
+ ### 🐛 Bug Fixes
192
+
193
+ * support strict mode ([9a82fda](https://github.com/kcfe/dobux/commit/9a82fda6c8c2e3cbf5c40bebc0f34a0d106f7ad5))
194
+
195
+
196
+
197
+
198
+
199
+ ## [1.0.1](https://github.com/kcfe/dobux/compare/v1.0.0...v1.0.1) (2021-01-02)
200
+
201
+
202
+
203
+
204
+
205
+ # [1.0.0](https://github.com/kwai-efe/dobux/compare/v0.0.1...v1.0.0) (2021-01-01)
206
+
207
+
208
+ ### ⚡ Performance Improvements
209
+
210
+ * only update when component depend effect loading ([cd2b6e0](https://github.com/kwai-efe/dobux/commit/cd2b6e0bba586180a4376e74ed374d61e3cb9705))
211
+
212
+
213
+ ### ✅ Tests
214
+
215
+ * getState ([52d5db8](https://github.com/kwai-efe/dobux/commit/52d5db861ebb029e6c669a3f2ec84b4ab48efa8e))
216
+ * increase coverage ([0339d5e](https://github.com/kwai-efe/dobux/commit/0339d5e20715fd6bea0f8ebdbae02fd9f5f3509f))
217
+ * multiple ([7f3eda0](https://github.com/kwai-efe/dobux/commit/7f3eda0865de792f03df7fdfe7c793702f5bd3fb))
218
+ * provider & index ([c4c6bea](https://github.com/kwai-efe/dobux/commit/c4c6bea16de1dd3303f0ceef0714c2225c0d6c15))
219
+ * reducer & effect ([c1c70b5](https://github.com/kwai-efe/dobux/commit/c1c70b53bc9803b095fd95b20ba11649e655cc18))
220
+ * store & model ([2daa59b](https://github.com/kwai-efe/dobux/commit/2daa59b8e4de9d1871e5cda3b57326f24ec1e654))
221
+ * utils ([6f87f2a](https://github.com/kwai-efe/dobux/commit/6f87f2a250bab27884b8a31a03d4690e093acd25))
222
+
223
+
224
+ ### ✨ Features
225
+
226
+ * add Container ([5e95d11](https://github.com/kwai-efe/dobux/commit/5e95d11b8455057f52a8307138416f668de8e57b))
227
+ * add createProvider ([e0381f5](https://github.com/kwai-efe/dobux/commit/e0381f57763ebe04bdaa4cc9ff381668e0a3ec87))
228
+ * add Provider & Consumer ([79ee803](https://github.com/kwai-efe/dobux/commit/79ee803edee50301be291a7edf676b623f18ca8b))
229
+ * add Store & Model ([e35e611](https://github.com/kwai-efe/dobux/commit/e35e6117312391c13be4b448681f0207f96c3e8c))
230
+ * add types ([dc06836](https://github.com/kwai-efe/dobux/commit/dc06836372704ad92e28d4ccf3385210cb2c658d))
231
+ * add utils ([026a26b](https://github.com/kwai-efe/dobux/commit/026a26b518fda377d0c7abd3f74de32b5f5b73c0))
232
+ * parse model ([f81dfa8](https://github.com/kwai-efe/dobux/commit/f81dfa8133158b7a26dfadafe47aa67ede250a0e))
233
+ * support auto reset when unmount ([339b650](https://github.com/kwai-efe/dobux/commit/339b65080fa4eb694d5230c455fe052a1bd05687))
234
+ * support getState ([794aca5](https://github.com/kwai-efe/dobux/commit/794aca5c7fcd8ce4ea56f9add02a39d5b76e73f5))
235
+ * support time travel ([9774e4b](https://github.com/kwai-efe/dobux/commit/9774e4bce23e71e0e71b0ee1e57e8b4a1bcbdde0))
236
+
237
+
238
+
239
+
240
+
241
+ ## 0.0.1 (2020-11-18)
242
+
243
+
244
+ ### ✨ Features
245
+
246
+ * init project ([7dce606](https://github.com/kwai-ad-fe/dobux/commit/7dce606e8a6be184e6004aba4340fac8ead83160))
package/LATESTLOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [Changes](https://github.com/kcfe/dobux/compare/v1.5.0...v1.5.1) (2022-07-13)
2
+
3
+
4
+ ### 🐛 Bug Fixes
5
+
6
+ * auto reset ([df1ab53](https://github.com/kcfe/dobux/commit/df1ab53ba91e2aacce1fbe7410214bda5b15182a))
7
+
8
+
9
+
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 kwai-ad-fe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ <p align="center" style="text-align: center; margin-left: -5px"><a href="https://kcfe.github.io/dobux/" target="_blank" rel="noopener noreferrer"><img width="100" src="https://static.yximgs.com/udata/pkg/ks-ad-fe/kcfe/dobux-simple-logo.png" alt="Dobux logo"></a></p>
2
+
3
+ <h2 align="center" style="text-align: center">Dobux</h2>
4
+
5
+ <p align="center" style="text-align: center">
6
+ Lightweight responsive state management solution based on React Context and React Hooks.
7
+ <br><br>
8
+ <a href="https://travis-ci.com/kcfe/dobux"><img src="https://travis-ci.com/kcfe/dobux.svg?branch=master" alt="Build Status"></a>
9
+ <a href="https://codecov.io/gh/kcfe/dobux"><img src="https://codecov.io/gh/kcfe/dobux/branch/master/graph/badge.svg" alt="Coverage Status"></a>
10
+ <a href="https://www.npmjs.com/package/dobux"><img src="https://img.shields.io/npm/v/dobux" alt="Version"></a>
11
+ <a href="https://www.npmjs.com/package/dobux"><img src="https://img.shields.io/npm/dm/dobux" alt="Downloads"></a>
12
+ <a href="https://img.shields.io/bundlephobia/minzip/dobux"><img src="https://img.shields.io/bundlephobia/minzip/dobux" alt="Bundle Size"></a>
13
+ <a href="https://github.com/kcfe/dobux/"><img src="https://img.shields.io/snyk/vulnerabilities/npm/dobux" alt="Vulnerabilities"></a>
14
+ <br>
15
+ <img src="https://img.shields.io/npm/dependency-version/dobux/peer/react" alt="Peer React">
16
+ <img src="https://img.shields.io/npm/dependency-version/dobux/peer/react-dom" alt="Peer React Dom">
17
+ </p>
18
+
19
+ English | [简体中文](./README.zh-CN.md)
20
+
21
+ ## ✨ Features
22
+
23
+ - **🎉 Simplify**:Only 3 core APIs, no additional learning cost, easy to get started with the knowledge of `React Hooks`.
24
+ - **🚀 Immutable**:Interact with view by simply modifying it while keeping all the benefits of immutable data.
25
+ - **🌲 Flexible Usage**:Support global and local data sources, manage the state of the entire application more elegantly.
26
+ - **🍳 Friendly Asynchronous Processing**:Record the loading status of asynchronous operations, simplify the presentation logic in the view layer.
27
+ - **🍬 TypeScript Support**:Complete `TypeScript` type definition, complete type checking and type inference can be obtained in the editor.
28
+
29
+ ## 📦 Installation
30
+
31
+ ```bash
32
+ // use npm
33
+ $ npm i dobux --save
34
+
35
+ // use yarn
36
+ $ yarn add dobux
37
+ ```
38
+
39
+ ## 🔨 Documents
40
+
41
+ - [Introduction](https://kcfe.github.io/dobux/guide)
42
+ - [Get Started](https://kcfe.github.io/dobux/guide/getting-started)
43
+ - [Best Practices](https://kcfe.github.io/dobux/guide/best-practice)
44
+ - [API](https://kcfe.github.io/dobux/api)
45
+ - [FAQ](https://kcfe.github.io/dobux/guide/faq)
46
+
47
+ ## 🔗 Examples
48
+
49
+ - [Simple counter](https://kcfe.github.io/dobux/guide/examples#简单的计数器)
50
+ - [Todo List](https://kcfe.github.io/dobux/guide/examples#待办事项清单)
51
+
52
+ ## 🖥 Version dependency
53
+
54
+ - React >= 16.8.0
55
+ - ReactDOM >= 16.8.0
56
+
57
+ ## 📄 LICENSE
58
+
59
+ [MIT](https://github.com/kcfe/dobux/blob/master/LICENSE)
@@ -0,0 +1,59 @@
1
+ <p align="center" style="text-align: center; margin-left: -5px"><a href="https://kcfe.github.io/dobux/" target="_blank" rel="noopener noreferrer"><img width="100" src="https://static.yximgs.com/udata/pkg/ks-ad-fe/kcfe/dobux-simple-logo.png" alt="Dobux logo"></a></p>
2
+
3
+ <h2 align="center" style="text-align: center">Dobux</h2>
4
+
5
+ <p align="center" style="text-align: center">
6
+ 基于 React Context 和 React Hooks 的轻量级响应式状态管理方案。
7
+ <br><br>
8
+ <a href="https://travis-ci.com/kcfe/dobux"><img src="https://travis-ci.com/kcfe/dobux.svg?branch=master" alt="Build Status"></a>
9
+ <a href="https://codecov.io/gh/kcfe/dobux"><img src="https://codecov.io/gh/kcfe/dobux/branch/master/graph/badge.svg" alt="Coverage Status"></a>
10
+ <a href="https://www.npmjs.com/package/dobux"><img src="https://img.shields.io/npm/v/dobux" alt="Version"></a>
11
+ <a href="https://www.npmjs.com/package/dobux"><img src="https://img.shields.io/npm/dm/dobux" alt="Downloads"></a>
12
+ <a href="https://img.shields.io/bundlephobia/minzip/dobux"><img src="https://img.shields.io/bundlephobia/minzip/dobux" alt="Bundle Size"></a>
13
+ <a href="https://github.com/kcfe/dobux/"><img src="https://img.shields.io/snyk/vulnerabilities/npm/dobux" alt="Vulnerabilities"></a>
14
+ <br>
15
+ <img src="https://img.shields.io/npm/dependency-version/dobux/peer/react" alt="Peer React">
16
+ <img src="https://img.shields.io/npm/dependency-version/dobux/peer/react-dom" alt="Peer React Dom">
17
+ </p>
18
+
19
+ [English](./README.md) | 简体中文
20
+
21
+ ## ✨ 特性
22
+
23
+ - **🎉 简单易用**:仅有 3 个核心 API,无需额外的学习成本,只需要了解 `React Hooks`
24
+ - **🚀 不可变数据**:通过简单地修改数据与视图交互,同时保留不可变数据的特性
25
+ - **🌲 灵活的使用方式**:支持全局和局部数据源,更优雅的管理整个应用的状态
26
+ - **🍳 友好的异步处理**:记录异步操作的加载状态,简化了视图层中的呈现逻辑
27
+ - **🍬 TypeScript 支持**:完整的 `TypeScript` 类型定义,在编辑器中能获得完整的类型检查和类型推断
28
+
29
+ ## 📦 安装
30
+
31
+ ```bash
32
+ // 使用 npm
33
+ $ npm i dobux --save
34
+
35
+ // 使用 yarn
36
+ $ yarn add dobux
37
+ ```
38
+
39
+ ## 🔨 文档
40
+
41
+ - [介绍](https://kcfe.github.io/dobux/guide)
42
+ - [快速上手](https://kcfe.github.io/dobux/guide/getting-started)
43
+ - [最佳实践](https://kcfe.github.io/dobux/guide/best-practice)
44
+ - [API](https://kcfe.github.io/dobux/api)
45
+ - [FAQ](https://kcfe.github.io/dobux/guide/faq)
46
+
47
+ ## 🔗 示例
48
+
49
+ - [简单计数器](https://kcfe.github.io/dobux/guide/examples#简单的计数器)
50
+ - [待办事项清单](https://kcfe.github.io/dobux/guide/examples#待办事项清单)
51
+
52
+ ## 🖥 版本依赖
53
+
54
+ - React >= 16.8.0
55
+ - ReactDOM >= 16.8.0
56
+
57
+ ## 📄 LICENSE
58
+
59
+ [MIT](https://github.com/kcfe/dobux/blob/master/LICENSE)
@@ -0,0 +1,108 @@
1
+ import React from 'react'
2
+ import { createStore } from '../src/index'
3
+ import { counter } from './helper/model'
4
+ import { createHook } from './helper/createHook'
5
+ import { Counter, Counter1 } from './helper/CountFunctionComponent'
6
+ import { act, fireEvent, render } from '@testing-library/react'
7
+ import { store } from './helper/store'
8
+
9
+ console.error = jest.fn()
10
+
11
+ jest.setTimeout(10000)
12
+
13
+ describe('effect test', () => {
14
+ it('effect should be async function', () => {
15
+ const store = createStore({
16
+ counter,
17
+ })
18
+ const { Provider, useModel } = store
19
+
20
+ const { result } = createHook(Provider, useModel, 'counter')
21
+
22
+ const increaseAsync = result.current.effects.increaseAsync
23
+
24
+ expect(typeof increaseAsync().then).toBe('function')
25
+ })
26
+
27
+ it('effect should have expected field', () => {
28
+ const store = createStore({
29
+ counter,
30
+ })
31
+ const { Provider, useModel } = store
32
+
33
+ const { result } = createHook(Provider, useModel, 'counter')
34
+
35
+ const increaseAsync = result.current.effects.increaseAsync
36
+
37
+ expect(increaseAsync.loading).toBe(false)
38
+ expect(increaseAsync.identifier).toBe(0)
39
+ })
40
+
41
+ it('effect should throw error when has error', () => {
42
+ const store = createStore({
43
+ counter,
44
+ })
45
+ const { Provider, useModel } = store
46
+
47
+ const { result } = createHook(Provider, useModel, 'counter')
48
+
49
+ const fetchError = result.current.effects.fetchError
50
+
51
+ return fetchError().catch((err: any) => {
52
+ expect(err).toBe('customer error')
53
+ })
54
+ })
55
+
56
+ it('should set loading to true when the effect is executed, and after execution, it should be set to false', async () => {
57
+ const store = createStore({
58
+ counter,
59
+ })
60
+ const { Provider, useModel } = store
61
+
62
+ const { result, waitForNextUpdate } = createHook(Provider, useModel, 'counter')
63
+
64
+ const increaseAsync = result.current.effects.increaseAsync
65
+
66
+ expect(increaseAsync.loading).toBe(false)
67
+ increaseAsync()
68
+ expect(increaseAsync.loading).toBe(true)
69
+
70
+ await waitForNextUpdate()
71
+
72
+ expect(increaseAsync.loading).toBe(false)
73
+ expect(result.current.state.count).toBe(1)
74
+ })
75
+
76
+ it('should only rerender when Component depend effect loading', done => {
77
+ const CounterRender = jest.fn()
78
+ const Counter1Render = jest.fn()
79
+
80
+ // https://spectrum.chat/testing-library/help/is-there-a-way-to-count-the-number-of-times-a-component-gets-rendered~8b8b3f8f-775d-49cc-80fd-baaf40fa37eb
81
+ const { getByTestId, queryByText } = render(
82
+ <store.Provider>
83
+ <Counter onRender={CounterRender} />
84
+ <Counter1 onRender={Counter1Render} />
85
+ </store.Provider>
86
+ )
87
+
88
+ expect(CounterRender).toBeCalledTimes(1)
89
+ expect(Counter1Render).toBeCalledTimes(1)
90
+
91
+ expect(queryByText('Loading ...')).not.toBeInTheDocument()
92
+
93
+ act(() => {
94
+ fireEvent.click(getByTestId('increaseAsync'))
95
+ })
96
+
97
+ expect(CounterRender).toBeCalledTimes(2)
98
+ expect(Counter1Render).toBeCalledTimes(1)
99
+
100
+ expect(queryByText('Loading ...')).toBeInTheDocument()
101
+
102
+ setTimeout(() => {
103
+ expect(CounterRender).toBeCalledTimes(4)
104
+ expect(Counter1Render).toBeCalledTimes(2)
105
+ done()
106
+ }, 1000)
107
+ })
108
+ })
@@ -0,0 +1,65 @@
1
+ import { act } from '@testing-library/react-hooks'
2
+ import { createStore } from '../src/index'
3
+ import { counter } from './helper/model'
4
+ import { createHook } from './helper/createHook'
5
+
6
+ describe('getEffects test', () => {
7
+ it('call getEffects with no parameter should return all model effects', async () => {
8
+ const store = createStore({
9
+ counter,
10
+ })
11
+ const { Provider, useModel } = store
12
+
13
+ const { result } = createHook(Provider, useModel, 'counter')
14
+
15
+ const effects = store.getEffects()
16
+
17
+ expect(effects).toHaveProperty('counter')
18
+
19
+ await act(async () => {
20
+ await effects.counter.increaseAsync()
21
+ })
22
+
23
+ expect(result.current.state.count).toBe(1)
24
+
25
+ await act(async () => {
26
+ await effects.counter.decreaseAsync()
27
+ })
28
+ expect(result.current.state.count).toBe(0)
29
+ })
30
+
31
+ it('call getEffects with parameter should return specific model state', async () => {
32
+ const store = createStore({
33
+ counter,
34
+ })
35
+ const { Provider, useModel } = store
36
+
37
+ const { result } = createHook(Provider, useModel, 'counter')
38
+
39
+ const effects = store.getEffects('counter')
40
+
41
+ expect(typeof effects.increaseAsync === 'function').toBeTruthy()
42
+ expect(typeof effects.decreaseAsync === 'function').toBeTruthy()
43
+
44
+ await act(async () => {
45
+ await effects.increaseAsync()
46
+ })
47
+ expect(result.current.state.count).toBe(1)
48
+
49
+ await act(async () => {
50
+ await effects.decreaseAsync()
51
+ })
52
+ expect(result.current.state.count).toBe(0)
53
+ })
54
+
55
+ it('call getEffects with not exist parameter should throw error', () => {
56
+ const store = createStore({
57
+ counter,
58
+ })
59
+
60
+ // @ts-ignore
61
+ expect(() => store.getEffects('counter1')).toThrow(
62
+ 'Invariant Failed: [store.getEffects] Expected the modelName to be one of counter, but got counter1'
63
+ )
64
+ })
65
+ })
@@ -0,0 +1,67 @@
1
+ import { act } from '@testing-library/react-hooks'
2
+ import { createStore } from '../src/index'
3
+ import { counter } from './helper/model'
4
+ import { createHook } from './helper/createHook'
5
+
6
+ describe('getReducers test', () => {
7
+ it('call getReducers with no parameter should return all model reducers', () => {
8
+ const store = createStore({
9
+ counter,
10
+ })
11
+ const { Provider, useModel } = store
12
+
13
+ const { result } = createHook(Provider, useModel, 'counter')
14
+
15
+ const reducers = store.getReducers()
16
+
17
+ expect(reducers).toHaveProperty('counter')
18
+
19
+ act(() => {
20
+ reducers.counter.increase()
21
+ })
22
+ expect(result.current.state.count).toBe(1)
23
+
24
+ act(() => {
25
+ reducers.counter.decrease()
26
+ })
27
+ expect(result.current.state.count).toBe(0)
28
+ })
29
+
30
+ it('call getReducers with parameter should return specific model state', () => {
31
+ const store = createStore({
32
+ counter,
33
+ })
34
+ const { Provider, useModel } = store
35
+
36
+ const { result } = createHook(Provider, useModel, 'counter')
37
+
38
+ const reducers = store.getReducers('counter')
39
+
40
+ expect(typeof reducers.increase === 'function').toBeTruthy()
41
+ expect(typeof reducers.decrease === 'function').toBeTruthy()
42
+ expect(typeof reducers.setValue === 'function').toBeTruthy()
43
+ expect(typeof reducers.setValues === 'function').toBeTruthy()
44
+ expect(typeof reducers.reset === 'function').toBeTruthy()
45
+
46
+ act(() => {
47
+ reducers.increase()
48
+ })
49
+ expect(result.current.state.count).toBe(1)
50
+
51
+ act(() => {
52
+ reducers.decrease()
53
+ })
54
+ expect(result.current.state.count).toBe(0)
55
+ })
56
+
57
+ it('call getReducers with not exist parameter should throw error', () => {
58
+ const store = createStore({
59
+ counter,
60
+ })
61
+
62
+ // @ts-ignore
63
+ expect(() => store.getReducers('counter1')).toThrow(
64
+ 'Invariant Failed: [store.getReducers] Expected the modelName to be one of counter, but got counter1'
65
+ )
66
+ })
67
+ })