-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.js
176 lines (169 loc) · 5.42 KB
/
fixtures.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import produce, { nothing } from "immer"
import { applyPatch } from "json-patch-es6"
export const emptyArray = [];
// cf. http://davidkpiano.github.io/xstate/docs/#/api/config
// NOTE : so it turns that xstate action as string is not a possibility, so only action as object remaining to test
const nonHierarchicalMachine = {
context: emptyArray,
initial: 'green',
states: {
green: {
on: {
TIMER: {
target: 'yellow', // since 4.0
// specify that 'startYellowTimer' action should be executed
actions: ['incGreenTimer']
}
}
},
yellow: {
onEntry: ['incYellowTimer'],
on: {
TIMER: [
{ target: 'red', cond: timer => timer.filter(x => x === 'yellow').length > 1 },
{ target: 'yellow', cond: timer => timer.filter(x => x === 'yellow').length <= 1 },
]
}
},
red: {
on: {
TIMER: {
target: 'green',
actions: ['logGreen']
}
}
},
}
};
// https://xstate.js.org/docs/#/guides/guards
export const initialContextHierarchicalMachine = { isAdmin: true };
const hierarchicalMachine = {
context: initialContextHierarchicalMachine,
id: 'door',
initial: 'closed',
states: {
closed: {
initial: 'idle',
states: {
'idle': {},
'error': {
onEntry: [function logMessage(extS, ev) {return { updates: [], outputs: ['Entered .closed.error!', ev] }}]
}
},
// 'OPEN', {CLOSE, overrideAdmin:true}, 'OPEN'}
on: {
OPEN: [
{ target: 'opened', cond: (extState, eventObj) => extState.isAdmin },
{ target: 'closed.error' }
]
}
},
opened: {
on: {
CLOSE: [
{ target: 'closed', cond: (extState, eventObj) => eventObj.overrideAdmin, actions: { type: 'cancelAdmin' } },
{ target: 'closed', cond: (extState, eventObj) => !eventObj.overrideAdmin }
]
}
},
}
};
// cf. http://davidkpiano.github.io/xstate/docs/#/api/config
const parallelMachine = {
key: 'intersection',
parallel: true,
states: {
northSouthLight: {
initial: 'green',
states: {
green: { on: { TIMER: 'yellow' } },
yellow: { on: { TIMER: 'red' } },
red: { on: { TIMER: 'green' } },
}
},
eastWestLight: {
initial: 'red',
states: {
green: { on: { TIMER: 'yellow' } },
yellow: { on: { TIMER: 'red' } },
red: { on: { TIMER: 'green' } },
}
}
}
};
const actionFactoryMaps = {
stringActions: {
'incYellowTimer': (extendedState, event) => {
return {
updates: extendedState => {extendedState.push('yellow')},
outputs: [extendedState, event]
}
},
'incGreenTimer': (extendedState, event) => {
return {
updates: extendedState => {extendedState.push('green')},
outputs: [extendedState, event]
}
},
'logGreen': (extendedState, event) => {
return {
updates: extendedState => {extendedState.pop(), extendedState.pop()},
outputs: [extendedState, event]
}
},
'cancelAdmin': (extendedState, event, action) => {
return {
updates: [{ op: 'add', path: '/isAdmin', value: false }],
outputs: ['admin rights overriden']
}
}
},
};
// DOC : for immer, updates are ONE function, not an array
export const NO_IMMER_UPDATES = nothing;
export const immerReducer = function (extendedState, updates) {
if (updates === NO_IMMER_UPDATES) return extendedState
const updateFn = updates;
return produce(extendedState, updateFn)
};
export const jsonPatchReducer = (extendedState, extendedStateUpdateOperations) => {
// NOTE : we don't validate operations, to avoid throwing errors when for instance the value property for an
// `add` JSON operation is `undefined` ; and of course we don't mutate the document in place
return applyPatch(extendedState, extendedStateUpdateOperations, false, false).newDocument;
};
export const NO_JSON_PATCH_UPDATES = [];
// DOC : outputs is an array of output = command
export const mergeOutputs = function (accOutputs, outputs) {
return (accOutputs || []).concat(outputs || [])
};
const reducers = {
immerReducer: immerReducer,
jsonpatchReducer: jsonPatchReducer
};
export const testCases = {
// NOTE : THAT was tough to check - but precisely gives a good idea
StandardMachineAndImmerAndStringsActionAndEvents: {
description: '(non-hierarchical, immer, mergeOutput, all action strings or none, event as string, >1 inputs)',
machine: nonHierarchicalMachine,
updateState: reducers.immerReducer,
actionFactoryMap: actionFactoryMaps.stringActions,
mergeOutputs,
inputSequence: ['TIMER', 'TIMER', 'TIMER', 'TIMER', 'TIMER'],
outputSequence: [
[[], "TIMER", ["green"], "TIMER"],
[["green", "yellow"], "TIMER"],
null,
[["green", "yellow", "yellow"], "TIMER"],
[["green",], "TIMER", ["green", "green"], "TIMER"],
]
},
HierarchicalMachineAndJSONPatchAndFunctionActionsAndObjectEvents: {
description: '(hierarchical, json patch, mergeOutput, action functions and strings, event as object, >1 inputs)',
machine: hierarchicalMachine,
updateState: reducers.jsonpatchReducer,
actionFactoryMap: actionFactoryMaps.stringActions,
mergeOutputs,
inputSequence: ['OPEN', { type: 'CLOSE', overrideAdmin: true }, 'OPEN'],
outputSequence: [null, ['admin rights overriden'], ["Entered .closed.error!", "OPEN"]]
},
};