-
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
Showing
6 changed files
with
398 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
import React, { createContext } from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import { StoreType } from '../types'; | ||
import { createSelectorHook } from './useSelector'; | ||
|
||
const oldState = { value: 42 }; | ||
|
||
const unsubscribe = jest.fn(); | ||
const subscribe = jest.fn().mockReturnValue({ unsubscribe }); | ||
const dispatch = jest.fn(); | ||
const getState = jest.fn(); | ||
|
||
const mapStateToProps = <S, P>(state: S, props: P): S & P => ({ ...props, ...state }); | ||
|
||
describe('Check the useSelector hook', () => { | ||
let results: any[]; | ||
let context: React.Context<StoreType<any, any>>; | ||
let Component: React.FunctionComponent<{ type: string }>; | ||
let root: renderer.ReactTestRenderer | undefined; | ||
|
||
beforeEach(() => { | ||
getState.mockReturnValue(oldState); | ||
|
||
root = undefined; | ||
results = []; | ||
context = createContext<StoreType<any>>({ dispatch, getState, subscribe }); | ||
|
||
Component = (props) => { | ||
const state = createSelectorHook(context)(mapStateToProps, props); | ||
|
||
results.push(state); | ||
|
||
return React.createElement<any>('input', state); | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
if (root) { | ||
renderer.act(() => { | ||
root!.unmount(); | ||
}); | ||
} | ||
}); | ||
|
||
test('it should be return the element with merge props when the element is created', () => { | ||
renderer.act(() => { | ||
root = renderer.create(<Component type="text" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(results).toMatchSnapshot('results'); | ||
expect(results.length).toBe(1); | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
expect(getState).toHaveBeenCalledTimes(1); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(unsubscribe).toHaveBeenCalledTimes(0); | ||
|
||
renderer.act(() => { | ||
root!.unmount(); | ||
}); | ||
|
||
expect(unsubscribe).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('it should be the element is recreated when element is updated with new props', () => { | ||
renderer.act(() => { | ||
root = renderer.create(<Component type="text" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
|
||
renderer.act(() => { | ||
root!.update(<Component type="number" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(results).toMatchSnapshot('results'); | ||
expect(results.length).toBe(2); | ||
expect(results[0] === results[1]).toBe(false); | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
expect(getState).toHaveBeenCalledTimes(2); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(unsubscribe).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('it should be the element is recreated when element is updated with same props', () => { | ||
renderer.act(() => { | ||
root = renderer.create(<Component type="text" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
|
||
renderer.act(() => { | ||
root!.update(<Component type="text" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(results).toMatchSnapshot('results'); | ||
expect(results.length).toBe(2); | ||
expect(results[0] === results[1]).toBe(false); | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
expect(getState).toHaveBeenCalledTimes(2); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(unsubscribe).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('it should be the element is recreated when element is updated with equal element', () => { | ||
const Element = <Component type="text" />; | ||
|
||
renderer.act(() => { | ||
root = renderer.create(Element); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
|
||
renderer.act(() => { | ||
root!.update(Element); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(results).toMatchSnapshot('results'); | ||
expect(results.length).toBe(1); | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
expect(getState).toHaveBeenCalledTimes(1); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(unsubscribe).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('it should be the element is recreated when the state is updated (new value)', () => { | ||
renderer.act(() => { | ||
root = renderer.create(<Component type="text" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(subscribe).toHaveBeenCalledWith(expect.any(Function)); | ||
|
||
const newState = { value: 13 }; | ||
getState.mockReturnValue(newState); | ||
|
||
renderer.act(() => { | ||
subscribe.mock.calls[0][0](newState); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(results).toMatchSnapshot('results'); | ||
expect(results.length).toBe(2); | ||
expect(results[0] === results[1]).toBe(false); | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
expect(getState).toHaveBeenCalledTimes(2); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(unsubscribe).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('it should be the element is recreated when the state is updated (same value but diff reference)', () => { | ||
renderer.act(() => { | ||
root = renderer.create(<Component type="text" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(subscribe).toHaveBeenCalledWith(expect.any(Function)); | ||
|
||
const newState = { value: 42 }; | ||
getState.mockReturnValue(newState); | ||
|
||
renderer.act(() => { | ||
subscribe.mock.calls[0][0](newState); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(results).toMatchSnapshot('results'); | ||
expect(results.length).toBe(2); | ||
expect(results[0] === results[1]).toBe(false); | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
expect(getState).toHaveBeenCalledTimes(2); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(unsubscribe).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('it should be the element is recreated when the state is updated (same reference)', () => { | ||
renderer.act(() => { | ||
root = renderer.create(<Component type="text" />); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(subscribe).toHaveBeenCalledWith(expect.any(Function)); | ||
|
||
renderer.act(() => { | ||
subscribe.mock.calls[0][0](oldState); | ||
}); | ||
|
||
expect(root!.toJSON()).toMatchSnapshot('json'); | ||
expect(results).toMatchSnapshot('results'); | ||
expect(results.length).toBe(1); | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
expect(getState).toHaveBeenCalledTimes(1); | ||
expect(subscribe).toHaveBeenCalledTimes(1); | ||
expect(unsubscribe).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
Oops, something went wrong.