-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
97 lines (83 loc) · 2.52 KB
/
index.test.js
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
import { matchPattern, payloadActions, errorActions } from './index'
describe('matchPattern', () => {
it('Matches with string pattern', () => {
{
const result = matchPattern('ACTION_TYPE', { type: 'ACTION_TYPE' })
expect(result).toEqual(true)
}
{
const result = matchPattern('ACTION_TYPE', { type: 'ACTION_TYPE_OTHER' })
expect(result).toEqual(false)
}
})
it('Matches with array of strings pattern', () => {
const arrayPattern = ['ACTION_TYPE_1', 'ACTION_TYPE_2']
{
const result = matchPattern(arrayPattern, { type: 'ACTION_TYPE_1' })
expect(result).toEqual(true)
}
{
const result = matchPattern(arrayPattern, { type: 'ACTION_TYPE_2' })
expect(result).toEqual(true)
}
{
const result = matchPattern(arrayPattern, { type: 'ACTION_TYPE_NONE' })
expect(result).toEqual(false)
}
})
it('Matches with predicate pattern', () => {
const predicatePattern = (action) => action.payload.foo === 'bar'
{
const result = matchPattern(
predicatePattern,
{ type: 'DUMMY_TYPE', payload: { foo: 'bar' } },
)
expect(result).toEqual(true)
}
{
const result = matchPattern(
predicatePattern,
{ type: 'DUMMY_TYPE', payload: { foo: 'noooo' } },
)
expect(result).toEqual(false)
}
})
it('Does not match with no pattern', () => {
const result = matchPattern(
undefined,
{ type: 'DUMMY_TYPE', payload: { foo: 'bar' } },
)
expect(result).toEqual(false)
})
})
describe('payloadActions', () => {
it('works', () => {
const { exampleAction } = payloadActions('FOO')
expect(typeof exampleAction).toEqual('function')
expect(exampleAction.type).toEqual('FOO/EXAMPLE_ACTION')
expect(exampleAction({ foo: 'bar' })).toEqual({
type: "FOO/EXAMPLE_ACTION",
payload: { foo: "bar" },
meta: {},
})
expect(exampleAction({ foo: 'bar' }, { meta: 'data' })).toEqual({
type: "FOO/EXAMPLE_ACTION",
payload: { foo: "bar" },
meta: { meta: 'data' },
})
})
})
describe('errorActions', () => {
it('works', () => {
const { exampleAction } = errorActions('FOO')
expect(typeof exampleAction).toEqual('function')
expect(exampleAction.type).toEqual('FOO/EXAMPLE_ACTION')
expect(exampleAction.error).toEqual(true)
expect(exampleAction({ foo: 'bar' }, { meta: 'data' })).toEqual({
type: "FOO/EXAMPLE_ACTION",
payload: { foo: "bar" },
meta: { meta: 'data' },
error: true
})
})
})