@@ -5,7 +5,60 @@ function toJSON(obj) {
55 return JSON . parse ( JSON . stringify ( obj ) )
66}
77
8- describe . only ( 'Overmind' , ( ) => {
8+ function createDefaultApp ( ) {
9+ const state = {
10+ foo : 'bar' ,
11+ }
12+ const changeFoo : Action = ( context ) => {
13+ context . state . foo = 'bar2'
14+ }
15+ const changeFooWithEffect : Action = ( context ) => {
16+ context . state . foo = context . hello ( )
17+ }
18+ const waitAndChangeFoo : Action = ( context ) => {
19+ return context . wait ( ) . then ( ( ) => {
20+ context . state . foo = 'bar2'
21+ } )
22+ }
23+ const asyncChangeFoo : Action = async ( context ) => {
24+ await Promise . resolve ( )
25+ context . state . foo = 'bar2'
26+ }
27+ const actions = {
28+ changeFoo,
29+ changeFooWithEffect,
30+ waitAndChangeFoo,
31+ asyncChangeFoo,
32+ }
33+ const effects = {
34+ hello ( ) {
35+ return 'hello'
36+ } ,
37+ wait ( ) {
38+ return Promise . resolve ( )
39+ } ,
40+ }
41+ const config = {
42+ state,
43+ actions,
44+ effects,
45+ }
46+
47+ type App = TApp < {
48+ state : typeof state
49+ actions : typeof actions
50+ effects : typeof effects
51+ } >
52+ type Action < Value = void , ReturnValue = Value > = TAction <
53+ App ,
54+ Value ,
55+ ReturnValue
56+ >
57+
58+ return new Overmind ( config )
59+ }
60+
61+ describe ( 'Overmind' , ( ) => {
962 test ( 'should instantiate app with state' , ( ) => {
1063 const app = new Overmind ( {
1164 state : {
@@ -35,96 +88,34 @@ describe.only('Overmind', () => {
3588 } )
3689 test ( 'should be able to type actions' , ( ) => {
3790 expect . assertions ( 2 )
38- const state = {
39- foo : 'bar' ,
40- }
41- const doThis : Action = ( context ) => {
42- context . state . foo = 'bar2'
43- }
44- const actions = {
45- doThis,
46- }
47- const config = {
48- state,
49- actions,
50- }
51-
52- type App = TApp < {
53- state : typeof state
54- actions : typeof actions
55- } >
56- type Action < Value = void , ReturnValue = Value > = TAction <
57- App ,
58- Value ,
59- ReturnValue
60- >
6191
62- const app = new Overmind ( config )
92+ const app = createDefaultApp ( )
6393
6494 expect ( app . state . foo ) . toBe ( 'bar' )
65- app . actions . doThis ( )
95+ app . actions . changeFoo ( )
6696 expect ( app . state . foo ) . toBe ( 'bar2' )
6797 } )
6898 test ( 'should allow changing state in actions' , ( ) => {
6999 expect . assertions ( 2 )
70- const app = new Overmind ( {
71- state : {
72- foo : 'bar' ,
73- } ,
74- actions : {
75- doThis ( context ?) {
76- context . state . foo = 'bar2'
77- } ,
78- } ,
79- } )
100+ const app = createDefaultApp ( )
80101
81102 expect ( app . state . foo ) . toBe ( 'bar' )
82- app . actions . doThis ( )
103+ app . actions . changeFoo ( )
83104 expect ( app . state . foo ) . toBe ( 'bar2' )
84105 } )
85106 test ( 'should expose effects to actions' , ( ) => {
86107 expect . assertions ( 2 )
87- const app = new Overmind ( {
88- state : {
89- foo : 'bar' ,
90- } ,
91- actions : {
92- doThis ( context ?) {
93- context . state . foo = context . hello ( )
94- } ,
95- } ,
96- effects : {
97- hello ( ) {
98- return 'hello'
99- } ,
100- } ,
101- } )
108+ const app = createDefaultApp ( )
102109
103110 expect ( app . state . foo ) . toBe ( 'bar' )
104- app . actions . doThis ( )
111+ app . actions . changeFooWithEffect ( )
105112 expect ( app . state . foo ) . toBe ( 'hello' )
106113 } )
107114 test ( 'should be able to do mutations async via effects' , ( ) => {
108115 expect . assertions ( 2 )
109- const app = new Overmind ( {
110- state : {
111- foo : 'bar' ,
112- } ,
113- actions : {
114- doThis ( context ?) {
115- return context . wait ( ) . then ( ( ) => {
116- context . state . foo = 'bar2'
117- } )
118- } ,
119- } ,
120- effects : {
121- wait ( ) {
122- return Promise . resolve ( )
123- } ,
124- } ,
125- } )
116+ const app = createDefaultApp ( )
126117 expect ( app . state . foo ) . toBe ( 'bar' )
127- return app . actions . doThis ( ) . then ( ( ) => {
118+ return app . actions . waitAndChangeFoo ( ) . then ( ( ) => {
128119 expect ( app . state . foo ) . toBe ( 'bar2' )
129120 } )
130121 } )
@@ -187,20 +178,11 @@ describe.only('Overmind', () => {
187178 } )
188179 test ( 'should track mutations' , ( ) => {
189180 expect . assertions ( 1 )
190- const app = new Overmind ( {
191- state : {
192- foo : 'bar' ,
193- } ,
194- actions : {
195- doThis ( context ?) {
196- context . state . foo = 'bar2'
197- } ,
198- } ,
199- } )
181+ const app = createDefaultApp ( )
200182 app . eventHub . once ( EventType . MUTATIONS , ( data ) => {
201183 expect ( toJSON ( data ) ) . toEqual ( {
202184 actionId : 0 ,
203- actionName : 'doThis ' ,
185+ actionName : 'changeFoo ' ,
204186 mutations : [
205187 {
206188 args : [ 'bar2' ] ,
@@ -213,26 +195,15 @@ describe.only('Overmind', () => {
213195 path : [ ] ,
214196 } )
215197 } )
216- app . actions . doThis ( )
198+ app . actions . changeFoo ( )
217199 } )
218200 test ( 'should track async mutations' , ( ) => {
219201 expect . assertions ( 1 )
220- const app = new Overmind ( {
221- state : {
222- foo : 'bar' ,
223- } ,
224- actions : {
225- doThis ( context ?) {
226- Promise . resolve ( ) . then ( ( ) => {
227- context . state . foo = 'bar2'
228- } )
229- } ,
230- } ,
231- } )
202+ const app = createDefaultApp ( )
232203 app . eventHub . on ( EventType . MUTATIONS , ( data ) => {
233204 expect ( toJSON ( data ) ) . toEqual ( {
234- actionId : 0 ,
235- actionName : 'doThis ' ,
205+ actionId : 2 ,
206+ actionName : 'waitAndChangeFoo ' ,
236207 mutations : [
237208 {
238209 args : [ 'bar2' ] ,
@@ -245,25 +216,15 @@ describe.only('Overmind', () => {
245216 path : [ ] ,
246217 } )
247218 } )
248- app . actions . doThis ( )
219+ app . actions . waitAndChangeFoo ( )
249220 } )
250221 test ( 'should track async mutations with async await' , ( ) => {
251222 expect . assertions ( 1 )
252- const app = new Overmind ( {
253- state : {
254- foo : 'bar' ,
255- } ,
256- actions : {
257- doThis : async ( context ?) => {
258- await Promise . resolve ( )
259- context . state . foo = 'bar2'
260- } ,
261- } ,
262- } )
223+ const app = createDefaultApp ( )
263224 app . eventHub . on ( EventType . MUTATIONS , ( data ) => {
264225 expect ( toJSON ( data ) ) . toEqual ( {
265- actionId : 0 ,
266- actionName : 'doThis ' ,
226+ actionId : 3 ,
227+ actionName : 'asyncChangeFoo ' ,
267228 mutations : [
268229 {
269230 args : [ 'bar2' ] ,
@@ -276,7 +237,7 @@ describe.only('Overmind', () => {
276237 path : [ ] ,
277238 } )
278239 } )
279- app . actions . doThis ( )
240+ app . actions . asyncChangeFoo ( )
280241 } )
281242 test ( 'should instantiate app with modules' , ( ) => {
282243 const foo = {
0 commit comments