11"use strict"
22
33const { createMachine} = require ( './index.js' )
4- const assert = require ( 'assert ' )
4+ const test = require ( 'tape ' )
55
66// BEGIN FIXTURES
77
@@ -45,68 +45,75 @@ const fetchUsersReducer = createMachine({
4545
4646// END FIXTURES
4747
48- // BEGIN HELPERS
48+ // run the reducer and go through several status transitions
49+ // to test a typical scenario—making an API call
4950
50- let state = undefined
51- let prevState = undefined
51+ test ( 'should transition between states' , t => {
5252
53- const action = ( type , payload ) => {
54- prevState = state
55- state = fetchUsersReducer ( state , { type, payload} )
56- }
53+ let state = undefined
54+ let prevState = undefined
5755
58- const expect = ( expected , maybeMessage ) => assert . deepEqual ( state , expected , maybeMessage )
56+ const expect = ( expected , maybeMessage ) => t . deepEquals ( state , expected , maybeMessage )
5957
60- // END HELPERS
58+ const action = ( type , payload ) => {
59+ prevState = state
60+ state = fetchUsersReducer ( state , { type, payload} )
61+ }
6162
62- // run the reducer and go through several status transitions
63- // to test a typical scenario—making an API call
63+ action ( 'DUMMY' )
64+ expect ( {
65+ status :'INIT' ,
66+ } , 'Should set initial status to "INIT"' )
6467
65- action ( 'DUMMY' )
66- expect ( {
67- status :'INIT' ,
68- } , 'Should set initial status to "INIT"' )
68+ action ( 'FETCH_USERS_RESPONSE' , { users} )
69+ expect ( prevState , 'Should ignore messages when not handled by current status' )
6970
70- action ( 'FETCH_USERS_RESPONSE' , { users} )
71- expect ( prevState , 'Should ignore messages when not handled by current status' )
71+ action ( 'FETCH_USERS' )
72+ expect ( {
73+ error :null ,
74+ status :'IN_PROGRESS'
75+ } )
7276
73- action ( 'FETCH_USERS ' )
74- expect ( {
75- error :null ,
76- status :'IN_PROGRESS '
77- } )
77+ action ( 'FETCH_USERS_FAIL' , 'timeout ')
78+ expect ( {
79+ error :'timeout' ,
80+ status :'INIT '
81+ } )
7882
79- action ( 'FETCH_USERS_FAIL' , 'timeout ')
80- expect ( {
81- error :'timeout' ,
82- status :'INIT '
83- } )
83+ action ( 'FETCH_USERS ' )
84+ expect ( {
85+ error :null ,
86+ status :'IN_PROGRESS '
87+ } )
8488
85- action ( 'FETCH_USERS' )
86- expect ( {
87- error :null ,
88- status :'IN_PROGRESS'
89- } )
89+ action ( 'FETCH_USERS' )
90+ expect ( prevState )
9091
91- action ( 'FETCH_USERS' )
92- expect ( prevState )
92+ action ( 'FETCH_USERS_RESPONSE' , { users} )
93+ expect ( {
94+ error :null ,
95+ status :'INIT' ,
96+ users
97+ } )
9398
94- action ( 'FETCH_USERS_RESPONSE' , { users} )
95- expect ( {
96- error :null ,
97- status :'INIT' ,
98- users
99+ t . end ( )
99100} )
100101
101- assert . throws (
102- ( ) => {
103- let store = { status :'STATUS_NOT_IN_CREATE_MACHINE' }
104- const reducer = createMachine ( { } )
105- store = reducer ( store , { type :'DUMMY' } )
102+ test ( 'should error on status not found' , t => {
103+ let store = { status :'STATUS_NOT_IN_CREATE_MACHINE' }
104+ const reducer = createMachine ( { } )
105+ let error
106+ try {
107+ reducer ( store , { type :'DUMMY' } )
108+ }
109+ catch ( err ) {
110+ error = err
111+ }
106112
107- } ,
108- err => err . message === 'reducersObject missing reducer for status STATUS_NOT_IN_CREATE_MACHINE' ,
109- 'should error when statusnot found '
110- )
113+ t . equals (
114+ error . message ,
115+ 'reducersObject missing reducer for statusSTATUS_NOT_IN_CREATE_MACHINE '
116+ )
111117
112- console . log ( 'success' )
118+ t . end ( )
119+ } )