@teleporthq/teleport-plugin-next-workflows 0.45.13 → 0.45.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/global-state-read-only-wiring.test.ts +257 -0
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/workflow-component-plugin.d.ts.map +1 -1
- package/dist/cjs/workflow-component-plugin.js +43 -1
- package/dist/cjs/workflow-component-plugin.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/workflow-component-plugin.d.ts.map +1 -1
- package/dist/esm/workflow-component-plugin.js +44 -2
- package/dist/esm/workflow-component-plugin.js.map +1 -1
- package/package.json +2 -2
- package/src/workflow-component-plugin.ts +64 -2
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// A workflow reads a global state through the state values its file hands the
|
|
2
|
+
// workflow runtime, and a global state is among them only when the file takes
|
|
3
|
+
// it from `useGlobalState()`. A file whose workflows only READ a global state
|
|
4
|
+
// used to get none of that, so the read was undefined: a timer in a component,
|
|
5
|
+
// gated on a global on/off switch that a page flips, never ticked.
|
|
6
|
+
|
|
7
|
+
import generate from '@babel/generator'
|
|
8
|
+
import { createNextWorkflowPlugin } from '../src/workflow-component-plugin'
|
|
9
|
+
import { buildStateGetHandlerSource } from '../src/nodes/state/_state-get'
|
|
10
|
+
|
|
11
|
+
const GLOBAL_STATES = {
|
|
12
|
+
TQ_switch: { id: 'TQ_switch', name: 'tickerTimerEnabled', type: 'boolean', defaultValue: true },
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const COMPONENT = { componentId: 'TQ_ticker', componentName: 'Ticker' }
|
|
16
|
+
|
|
17
|
+
const node = (id: string, type: string, config: Record<string, unknown>) => ({
|
|
18
|
+
id,
|
|
19
|
+
type,
|
|
20
|
+
config,
|
|
21
|
+
stepNumber: 1,
|
|
22
|
+
label: id,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const UPDATE_COUNT = node('bump', 'state-update-local-state', { property: 'count', value: 1 })
|
|
26
|
+
|
|
27
|
+
const timerWorkflow = (
|
|
28
|
+
nodes: Array<ReturnType<typeof node>>,
|
|
29
|
+
trigger: Record<string, unknown> = { scope: 'component', config: COMPONENT }
|
|
30
|
+
) => ({
|
|
31
|
+
id: 'wf-ticker',
|
|
32
|
+
name: 'Ticker Count Update',
|
|
33
|
+
trigger: {
|
|
34
|
+
type: 'event-interval',
|
|
35
|
+
nodeId: 'tick',
|
|
36
|
+
scope: trigger.scope,
|
|
37
|
+
config: { intervalMs: 1000, autoStart: true, ...(trigger.config as object) },
|
|
38
|
+
},
|
|
39
|
+
nodes,
|
|
40
|
+
edges: nodes.map((target, index) => ({
|
|
41
|
+
id: `e${index}`,
|
|
42
|
+
source: index === 0 ? 'tick' : nodes[index - 1].id,
|
|
43
|
+
target: target.id,
|
|
44
|
+
})),
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const jsxComponentChunk = () => ({
|
|
48
|
+
type: 'chunk-type-ast',
|
|
49
|
+
name: 'jsx-component',
|
|
50
|
+
content: {
|
|
51
|
+
type: 'VariableDeclaration',
|
|
52
|
+
kind: 'const',
|
|
53
|
+
declarations: [
|
|
54
|
+
{
|
|
55
|
+
type: 'VariableDeclarator',
|
|
56
|
+
id: { type: 'Identifier', name: 'Ticker' },
|
|
57
|
+
init: {
|
|
58
|
+
type: 'ArrowFunctionExpression',
|
|
59
|
+
params: [] as unknown[],
|
|
60
|
+
body: {
|
|
61
|
+
type: 'BlockStatement',
|
|
62
|
+
body: [{ type: 'ReturnStatement', argument: null as unknown }],
|
|
63
|
+
directives: [] as unknown[],
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
interface Generated {
|
|
72
|
+
component: string
|
|
73
|
+
module: string
|
|
74
|
+
dependencies: Record<string, { path?: string }>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const generateFile = async (params: {
|
|
78
|
+
workflow: any
|
|
79
|
+
isPage: boolean
|
|
80
|
+
globalStateDefinitions?: Record<string, unknown>
|
|
81
|
+
customNodes?: Record<string, unknown>
|
|
82
|
+
}): Promise<Generated> => {
|
|
83
|
+
const { workflow, isPage, globalStateDefinitions = GLOBAL_STATES, customNodes = {} } = params
|
|
84
|
+
const plugin = createNextWorkflowPlugin({ isPage })
|
|
85
|
+
const structure: any = {
|
|
86
|
+
uidl: {
|
|
87
|
+
name: isPage ? 'Home' : 'Ticker',
|
|
88
|
+
node: { type: 'element', content: { elementType: 'container', name: 'Container' } },
|
|
89
|
+
stateDefinitions: { count: { type: 'number', defaultValue: 0 } },
|
|
90
|
+
...(isPage ? { outputOptions: { pageId: 'page-1', fileName: 'index' } } : {}),
|
|
91
|
+
},
|
|
92
|
+
chunks: [jsxComponentChunk()],
|
|
93
|
+
options: {
|
|
94
|
+
workflows: { workflows: { [workflow.id]: workflow }, customNodes },
|
|
95
|
+
globalStateDefinitions,
|
|
96
|
+
},
|
|
97
|
+
dependencies: {},
|
|
98
|
+
}
|
|
99
|
+
await plugin(structure)
|
|
100
|
+
const componentChunk = structure.chunks.find((c: any) => c.name === 'jsx-component')
|
|
101
|
+
const moduleChunk = structure.chunks.find((c: any) => c.name === 'workflow-module')
|
|
102
|
+
return {
|
|
103
|
+
component: generate(componentChunk.content).code,
|
|
104
|
+
module: moduleChunk ? String(moduleChunk.content) : '',
|
|
105
|
+
dependencies: structure.dependencies,
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const HOOK =
|
|
110
|
+
/const \{\s*tickerTimerEnabled,\s*setTickerTimerEnabled,\s*refreshGlobalState\s*\} = useGlobalState\(\)/
|
|
111
|
+
const VALUE_IN_REF =
|
|
112
|
+
/__wfStateRef\.current = \{\s*count: count,\s*tickerTimerEnabled: tickerTimerEnabled\s*\}/
|
|
113
|
+
|
|
114
|
+
const expectGlobalStateWired = (file: Generated) => {
|
|
115
|
+
expect(file.dependencies.useGlobalState).toEqual({
|
|
116
|
+
type: 'local',
|
|
117
|
+
path: '@/global-state-context',
|
|
118
|
+
meta: { namedImport: true },
|
|
119
|
+
})
|
|
120
|
+
expect(file.component).toMatch(HOOK)
|
|
121
|
+
// Assigned on every render, after the hook: a change of the global value
|
|
122
|
+
// re-renders the file, and the next run reads the new value.
|
|
123
|
+
expect(file.component).toMatch(VALUE_IN_REF)
|
|
124
|
+
expect(file.component.indexOf('useGlobalState()')).toBeLessThan(
|
|
125
|
+
file.component.indexOf('__wfStateRef.current =')
|
|
126
|
+
)
|
|
127
|
+
// The runtime is told the state's type, and each run starts from the ref.
|
|
128
|
+
expect(file.component).toMatch(/tickerTimerEnabled: "boolean"/)
|
|
129
|
+
expect(file.module).toContain(
|
|
130
|
+
'triggerContext.__stateValues = Object.assign({}, stateValuesRef.current);'
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const expectNoGlobalStateWiring = (file: Generated) => {
|
|
135
|
+
expect(file.dependencies.useGlobalState).toBeUndefined()
|
|
136
|
+
expect(file.component).not.toContain('useGlobalState')
|
|
137
|
+
expect(file.component).not.toContain('tickerTimerEnabled')
|
|
138
|
+
expect(file.component).toMatch(/__wfStateRef\.current = \{\s*count: count\s*\}/)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const READ_SWITCH = node('read-switch', 'state-get-global-state', {
|
|
142
|
+
property: 'tickerTimerEnabled',
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
describe('a file whose workflows only read a global state', () => {
|
|
146
|
+
it('a component gets the import, the hook, and the value in its state ref', async () => {
|
|
147
|
+
const file = await generateFile({
|
|
148
|
+
workflow: timerWorkflow([READ_SWITCH, UPDATE_COUNT]),
|
|
149
|
+
isPage: false,
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
expectGlobalStateWired(file)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('a page gets the same', async () => {
|
|
156
|
+
const file = await generateFile({
|
|
157
|
+
workflow: timerWorkflow([READ_SWITCH, UPDATE_COUNT], {
|
|
158
|
+
scope: 'page',
|
|
159
|
+
config: { pageId: 'page-1' },
|
|
160
|
+
}),
|
|
161
|
+
isPage: true,
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
expectGlobalStateWired(file)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('counts a read made inside a custom node the workflow calls', async () => {
|
|
168
|
+
const file = await generateFile({
|
|
169
|
+
workflow: timerWorkflow([
|
|
170
|
+
node('call', 'general-custom-node', { customNodeId: 'shared-check' }),
|
|
171
|
+
UPDATE_COUNT,
|
|
172
|
+
]),
|
|
173
|
+
isPage: false,
|
|
174
|
+
customNodes: { 'shared-check': { id: 'shared-check', nodes: [READ_SWITCH], edges: [] } },
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
expectGlobalStateWired(file)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it.each([
|
|
181
|
+
['a {{state.<name>}} token', { value: '{{state.tickerTimerEnabled}}' }],
|
|
182
|
+
[
|
|
183
|
+
'a context reference into the state values',
|
|
184
|
+
{
|
|
185
|
+
value: {
|
|
186
|
+
type: 'workflowContext',
|
|
187
|
+
nodeId: 'tick',
|
|
188
|
+
path: ['tick', '__stateValues', 'tickerTimerEnabled'],
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
[
|
|
193
|
+
'an expression over the state values',
|
|
194
|
+
{ expression: 'context.__stateValues.tickerTimerEnabled === true' },
|
|
195
|
+
],
|
|
196
|
+
])('counts a read written as %s', async (_form, config) => {
|
|
197
|
+
const file = await generateFile({
|
|
198
|
+
workflow: timerWorkflow([node('gate', 'general-if-statement', config), UPDATE_COUNT]),
|
|
199
|
+
isPage: false,
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
expectGlobalStateWired(file)
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
describe('a file that uses no global state is generated as before', () => {
|
|
207
|
+
it('a workflow on local state only gets no hook, in a project that has global states', async () => {
|
|
208
|
+
const file = await generateFile({ workflow: timerWorkflow([UPDATE_COUNT]), isPage: false })
|
|
209
|
+
|
|
210
|
+
expectNoGlobalStateWiring(file)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it.each([
|
|
214
|
+
['a token naming a local state', { value: '{{state.count}}' }],
|
|
215
|
+
['an expression over the state values naming none', { expression: '__stateValues.count > 3' }],
|
|
216
|
+
[
|
|
217
|
+
'a name that merely contains a global state name',
|
|
218
|
+
{ expression: '__stateValues.tickerTimerEnabledLabel' },
|
|
219
|
+
],
|
|
220
|
+
])('%s gets no hook', async (_form, config) => {
|
|
221
|
+
const file = await generateFile({
|
|
222
|
+
workflow: timerWorkflow([node('gate', 'general-if-statement', config), UPDATE_COUNT]),
|
|
223
|
+
isPage: false,
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
expectNoGlobalStateWiring(file)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it('a read node in a project that declares no global state adds nothing', async () => {
|
|
230
|
+
const file = await generateFile({
|
|
231
|
+
workflow: timerWorkflow([READ_SWITCH, UPDATE_COUNT]),
|
|
232
|
+
isPage: false,
|
|
233
|
+
globalStateDefinitions: {},
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
expect(file.dependencies.useGlobalState).toBeUndefined()
|
|
237
|
+
expect(file.component).not.toContain('useGlobalState')
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
describe('the read node itself', () => {
|
|
242
|
+
it('returns the value the state ref held when the run started', async () => {
|
|
243
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
244
|
+
const handler = new Function(`return ${buildStateGetHandlerSource('state_get_global_state')}`)()
|
|
245
|
+
const stateValuesRef = { current: { tickerTimerEnabled: true } }
|
|
246
|
+
const context = { __stateValues: { ...stateValuesRef.current } }
|
|
247
|
+
|
|
248
|
+
await expect(handler({ property: 'tickerTimerEnabled' }, context)).resolves.toEqual({
|
|
249
|
+
value: true,
|
|
250
|
+
key: 'tickerTimerEnabled',
|
|
251
|
+
})
|
|
252
|
+
await expect(handler({ property: 'tickerTimerEnabled' }, {})).resolves.toEqual({
|
|
253
|
+
value: undefined,
|
|
254
|
+
key: 'tickerTimerEnabled',
|
|
255
|
+
})
|
|
256
|
+
})
|
|
257
|
+
})
|