1
1
import { createDuck } from '../src' ;
2
2
3
+ type CountState = { count : number } ;
4
+
3
5
describe ( 'Redux Duck' , ( ) => {
4
- test ( 'define type without app name' , ( ) => {
5
- const duck = createDuck ( 'duck-name' ) ;
6
- expect ( duck . defineType ( 'action-name' ) ) . toBe ( 'duck-name/action-name' ) ;
7
- } ) ;
6
+ describe ( 'Define Type' , ( ) => {
7
+ test ( 'Without App Name' , ( ) => {
8
+ const duck = createDuck ( 'duck-name' ) ;
9
+ expect ( duck . defineType ( 'action-name' ) ) . toBe ( 'duck-name/action-name' ) ;
10
+ } ) ;
8
11
9
- test ( 'define type with app name' , ( ) => {
10
- const duck = createDuck ( 'duck-name' , 'app-name' ) ;
11
- expect ( duck . defineType ( 'action-name' ) ) . toBe (
12
- 'app-name/duck-name/action-name'
13
- ) ;
12
+ test ( 'With App Name' , ( ) => {
13
+ const duck = createDuck ( 'duck-name' , 'app-name' ) ;
14
+ expect ( duck . defineType ( 'action-name' ) ) . toBe (
15
+ 'app-name/duck-name/action-name'
16
+ ) ;
17
+ } ) ;
14
18
} ) ;
15
19
16
- test ( 'create action creator' , ( ) => {
17
- const duck = createDuck ( 'duck-name' , 'app-name' ) ;
18
- const type = duck . defineType ( 'action-name' ) ;
20
+ describe ( 'Action Creator' , ( ) => {
21
+ test ( 'No Error' , ( ) => {
22
+ const duck = createDuck ( 'duck-name' , 'app-name' ) ;
23
+ const type = duck . defineType ( 'action-name' ) ;
24
+
25
+ const action = duck . createAction < { id : number } , { analytics : string } > (
26
+ type
27
+ ) ;
28
+ expect ( typeof action ) . toBe ( 'function' ) ;
29
+ expect ( action ( ) ) . toEqual ( {
30
+ type,
31
+ error : false ,
32
+ meta : undefined ,
33
+ payload : undefined ,
34
+ } ) ;
35
+ expect ( action ( { id : 1 } ) ) . toEqual ( {
36
+ type,
37
+ payload : { id : 1 } ,
38
+ meta : undefined ,
39
+ error : false ,
40
+ } ) ;
41
+ expect ( action ( { id : 1 } , { analytics : 'random' } ) ) . toEqual ( {
42
+ type,
43
+ payload : { id : 1 } ,
44
+ meta : { analytics : 'random' } ,
45
+ error : false ,
46
+ } ) ;
47
+ } ) ;
19
48
20
- const action = duck . createAction ( type ) ;
21
- expect ( typeof action ) . toBe ( 'function' ) ;
22
- expect ( action ( ) ) . toEqual ( { type } ) ;
23
- expect ( action ( { id : 1 } ) ) . toEqual ( { type, payload : { id : 1 } } ) ;
49
+ test ( 'Error' , ( ) => {
50
+ const duck = createDuck ( 'duck-name' , 'app-name' ) ;
51
+ const type = duck . defineType ( 'action-name' ) ;
52
+
53
+ const action = duck . createAction < { id : number } , { analytics : string } > (
54
+ type ,
55
+ true
56
+ ) ;
57
+ expect ( typeof action ) . toBe ( 'function' ) ;
58
+ expect ( action ( ) ) . toEqual ( {
59
+ type,
60
+ error : true ,
61
+ meta : undefined ,
62
+ payload : undefined ,
63
+ } ) ;
64
+ expect ( action ( { id : 1 } ) ) . toEqual ( {
65
+ type,
66
+ payload : { id : 1 } ,
67
+ meta : undefined ,
68
+ error : true ,
69
+ } ) ;
70
+ expect ( action ( { id : 1 } , { analytics : 'random' } ) ) . toEqual ( {
71
+ type,
72
+ payload : { id : 1 } ,
73
+ meta : { analytics : 'random' } ,
74
+ error : true ,
75
+ } ) ;
76
+ } ) ;
24
77
} ) ;
25
78
26
- test ( 'reducer ' , ( ) => {
79
+ test ( 'Create Reducer ' , ( ) => {
27
80
const duck = createDuck ( 'duck-name' , 'app-name' ) ;
28
81
const type = duck . defineType ( 'action-name' ) ;
29
- const action = duck . createAction ( type ) ;
30
-
31
- type CountState = { count : number } ;
82
+ const action = duck . createAction < undefined , undefined > ( type ) ;
32
83
33
84
const reducer = duck . createReducer < CountState > (
34
85
{
@@ -45,4 +96,41 @@ describe('Redux Duck', () => {
45
96
expect ( reducer ( undefined , action ( ) ) ) . toEqual ( { count : 1 } ) ;
46
97
expect ( reducer ( { count : 2 } ) ) . toEqual ( { count : 2 } ) ;
47
98
} ) ;
99
+
100
+ describe ( 'Errors' , ( ) => {
101
+ test ( 'No Cases' , ( ) => {
102
+ const duck = createDuck ( 'duck-name' , 'app-name' ) ;
103
+ expect ( ( ) => duck . createReducer ( { } , '' ) ) . toThrowError (
104
+ 'You should pass at least one case name when creating a reducer.'
105
+ ) ;
106
+ } ) ;
107
+
108
+ test ( 'Zero Valid Cases' , ( ) => {
109
+ const duck = createDuck ( 'duck-name' , 'app-name' ) ;
110
+ expect ( ( ) => duck . createReducer ( { undefined : s => s } , '' ) ) . toThrowError (
111
+ 'All of your action types are undefined.'
112
+ ) ;
113
+ } ) ;
114
+
115
+ test ( 'Only One Valid' , ( ) => {
116
+ const duck = createDuck ( 'duck-name' , 'app-name' ) ;
117
+ expect ( ( ) =>
118
+ duck . createReducer ( { valid : s => s , undefined : s => s } , '' )
119
+ ) . toThrowError (
120
+ 'One or more of your action types are undefined. Valid cases are: valid.'
121
+ ) ;
122
+ } ) ;
123
+
124
+ test ( 'More Than One Valid' , ( ) => {
125
+ const duck = createDuck ( 'duck-name' , 'app-name' ) ;
126
+ expect ( ( ) =>
127
+ duck . createReducer (
128
+ { valid : s => s , undefined : s => s , anotherValid : s => s } ,
129
+ ''
130
+ )
131
+ ) . toThrowError (
132
+ 'One or more of your action types are undefined. Valid cases are: valid, anotherValid.'
133
+ ) ;
134
+ } ) ;
135
+ } ) ;
48
136
} ) ;
0 commit comments