1 | import { createAction, getType } from './createAction'
|
2 |
|
3 | describe('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 |
|
125 | describe('getType', () => {
|
126 | it('should return the action type', () => {
|
127 | const actionCreator = createAction('A_TYPE')
|
128 | expect(getType(actionCreator)).toEqual('A_TYPE')
|
129 | })
|
130 | })
|