|
1 |
| -import { createAction, getType } from '@reduxjs/toolkit' |
| 1 | +import { createAction, getType, isAction } from '@reduxjs/toolkit' |
2 | 2 |
|
3 | 3 | describe('createAction', () => {
|
4 | 4 | it('should create an action', () => {
|
@@ -122,6 +122,27 @@ describe('createAction', () => {
|
122 | 122 | })
|
123 | 123 | })
|
124 | 124 |
|
| 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 | + |
125 | 146 | describe('getType', () => {
|
126 | 147 | it('should return the action type', () => {
|
127 | 148 | const actionCreator = createAction('A_TYPE')
|
|
0 commit comments