Skip to content

Commit 0b0aedf

Browse files
committed
feat(model): refactor model initialize method
Model() can accept single model
1 parent 2e51b26 commit 0b0aedf

File tree

12 files changed

+309
-140
lines changed

12 files changed

+309
-140
lines changed

__test__/Model/mixed.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// <reference path="../index.d.ts" />
2+
import { testHook } from 'react-hooks-testing-library'
3+
import { Counter } from '..'
4+
import { Model } from '../../src'
5+
6+
describe('useStore', () => {
7+
test('create by single model definition', async () => {
8+
let state: any
9+
let actions: any
10+
let count = 0
11+
const Home = Model(Counter)
12+
const { useStore, subscribe, unsubscribe } = Model({ Home })
13+
testHook(() => {
14+
;[state, actions] = useStore('Home')
15+
})
16+
expect(state).toEqual({ count: 0 })
17+
await actions.increment(3)
18+
expect(state).toEqual({ count: 3 })
19+
// test subscribe
20+
subscribe('Home', 'increment', () => (count += 1))
21+
await actions.increment(4)
22+
expect(count).toBe(1)
23+
expect(state.count).toBe(7)
24+
// test unsubscribe
25+
unsubscribe('Home', 'increment')
26+
await actions.increment(3)
27+
expect(state.count).toBe(10)
28+
expect(count).toBe(1)
29+
})
30+
})

__test__/Model/single.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference path="../index.d.ts" />
2+
import { testHook } from 'react-hooks-testing-library'
3+
import { Counter } from '..'
4+
import { Model } from '../../src'
5+
6+
describe('useStore', () => {
7+
test('create by single model definition', async () => {
8+
let state: any
9+
let actions: any
10+
let count = 0
11+
const { useStore, subscribe, unsubscribe } = Model(Counter)
12+
testHook(() => {
13+
;[state, actions] = useStore()
14+
})
15+
expect(state).toEqual({ count: 0 })
16+
await actions.increment(3)
17+
expect(state).toEqual({ count: 3 })
18+
// test subscribe
19+
subscribe('increment', () => (count += 1))
20+
await actions.increment(4)
21+
expect(count).toBe(1)
22+
expect(state.count).toBe(7)
23+
// test unsubscribe
24+
unsubscribe('increment')
25+
await actions.increment(3)
26+
expect(state.count).toBe(10)
27+
expect(count).toBe(1)
28+
})
29+
})

__test__/class/class.spec.tsx

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as React from 'react'
44
import { Model, Provider, connect } from '../../src'
55
import { Counter } from '../index'
66
import { render, fireEvent } from 'react-testing-library'
7-
import { testHook } from 'react-hooks-testing-library'
87
import { timeout } from '../../src/helper'
98

109
const Button = connect(
@@ -51,22 +50,4 @@ describe('class component', () => {
5150
await timeout(100, {}) // Wait Consumer rerender
5251
expect(button!.textContent).toBe('3')
5352
})
54-
test('communicator', async () => {
55-
let state: any
56-
const { useStore } = Model({ Counter })
57-
testHook(() => {
58-
;[state] = useStore('Counter')
59-
})
60-
const { container } = render(
61-
<Provider>
62-
<Button />
63-
</Provider>
64-
)
65-
const button: any = container.firstChild
66-
expect(button!.textContent).toBe('0')
67-
fireEvent.click(button)
68-
await timeout(100, {}) // Wait Consumer rerender
69-
expect(button!.textContent).toBe('3')
70-
expect(state.count).toBe(3)
71-
})
7253
})
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// <reference path="../index.d.ts" />
2+
import 'react-testing-library/cleanup-after-each'
3+
import * as React from 'react'
4+
import { Model, Provider, connect } from '../../src'
5+
import { Counter } from '../index'
6+
import { render, fireEvent } from 'react-testing-library'
7+
import { testHook } from 'react-hooks-testing-library'
8+
import { timeout } from '../../src/helper'
9+
10+
const Button = connect(
11+
'Counter',
12+
(props: any) => props
13+
)(
14+
class extends React.PureComponent<any> {
15+
render() {
16+
const { state, actions } = this.props
17+
return (
18+
<button
19+
onClick={() => {
20+
actions.increment(3).catch((e: any) => console.error(e))
21+
}}
22+
>
23+
{state.count}
24+
</button>
25+
)
26+
}
27+
}
28+
)
29+
30+
describe('class component', () => {
31+
test('communicator', async () => {
32+
let state: any
33+
const { useStore } = Model({ Counter })
34+
testHook(() => {
35+
;[state] = useStore('Counter')
36+
})
37+
const { container } = render(
38+
<Provider>
39+
<Button />
40+
</Provider>
41+
)
42+
const button: any = container.firstChild
43+
expect(button!.textContent).toBe('0')
44+
fireEvent.click(button)
45+
await timeout(100, {}) // Wait Consumer rerender
46+
expect(button!.textContent).toBe('3')
47+
expect(state.count).toBe(3)
48+
})
49+
})

