source: node_modules/@reduxjs/toolkit/src/tests/createAction.test.ts@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

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