-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create hooks api * Use sideEffect for hook subscription. * Update hooks code. * Performance improvements to the hook * Hooks tests
- Loading branch information
1 parent
39da05e
commit 07e889e
Showing
20 changed files
with
533 additions
and
112 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
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 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 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,19 @@ | ||
import React from 'react'; | ||
import { useStaat } from './state'; | ||
|
||
export const LongListItem: React.FunctionComponent = () => { | ||
const name = useStaat(({ welcome }) => welcome.name); | ||
return <div>Value: {name}</div>; | ||
}; | ||
|
||
export const LongList: React.FunctionComponent = () => { | ||
return ( | ||
<div> | ||
{Array(5000) | ||
.fill(undefined) | ||
.map((_, index) => ( | ||
<LongListItem key={index} /> | ||
))} | ||
</div> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,43 +1,21 @@ | ||
import * as React from 'react'; | ||
|
||
import { connect, appState } from './state'; | ||
import * as welcome from './welcome-state-definition'; | ||
import { useReducers, useStaat } from './state'; | ||
import * as welcomeDefinition from './welcome-state-definition'; | ||
|
||
const Welcome: React.StatelessComponent<WelcomeProps> = props => { | ||
const Welcome: React.FunctionComponent = () => { | ||
const name = useStaat(({ welcome }) => welcome.name); | ||
const { setName } = useReducers({ | ||
setName: welcomeDefinition.setName, | ||
}); | ||
return ( | ||
<div> | ||
<h1>Welcome, {props.name}</h1> | ||
<input type='text' onChange={evt => props.setName(evt.target.value)} /> | ||
<h1>Welcome, {name}</h1> | ||
<input type='text' onChange={evt => setName(evt.target.value)} /> | ||
{/* <button onClick={() => props.undo()}>Undo</button> | ||
<button onClick={() => props.redo()}>Redo</button> */} | ||
</div> | ||
); | ||
}; | ||
type StateProps = { | ||
name?: string; | ||
}; | ||
|
||
type TrasformerProps = { | ||
setName: (name: string) => void; | ||
// undo: typeof welcomeState.undo; | ||
// redo: typeof welcomeState.redo; | ||
}; | ||
|
||
type OwnProps = {}; | ||
|
||
type WelcomeProps = StateProps & TrasformerProps & OwnProps; | ||
|
||
export default connect<OwnProps, StateProps, TrasformerProps>( | ||
state => { | ||
return { | ||
name: state.welcome.name, | ||
}; | ||
}, | ||
() => { | ||
return { | ||
setName: (name: string) => appState.reduce(welcome.setName, name), | ||
// undo: welcome.undo.bind(welcomeState), | ||
// redo: welcomeState.redo.bind(welcomeState) | ||
}; | ||
}, | ||
)(Welcome); | ||
export default Welcome; |
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,152 @@ | ||
import { reactStaat } from '../react'; | ||
import staat from 'staat'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { wait } from '@testing-library/react'; | ||
|
||
type TestState = { | ||
count: number; | ||
other: string; | ||
}; | ||
|
||
const state: TestState = { | ||
count: 0, | ||
other: 'tst', | ||
}; | ||
|
||
it('builds react-staat object', () => { | ||
const staatState = staat({ ...state }); | ||
const sut = reactStaat(staatState); | ||
expect(typeof sut.Provider).toBe('function'); | ||
expect(typeof sut.connect).toBe('function'); | ||
expect(typeof sut.useStaat).toBe('function'); | ||
expect(typeof sut.useReducers).toBe('function'); | ||
}); | ||
|
||
describe('Hooks', () => { | ||
describe('useStaat', () => { | ||
it('updates the value', async () => { | ||
const staatState = staat({ ...state }); | ||
const { useStaat } = reactStaat(staatState); | ||
|
||
const { result } = renderHook(() => useStaat(({ count }) => count)); | ||
|
||
act(() => { | ||
staatState.reduce(s => ({ ...s, count: 1000 })); | ||
}); | ||
|
||
await wait(() => expect(result.current).toBe(1000)); | ||
}); | ||
}); | ||
|
||
it('unsubscribes from staat', async () => { | ||
const staatState = staat({ ...state }); | ||
const { useStaat } = reactStaat(staatState); | ||
staatState.unsubscribe = jest.fn(); | ||
const { unmount } = renderHook(() => useStaat(({ count }) => count)); | ||
unmount(); | ||
expect(staatState.unsubscribe).toHaveBeenCalled(); | ||
}); | ||
|
||
it('updates if number of members is different', async () => { | ||
const staatState = staat({ ...state }); | ||
const { useStaat } = reactStaat(staatState); | ||
|
||
const { result } = renderHook(() => useStaat(({ count }) => count)); | ||
|
||
act(() => { | ||
staatState.reduce(sts => { | ||
return { ...sts, count: 200, another: '1' }; | ||
}); | ||
}); | ||
|
||
await wait(() => expect(result.current).toBe(200)); | ||
}); | ||
|
||
it('does not update the component if state is same object', async () => { | ||
const original = { ...state }; | ||
const staatState = staat(original); | ||
const { useStaat } = reactStaat(staatState); | ||
|
||
const { result } = renderHook(() => useStaat(sts => sts)); | ||
|
||
act(() => { | ||
staatState.reduce(sts => { | ||
sts.count = 200; | ||
return sts; | ||
}); | ||
}); | ||
await wait(() => expect(result.current).toBe(original), { timeout: 100 }); | ||
expect(original.count).toBe(200); | ||
}); | ||
|
||
it('does not update the component if state members are equal', async () => { | ||
const original = { ...state }; | ||
const staatState = staat(original); | ||
const { useStaat } = reactStaat(staatState); | ||
|
||
const { result } = renderHook(() => useStaat(sts => sts)); | ||
|
||
act(() => { | ||
staatState.reduce(sts => { | ||
return { ...sts }; | ||
}); | ||
}); | ||
await wait(() => expect(result.current).toBe(original), { timeout: 100 }); | ||
expect(result.current).toBe(original); | ||
}); | ||
|
||
it('updates when same number of properties but different name', async () => { | ||
const original: { a: string; b?: number; c?: number } = { a: '1', b: 2 }; | ||
const staatState = staat(original); | ||
const { useStaat } = reactStaat(staatState); | ||
|
||
const { result } = renderHook(() => useStaat(sts => sts)); | ||
|
||
act(() => { | ||
staatState.reduce(() => { | ||
return { a: '1', c: 2 }; | ||
}); | ||
}); | ||
await wait(() => expect(result.current).toEqual({ a: '1', c: 2 }), { | ||
timeout: 100, | ||
}); | ||
}); | ||
|
||
describe('useReducers', () => { | ||
it('changes the state without arguments', () => { | ||
const staatState = staat({ ...state }); | ||
const { useReducers } = reactStaat(staatState); | ||
const newValue = 10; | ||
|
||
const { result } = renderHook(() => | ||
useReducers({ | ||
testReducer: (sts: TestState) => ({ ...sts, count: newValue }), | ||
}), | ||
); | ||
act(() => { | ||
result.current.testReducer(); | ||
}); | ||
|
||
expect(staatState.currentState.count).toBe(10); | ||
}); | ||
|
||
it('changes the state with arguments', () => { | ||
const staatState = staat({ ...state }); | ||
const { useReducers } = reactStaat(staatState); | ||
|
||
const { result } = renderHook(() => | ||
useReducers({ | ||
testReducer: (sts: TestState, value: number) => ({ | ||
...sts, | ||
count: value, | ||
}), | ||
}), | ||
); | ||
act(() => { | ||
result.current.testReducer(300); | ||
}); | ||
|
||
expect(staatState.currentState.count).toBe(300); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.