Spaces:
Running
Running
File size: 4,873 Bytes
c2b7eb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import { createAction, isActionCreator } from '@reduxjs/toolkit'
describe('createAction', () => {
it('should create an action', () => {
const actionCreator = createAction<string>('A_TYPE')
expect(actionCreator('something')).toEqual({
type: 'A_TYPE',
payload: 'something',
})
})
describe('when stringifying action', () => {
it('should return the action type', () => {
const actionCreator = createAction('A_TYPE')
expect(`${actionCreator}`).toEqual('A_TYPE')
})
})
describe('when passing a prepareAction method only returning a payload', () => {
it('should use the payload returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
}))
expect(actionCreator(5).payload).toBe(10)
})
it('should not have a meta attribute on the resulting Action', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
}))
expect('meta' in actionCreator(5)).toBeFalsy()
})
})
describe('when passing a prepareAction method returning a payload and meta', () => {
it('should use the payload returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
meta: a / 2,
}))
expect(actionCreator(5).payload).toBe(10)
})
it('should use the meta returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
meta: a / 2,
}))
expect(actionCreator(10).meta).toBe(5)
})
})
describe('when passing a prepareAction method returning a payload and error', () => {
it('should use the payload returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
error: true,
}))
expect(actionCreator(5).payload).toBe(10)
})
it('should use the error returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
error: true,
}))
expect(actionCreator(10).error).toBe(true)
})
})
describe('when passing a prepareAction method returning a payload, meta and error', () => {
it('should use the payload returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
meta: a / 2,
error: true,
}))
expect(actionCreator(5).payload).toBe(10)
})
it('should use the error returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
meta: a / 2,
error: true,
}))
expect(actionCreator(10).error).toBe(true)
})
it('should use the meta returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
payload: a * 2,
meta: a / 2,
error: true,
}))
expect(actionCreator(10).meta).toBe(5)
})
})
describe('when passing a prepareAction that accepts multiple arguments', () => {
it('should pass all arguments of the resulting actionCreator to prepareAction', () => {
const actionCreator = createAction(
'A_TYPE',
(a: string, b: string, c: string) => ({
payload: a + b + c,
}),
)
expect(actionCreator('1', '2', '3').payload).toBe('123')
})
})
describe('actionCreator.match', () => {
test('should return true for actions generated by own actionCreator', () => {
const actionCreator = createAction('test')
expect(actionCreator.match(actionCreator())).toBe(true)
})
test('should return true for matching actions', () => {
const actionCreator = createAction('test')
expect(actionCreator.match({ type: 'test' })).toBe(true)
})
test('should return false for other actions', () => {
const actionCreator = createAction('test')
expect(actionCreator.match({ type: 'test-abc' })).toBe(false)
})
})
})
const actionCreator = createAction('anAction')
class Action {
type = 'totally an action'
}
describe('isActionCreator', () => {
it('should only return true for action creators', () => {
expect(isActionCreator(actionCreator)).toBe(true)
const notActionCreators = [
{ type: 'an action' },
{ type: 'more props', extra: true },
actionCreator(),
Promise.resolve({ type: 'an action' }),
new Action(),
false,
'a string',
false,
]
for (const notActionCreator of notActionCreators) {
expect(isActionCreator(notActionCreator)).toBe(false)
}
})
})
|