Skip to content

Commit

Permalink
Merge pull request #92 from byte-fe/feat/Model
Browse files Browse the repository at this point in the history
feat(model): refactor model initialize method
  • Loading branch information
leecade authored Apr 10, 2019
2 parents 2e51b26 + 12fce12 commit b5e82af
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 142 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ coverage/
.rts2_cache_umd/

# Github
.github/
.github/
.git/
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ npm install react-model
- [FAQ](#faq)
- [How can I disable the console debugger?](#how-can-i-disable-the-console-debugger)
- [How can I add custom middleware](#how-can-i-add-custom-middleware)
- [How can I make persist models](#how-can-i-make-persist-models)

## Core Concept

Expand Down Expand Up @@ -655,3 +656,21 @@ export default Model(stores)
```
[⇧ back to top](#table-of-contents)
#### How can I make persist models
```typescript
import { actionMiddlewares, Model } from 'react-model'
import Example from 'models/example'

const persistMiddleware: Middleware = async (context, restMiddlewares) => {
localStorage.setItem('__REACT_MODEL__', JSON.stringify(context.Global.State))
await context.next(restMiddlewares)
}

actionMiddlewares.push(persistMiddleware)

Model({ Example }, JSON.parse(localStorage.getItem('__REACT_MODEL__')))
```
[⇧ back to top](#table-of-contents)
30 changes: 30 additions & 0 deletions __test__/Model/mixed.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference path="../index.d.ts" />
import { testHook } from 'react-hooks-testing-library'
import { NextCounter } from '..'
import { Model } from '../../src'

describe('useStore', () => {
test('create by single model definition', async () => {
let state: any
let actions: any
let count = 0
const Home = Model(NextCounter)
const { useStore, subscribe, unsubscribe } = Model({ Home })
testHook(() => {
;[state, actions] = useStore('Home')
})
expect(state).toEqual({ count: 0 })
await actions.increment(3)
expect(state).toEqual({ count: 3 })
// test subscribe
subscribe('Home', 'increment', () => (count += 1))
await actions.increment(4)
expect(count).toBe(1)
expect(state.count).toBe(7)
// test unsubscribe
unsubscribe('Home', 'increment')
await actions.increment(3)
expect(state.count).toBe(10)
expect(count).toBe(1)
})
})
29 changes: 29 additions & 0 deletions __test__/Model/single.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path="../index.d.ts" />
import { testHook } from 'react-hooks-testing-library'
import { NextCounter } from '..'
import { Model } from '../../src'

describe('useStore', () => {
test('create by single model definition', async () => {
let state: any
let actions: any
let count = 0
const { useStore, subscribe, unsubscribe } = Model(NextCounter)
testHook(() => {
;[state, actions] = useStore()
})
expect(state).toEqual({ count: 0 })
await actions.increment(3)
expect(state).toEqual({ count: 3 })
// test subscribe
subscribe('increment', () => (count += 1))
await actions.increment(4)
expect(count).toBe(1)
expect(state.count).toBe(7)
// test unsubscribe
unsubscribe('increment')
await actions.increment(3)
expect(state.count).toBe(10)
expect(count).toBe(1)
})
})
19 changes: 0 additions & 19 deletions __test__/class/class.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as React from 'react'
import { Model, Provider, connect } from '../../src'
import { Counter } from '../index'
import { render, fireEvent } from 'react-testing-library'
import { testHook } from 'react-hooks-testing-library'
import { timeout } from '../../src/helper'

const Button = connect(
Expand Down Expand Up @@ -51,22 +50,4 @@ describe('class component', () => {
await timeout(100, {}) // Wait Consumer rerender
expect(button!.textContent).toBe('3')
})
test('communicator', async () => {
let state: any
const { useStore } = Model({ Counter })
testHook(() => {
;[state] = useStore('Counter')
})
const { container } = render(
<Provider>
<Button />
</Provider>
)
const button: any = container.firstChild
expect(button!.textContent).toBe('0')
fireEvent.click(button)
await timeout(100, {}) // Wait Consumer rerender
expect(button!.textContent).toBe('3')
expect(state.count).toBe(3)
})
})
49 changes: 49 additions & 0 deletions __test__/class/communicator.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference path="../index.d.ts" />
import 'react-testing-library/cleanup-after-each'
import * as React from 'react'
import { Model, Provider, connect } from '../../src'
import { Counter } from '../index'
import { render, fireEvent } from 'react-testing-library'
import { testHook } from 'react-hooks-testing-library'
import { timeout } from '../../src/helper'

const Button = connect(
'Counter',
(props: any) => props
)(
class extends React.PureComponent<any> {
render() {
const { state, actions } = this.props
return (
<button
onClick={() => {
actions.increment(3).catch((e: any) => console.error(e))
}}
>
{state.count}
</button>
)
}
}
)

describe('class component', () => {
test('communicator', async () => {
let state: any
const { useStore } = Model({ Counter })
testHook(() => {
;[state] = useStore('Counter')
})
const { container } = render(
<Provider>
<Button />
</Provider>
)
const button: any = container.firstChild
expect(button!.textContent).toBe('0')
fireEvent.click(button)
await timeout(100, {}) // Wait Consumer rerender
expect(button!.textContent).toBe('3')
expect(state.count).toBe(3)
})
})
20 changes: 1 addition & 19 deletions __test__/getActions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="./index.d.ts" />
import { testHook } from 'react-hooks-testing-library'
import { Counter } from '.'
import { Model } from '../src'
import { Counter, AsyncCounter } from '.'

describe('useStore', () => {
test('return default initial values', () => {
Expand All @@ -25,22 +25,4 @@ describe('useStore', () => {
await actions.increment(4)
expect(state.count).toBe(7)
})
test('consumer actions return Partial<State>', async () => {
let state: any
let actions: any
const { useStore, getActions } = Model({ Counter })
testHook(() => {
;[state] = useStore('Counter')
actions = getActions('Counter')
})
await actions.add(3)
expect(state).toEqual({ count: 3 })
})
test('use initialModels', async () => {
const { getInitialState } = Model({ AsyncCounter })
const initialModels = await getInitialState()
const { getState } = Model({ AsyncCounter }, initialModels)
const state = getState('AsyncCounter')
expect(state.count).toBe(1)
})
})
20 changes: 20 additions & 0 deletions __test__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ export const Counter: ModelType<
}
}

// v3.0
export const NextCounter: NextModelType<
CounterState,
CounterActionParams & ExtraActionParams
> = {
state: { count: 0 },
actions: {
increment: params => {
return state => {
state.count += params
}
},
add: (params, { state }) => {
return {
count: state.count + params
}
}
}
}

export const Theme: ModelType<ThemeState, ThemeActionParams> = {
state: {
theme: 'dark'
Expand Down
44 changes: 0 additions & 44 deletions __test__/useStore.spec.ts

This file was deleted.

17 changes: 17 additions & 0 deletions __test__/useStore/actions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="../index.d.ts" />
import { testHook } from 'react-hooks-testing-library'
import { Counter } from '..'
import { Model } from '../../src'

describe('useStore', () => {
test('consumer actions return Partial<State>', async () => {
let state: any
let actions: any
const { useStore } = Model({ Counter })
testHook(() => {
;[state, actions] = useStore('Counter')
})
await actions.add(3)
expect(state).toEqual({ count: 3 })
})
})
27 changes: 27 additions & 0 deletions __test__/useStore/initial.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path="../index.d.ts" />
import { testHook } from 'react-hooks-testing-library'
import { Model } from '../../src'
import { Counter } from '..'

describe('useStore', () => {
test('return default initial values', () => {
let state
const { useStore } = Model({ Counter })
testHook(() => {
;[state] = useStore('Counter')
})
expect(state).toEqual({ count: 0 })
})
test('consumer actions return function', async () => {
let state: any
let actions: any
const { useStore } = Model({ Counter })
testHook(() => {
;[state, actions] = useStore('Counter')
})
await actions.increment(3)
expect(state).toEqual({ count: 3 })
await actions.increment(4)
expect(state.count).toBe(7)
})
})
13 changes: 13 additions & 0 deletions __test__/useStore/initialModels.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="../index.d.ts" />
import { AsyncCounter } from '..'
import { Model } from '../../src'

describe('useStore', () => {
test('use initialModels', async () => {
const { getInitialState } = Model({ AsyncCounter })
const initialModels = await getInitialState()
const { getState } = Model({ AsyncCounter }, initialModels)
const state = getState('AsyncCounter')
expect(state.count).toBe(1)
})
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-model",
"version": "2.6.0",
"version": "2.7.0",
"description": "The State management library for React",
"main": "./dist/react-model.js",
"umd:main": "./dist/react-model.umd.js",
Expand Down Expand Up @@ -44,7 +44,7 @@
"remark-preset-lint-recommended": "^3.0.2",
"ts-jest": "^24.0.0",
"tslint": "^5.14.0",
"typescript": "^3.3.1"
"typescript": "^3.3.4000"
},
"husky": {
"hooks": {
Expand Down
Loading

0 comments on commit b5e82af

Please sign in to comment.