Skip to content

Commit 0c6cd6e

Browse files
author
ben.durrant
committed
add test
1 parent 482270e commit 0c6cd6e

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

packages/toolkit/src/tests/createAction.test.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createAction, getType } from '@reduxjs/toolkit'
1+
import { createAction, getType, isAction } from '@reduxjs/toolkit'
22

33
describe('createAction', () => {
44
it('should create an action', () => {
@@ -122,6 +122,27 @@ describe('createAction', () => {
122122
})
123123
})
124124

125+
describe('isAction', () => {
126+
it('should only return true for plain objects with a type property', () => {
127+
const actionCreator = createAction('anAction')
128+
class Action {
129+
type = 'totally an action'
130+
}
131+
const testCases: [action: unknown, expected: boolean][] = [
132+
[{ type: 'an action' }, true],
133+
[{ type: 'more props', extra: true }, true],
134+
[actionCreator(), true],
135+
[actionCreator, false],
136+
[Promise.resolve({ type: 'an action' }), false],
137+
[new Action(), false],
138+
['a string', false],
139+
]
140+
for (const [action, expected] of testCases) {
141+
expect(isAction(action)).toBe(expected)
142+
}
143+
})
144+
})
145+
125146
describe('getType', () => {
126147
it('should return the action type', () => {
127148
const actionCreator = createAction('A_TYPE')

0 commit comments

Comments
 (0)