-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from byte-fe/feat/Model
feat(model): refactor model initialize method
- Loading branch information
Showing
15 changed files
with
400 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,5 @@ coverage/ | |
.rts2_cache_umd/ | ||
|
||
# Github | ||
.github/ | ||
.github/ | ||
.git/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.