-
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
5 changed files
with
575 additions
and
321 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ReduxHotModule } from '../../../src/reduxHotModule' | ||
|
||
jest.mock('react-redux', () => ({ | ||
connect: jest.fn((mapStateToProps, mapDispatchToProps) => (component) => component) | ||
})) | ||
|
||
describe('test params actions', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('should have expected name', () => { | ||
const ml = new ReduxHotModule('moduleName') | ||
|
||
ml.addParamsAction('ready', {}) | ||
|
||
const { actions } = ml.create() | ||
|
||
expect(typeof actions).toBe('object') | ||
|
||
const actionNames = Object.keys(actions) | ||
|
||
expect(actionNames).toHaveLength(1) | ||
expect(actionNames).toContain('readyAction') | ||
}) | ||
|
||
test('should return redux action with default value', () => { | ||
const ml = new ReduxHotModule('moduleName') | ||
|
||
const defaultValue = {} | ||
|
||
ml.addParamsAction('ready', defaultValue) | ||
|
||
const { actions } = ml.create() | ||
|
||
expect(Object.keys(actions)).toHaveLength(1) | ||
|
||
const { readyAction } = actions | ||
|
||
expect(typeof readyAction).toBe('function') | ||
|
||
const reduxAction = readyAction() | ||
const expected = { | ||
type: '@@moduleName/READY', | ||
payload: defaultValue | ||
} | ||
|
||
expect(typeof reduxAction).toBe('object') | ||
expect(reduxAction).toStrictEqual(expected) | ||
expect(reduxAction.payload).not.toBe(defaultValue) | ||
}) | ||
|
||
test('should return redux action with provided value', () => { | ||
const ml = new ReduxHotModule('moduleName') | ||
|
||
ml.addParamsAction('ready', { a: 'a', b: 'b' }) | ||
|
||
const { actions } = ml.create() | ||
|
||
expect(Object.keys(actions)).toHaveLength(1) | ||
|
||
const { readyAction } = actions | ||
|
||
expect(typeof readyAction).toBe('function') | ||
|
||
const value = { b: 'new b' } | ||
const reduxAction = readyAction(value) | ||
const expected = { | ||
type: '@@moduleName/READY', | ||
payload: { a: 'a', b: 'new b' } | ||
} | ||
|
||
expect(typeof reduxAction).toBe('object') | ||
expect(reduxAction).toStrictEqual(expected) | ||
expect(reduxAction.payload).not.toBe(value) | ||
}) | ||
|
||
test('should return redux action with empty object value if action added without default value', () => { | ||
const ml = new ReduxHotModule('moduleName') | ||
|
||
ml.addParamsAction('ready') | ||
|
||
const { actions } = ml.create() | ||
|
||
expect(Object.keys(actions)).toHaveLength(1) | ||
|
||
const { readyAction } = actions | ||
|
||
expect(typeof readyAction).toBe('function') | ||
|
||
const reduxAction = readyAction() | ||
const expected = { | ||
type: '@@moduleName/READY', | ||
payload: {} | ||
} | ||
|
||
expect(typeof reduxAction).toBe('object') | ||
expect(reduxAction).toStrictEqual(expected) | ||
}) | ||
}) |
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,61 @@ | ||
import { ReduxHotModule } from '../../../src/reduxHotModule' | ||
|
||
jest.mock('react-redux', () => ({ | ||
connect: jest.fn((mapStateToProps, mapDispatchToProps) => (component) => component) | ||
})) | ||
|
||
describe('test params reducer', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('should set default values when action value is not provided', () => { | ||
const ml = new ReduxHotModule('moduleName') | ||
const defaultValue = { a: 'new a', b: 'bew b' } | ||
|
||
ml.addParamAction('a', 'a') | ||
ml.addParamAction('b', 'b') | ||
ml.addParamAction('c', 'c') | ||
|
||
ml.addParamsAction('items', defaultValue) | ||
|
||
const { actions, reducer, initialState } = ml.create() | ||
|
||
expect(typeof actions).toBe('object') | ||
expect(typeof reducer).toBe('function') | ||
expect(typeof initialState).toBe('object') | ||
expect(initialState).toStrictEqual({ a: 'a', b: 'b', c: 'c' }) | ||
|
||
const { itemsAction } = actions | ||
const nextState = reducer(initialState, itemsAction()) | ||
|
||
expect(nextState).not.toBe(initialState) | ||
expect(nextState).toStrictEqual({ a: 'new a', b: 'bew b', c: 'c' }) | ||
}) | ||
|
||
test('should set provided value when action value is provided', () => { | ||
const ml = new ReduxHotModule('moduleName') | ||
|
||
const defaultValue = { a: 'new a', b: 'new b' } | ||
|
||
ml.addParamAction('a', 'a') | ||
ml.addParamAction('b', 'b') | ||
ml.addParamAction('c', 'c') | ||
|
||
ml.addParamsAction('items', defaultValue) | ||
|
||
const { actions, reducer, initialState } = ml.create() | ||
|
||
expect(typeof actions).toBe('object') | ||
expect(typeof reducer).toBe('function') | ||
expect(typeof initialState).toBe('object') | ||
expect(initialState).toStrictEqual({ a: 'a', b: 'b', c: 'c' }) | ||
|
||
const { itemsAction } = actions | ||
const value = { b: 'bbb' } | ||
const nextState = reducer(initialState, itemsAction(value)) | ||
|
||
expect(nextState).not.toBe(initialState) | ||
expect(nextState).toStrictEqual({ a: 'new a', b: 'bbb', c: 'c' }) | ||
}) | ||
}) |
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.