forked from facebookarchive/redux-react-hook
-
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.
- Loading branch information
1 parent
33545b5
commit aab2a98
Showing
4 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
Empty file.
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,141 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import {Store} from 'redux'; | ||
import {useMappedState} from '..'; | ||
|
||
interface IAction { | ||
type: 'add todo'; | ||
} | ||
|
||
interface IState { | ||
bar: number; | ||
foo: string; | ||
} | ||
|
||
describe('redux-react-hook', () => { | ||
let subscriberCallback: () => void; | ||
let state: IState; | ||
let cancelSubscription: () => void; | ||
let store: Store<IState, IAction>; | ||
let context: React.Context<Store<IState>>; | ||
let reactRoot: HTMLDivElement; | ||
|
||
const createStore = (): Store<IState, IAction> => ({ | ||
dispatch: (action: any) => action, | ||
getState: () => state, | ||
subscribe: (l: () => void) => { | ||
subscriberCallback = l; | ||
return cancelSubscription; | ||
}, | ||
// tslint:disable-next-line:no-empty | ||
replaceReducer() {}, | ||
}); | ||
|
||
beforeEach(() => { | ||
cancelSubscription = jest.fn(); | ||
state = {bar: 123, foo: 'bar'}; | ||
store = createStore(); | ||
context = React.createContext(store); | ||
|
||
reactRoot = document.createElement('div'); | ||
document.body.appendChild(reactRoot); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(reactRoot); | ||
}); | ||
|
||
function render(element: React.ReactElement<any>) { | ||
ReactDOM.render( | ||
<context.Provider value={store}>{element}</context.Provider>, | ||
reactRoot, | ||
); | ||
} | ||
|
||
function getText() { | ||
return reactRoot.textContent; | ||
} | ||
|
||
it('renders with state from the store', () => { | ||
const mapState = (s: IState) => s.foo; | ||
const Component = () => { | ||
const foo = useMappedState(context, mapState); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
render(<Component />); | ||
|
||
expect(getText()).toBe('bar'); | ||
}); | ||
|
||
it('rerenders with new state when the subscribe callback is called', () => { | ||
const mapState = (s: IState) => s.foo; | ||
const Component = () => { | ||
const foo = useMappedState(context, mapState); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
render(<Component />); | ||
|
||
state = {bar: 123, foo: 'foo'}; | ||
subscriberCallback(); | ||
|
||
expect(getText()).toBe('foo'); | ||
}); | ||
|
||
it('does not rerender if the selected state has not changed', () => { | ||
const mapState = (s: IState) => s.foo; | ||
let renderCount = 0; | ||
const Component = () => { | ||
const foo = useMappedState(context, mapState); | ||
renderCount++; | ||
return ( | ||
<div> | ||
{foo} {renderCount} | ||
</div> | ||
); | ||
}; | ||
|
||
render(<Component />); | ||
|
||
expect(getText()).toBe('bar 1'); | ||
|
||
state = {bar: 456, ...state}; | ||
subscriberCallback(); | ||
|
||
expect(getText()).toBe('bar 1'); | ||
}); | ||
|
||
it('rerenders if the mapState function changes', () => { | ||
const Component = ({n}: {n: number}) => { | ||
const mapState = React.useCallback((s: IState) => s.foo + ' ' + n, [n]); | ||
const foo = useMappedState(context, mapState); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
render(<Component n={100} />); | ||
|
||
expect(getText()).toBe('bar 100'); | ||
|
||
render(<Component n={45} />); | ||
|
||
expect(getText()).toBe('bar 45'); | ||
}); | ||
|
||
it('rerenders if the store changes', () => { | ||
const mapState = (s: IState) => s.foo; | ||
const Component = () => { | ||
const foo = useMappedState(context, mapState); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
render(<Component />); | ||
|
||
expect(getText()).toBe('bar'); | ||
|
||
store = createStore(); | ||
state = {...state, foo: 'hello'}; | ||
render(<Component />); | ||
expect(getText()).toBe('hello'); | ||
}); | ||
}); |
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