Skip to content

Commit 7f481ad

Browse files
committed
test(middleware): add test for model's customize middlewares
1 parent a09c1c5 commit 7f481ad

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

__test__/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type CounterActionParams = {
1515
increment: number
1616
}
1717

18+
type NextCounterActionParams = {
19+
increment: number
20+
add: number
21+
}
22+
1823
type ExtraActionParams = {
1924
add: number
2025
}

__test__/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/// <reference path="./index.d.ts" />
22
/// <reference path="../src/index.d.ts" />
3+
import { Model } from '../src'
34
import { timeout } from '../src/helper'
5+
import { actionMiddlewares } from '../src/middlewares'
46

57
export const ActionsTester: ModelType<ActionTesterState, ActionTesterParams> = {
68
state: {
@@ -124,3 +126,28 @@ export const ErrorCounter: ModelType<CounterState, CounterActionParams> = {
124126
}
125127
}
126128
}
129+
130+
const delayMiddleware: Middleware = async (context, restMiddlewares) => {
131+
await timeout(2000, {})
132+
context.next(restMiddlewares)
133+
}
134+
135+
const nextCounterModel: NextModelType<CounterState, NextCounterActionParams> = {
136+
actions: {
137+
add: num => {
138+
return state => {
139+
state.count += num
140+
}
141+
},
142+
increment: async (num, { actions }) => {
143+
actions.add(num)
144+
await timeout(300, {})
145+
}
146+
},
147+
middlewares: [delayMiddleware, ...actionMiddlewares],
148+
state: {
149+
count: 0
150+
}
151+
}
152+
153+
export const NextCounterModel = Model(nextCounterModel)

__test__/middlewares/model.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="../index.d.ts" />
2+
import 'react-testing-library/cleanup-after-each'
3+
process.env.NODE_ENV = 'production'
4+
import { testHook } from 'react-hooks-testing-library'
5+
import { NextCounterModel } from '..'
6+
import { Model } from '../../src'
7+
8+
describe('NextModel', () => {
9+
test("allows you to customize model's middleware", async () => {
10+
let actions: any
11+
let state: any
12+
const { useStore, getActions } = Model({ NextCounterModel })
13+
const beginTime = Date.now()
14+
testHook(() => {
15+
;[state, actions] = useStore('NextCounterModel')
16+
})
17+
await actions.increment(2)
18+
await getActions('NextCounterModel').increment(1)
19+
expect(Date.now() - beginTime > 300)
20+
expect(state.count).toBe(3)
21+
})
22+
})

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-model",
3-
"version": "2.7.1",
3+
"version": "2.7.1-alpha.2",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
66
"umd:main": "./dist/react-model.umd.js",
@@ -13,7 +13,12 @@
1313
"test": "jest --silent",
1414
"test:coverage": "jest --collect-coverage --silent"
1515
},
16-
"keywords": ["react", "model", "state-management", "react-hooks"],
16+
"keywords": [
17+
"react",
18+
"model",
19+
"state-management",
20+
"react-hooks"
21+
],
1722
"author": "ArrayZoneYour <hustliyidong@gmail.com>",
1823
"license": "MIT",
1924
"dependencies": {
@@ -44,7 +49,7 @@
4449
"remark-preset-lint-recommended": "^3.0.2",
4550
"ts-jest": "^24.0.0",
4651
"tslint": "^5.14.0",
47-
"typescript": "^3.3.4000"
52+
"typescript": "^3.4.5"
4853
},
4954
"husky": {
5055
"hooks": {

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function Model<M extends Models, MT extends NextModelType>(
8080
}
8181
Global.Actions[name] = Global.Actions[model.__id]
8282
Global.AsyncState[name] = Global.AsyncState[model.__id]
83+
Global.Middlewares[name] = Global.Middlewares[model.__id]
8384
}
8485
})
8586

0 commit comments

Comments
 (0)