__test__/getActions.spec.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="./index.d.ts" />
22
import { testHook } from 'react-hooks-testing-library'
3+
import { Counter } from '.'
34
import { Model } from '../src'
4-
import { Counter, AsyncCounter } from '.'
55

66
describe('useStore', () => {
77
test('return default initial values', () => {
@@ -25,22 +25,4 @@ describe('useStore', () => {
2525
await actions.increment(4)
2626
expect(state.count).toBe(7)
2727
})
28-
test('consumer actions return Partial<State>', async () => {
29-
let state: any
30-
let actions: any
31-
const { useStore, getActions } = Model({ Counter })
32-
testHook(() => {
33-
;[state] = useStore('Counter')
34-
actions = getActions('Counter')
35-
})
36-
await actions.add(3)
37-
expect(state).toEqual({ count: 3 })
38-
})
39-
test('use initialModels', async () => {
40-
const { getInitialState } = Model({ AsyncCounter })
41-
const initialModels = await getInitialState()
42-
const { getState } = Model({ AsyncCounter }, initialModels)
43-
const state = getState('AsyncCounter')
44-
expect(state.count).toBe(1)
45-
})
4628
})

__test__/useStore.spec.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

__test__/useStore/actions.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="../index.d.ts" />
2+
import { testHook } from 'react-hooks-testing-library'
3+
import { Counter } from '..'
4+
import { Model } from '../../src'
5+
6+
describe('useStore', () => {
7+
test('consumer actions return Partial<State>', async () => {
8+
let state: any
9+
let actions: any
10+
const { useStore } = Model({ Counter })
11+
testHook(() => {
12+
;[state, actions] = useStore('Counter')
13+
})
14+
await actions.add(3)
15+
expect(state).toEqual({ count: 3 })
16+
})
17+
})

__test__/useStore/initial.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path="../index.d.ts" />
2+
import { testHook } from 'react-hooks-testing-library'
3+
import { Model } from '../../src'
4+
import { Counter } from '..'
5+
6+
describe('useStore', () => {
7+
test('return default initial values', () => {
8+
let state
9+
const { useStore } = Model({ Counter })
10+
testHook(() => {
11+
;[state] = useStore('Counter')
12+
})
13+
expect(state).toEqual({ count: 0 })
14+
})
15+
test('consumer actions return function', async () => {
16+
let state: any
17+
let actions: any
18+
const { useStore } = Model({ Counter })
19+
testHook(() => {
20+
;[state, actions] = useStore('Counter')
21+
})
22+
await actions.increment(3)
23+
expect(state).toEqual({ count: 3 })
24+
await actions.increment(4)
25+
expect(state.count).toBe(7)
26+
})
27+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="../index.d.ts" />
2+
import { AsyncCounter } from '..'
3+
import { Model } from '../../src'
4+
5+
describe('useStore', () => {
6+
test('use initialModels', async () => {
7+
const { getInitialState } = Model({ AsyncCounter })
8+
const initialModels = await getInitialState()
9+
const { getState } = Model({ AsyncCounter }, initialModels)
10+
const state = getState('AsyncCounter')
11+
expect(state.count).toBe(1)
12+
})
13+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"remark-preset-lint-recommended": "^3.0.2",
4545
"ts-jest": "^24.0.0",
4646
"tslint": "^5.14.0",
47-
"typescript": "^3.3.1"
47+
"typescript": "^3.3.4000"
4848
},
4949
"husky": {
5050
"hooks": {

0 commit comments

Comments
 (0)