@teleporthq/teleport-plugin-common 0.44.0-alpha.0 → 0.44.1

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.
Files changed (190) hide show
  1. package/__tests__/builders/html-builders.ts +9 -2
  2. package/__tests__/builders/style-builders.ts +23 -0
  3. package/__tests__/node-handlers/node-to-html/index.ts +2 -0
  4. package/__tests__/node-handlers/node-to-jsx/attribute-value-mapper.ts +77 -0
  5. package/__tests__/node-handlers/node-to-jsx/calendar-events-reviver.ts +176 -0
  6. package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-ecommerce-source.ts +79 -0
  7. package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-loading.ts +122 -0
  8. package/__tests__/node-handlers/node-to-jsx/index.ts +719 -3
  9. package/__tests__/node-handlers/node-to-jsx/url-search-params-expression.ts +59 -0
  10. package/__tests__/node-handlers/node-to-jsx/utils.ts +602 -3
  11. package/__tests__/utils/ast-utils.ts +188 -0
  12. package/__tests__/utils/content-property-sanitiser.ts +156 -0
  13. package/__tests__/utils/page-seeds-state-from-data-source.ts +70 -0
  14. package/__tests__/utils/parse-string-with-template-expressions.ts +82 -0
  15. package/__tests__/utils/resource-auth-scheme.ts +55 -0
  16. package/__tests__/utils/route-utils.ts +118 -0
  17. package/__tests__/utils/style-utils.ts +59 -0
  18. package/__tests__/utils/template-expression-balance.ts +83 -0
  19. package/__tests__/utils/url-query-writer.ts +239 -0
  20. package/__tests__/utils/url-search-param-sync.ts +138 -0
  21. package/__tests__/utils/url-search-params.ts +48 -0
  22. package/dist/cjs/builders/ast-builders.d.ts +2 -0
  23. package/dist/cjs/builders/ast-builders.d.ts.map +1 -1
  24. package/dist/cjs/builders/ast-builders.js +41 -3
  25. package/dist/cjs/builders/ast-builders.js.map +1 -1
  26. package/dist/cjs/builders/hast-builders.d.ts +1 -0
  27. package/dist/cjs/builders/hast-builders.d.ts.map +1 -1
  28. package/dist/cjs/builders/hast-builders.js +11 -1
  29. package/dist/cjs/builders/hast-builders.js.map +1 -1
  30. package/dist/cjs/builders/style-builders.d.ts +8 -2
  31. package/dist/cjs/builders/style-builders.d.ts.map +1 -1
  32. package/dist/cjs/builders/style-builders.js +65 -19
  33. package/dist/cjs/builders/style-builders.js.map +1 -1
  34. package/dist/cjs/index.d.ts +5 -1
  35. package/dist/cjs/index.d.ts.map +1 -1
  36. package/dist/cjs/index.js +9 -1
  37. package/dist/cjs/index.js.map +1 -1
  38. package/dist/cjs/node-handlers/node-to-html/constants.d.ts.map +1 -1
  39. package/dist/cjs/node-handlers/node-to-html/constants.js +2 -0
  40. package/dist/cjs/node-handlers/node-to-html/constants.js.map +1 -1
  41. package/dist/cjs/node-handlers/node-to-html/index.d.ts.map +1 -1
  42. package/dist/cjs/node-handlers/node-to-html/index.js +69 -1
  43. package/dist/cjs/node-handlers/node-to-html/index.js.map +1 -1
  44. package/dist/cjs/node-handlers/node-to-html/types.d.ts +5 -1
  45. package/dist/cjs/node-handlers/node-to-html/types.d.ts.map +1 -1
  46. package/dist/cjs/node-handlers/node-to-html/utils.d.ts +2 -1
  47. package/dist/cjs/node-handlers/node-to-html/utils.d.ts.map +1 -1
  48. package/dist/cjs/node-handlers/node-to-html/utils.js +174 -13
  49. package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
  50. package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  51. package/dist/cjs/node-handlers/node-to-jsx/index.js +1461 -66
  52. package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
  53. package/dist/cjs/node-handlers/node-to-jsx/types.d.ts +60 -6
  54. package/dist/cjs/node-handlers/node-to-jsx/types.d.ts.map +1 -1
  55. package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts +73 -7
  56. package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  57. package/dist/cjs/node-handlers/node-to-jsx/utils.js +864 -71
  58. package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
  59. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  60. package/dist/cjs/utils/ast-utils.d.ts +142 -11
  61. package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
  62. package/dist/cjs/utils/ast-utils.js +931 -75
  63. package/dist/cjs/utils/ast-utils.js.map +1 -1
  64. package/dist/cjs/utils/hast-utils.d.ts +8 -6
  65. package/dist/cjs/utils/hast-utils.d.ts.map +1 -1
  66. package/dist/cjs/utils/hast-utils.js +28 -4
  67. package/dist/cjs/utils/hast-utils.js.map +1 -1
  68. package/dist/cjs/utils/route-utils.d.ts +117 -0
  69. package/dist/cjs/utils/route-utils.d.ts.map +1 -0
  70. package/dist/cjs/utils/route-utils.js +212 -0
  71. package/dist/cjs/utils/route-utils.js.map +1 -0
  72. package/dist/cjs/utils/style-utils.d.ts +1 -0
  73. package/dist/cjs/utils/style-utils.d.ts.map +1 -1
  74. package/dist/cjs/utils/style-utils.js +181 -2
  75. package/dist/cjs/utils/style-utils.js.map +1 -1
  76. package/dist/cjs/utils/template-expression-balance.d.ts +58 -0
  77. package/dist/cjs/utils/template-expression-balance.d.ts.map +1 -0
  78. package/dist/cjs/utils/template-expression-balance.js +181 -0
  79. package/dist/cjs/utils/template-expression-balance.js.map +1 -0
  80. package/dist/cjs/utils/types.d.ts +3 -0
  81. package/dist/cjs/utils/types.d.ts.map +1 -0
  82. package/dist/cjs/utils/types.js +3 -0
  83. package/dist/cjs/utils/types.js.map +1 -0
  84. package/dist/cjs/utils/url-query-writer.d.ts +123 -0
  85. package/dist/cjs/utils/url-query-writer.d.ts.map +1 -0
  86. package/dist/cjs/utils/url-query-writer.js +260 -0
  87. package/dist/cjs/utils/url-query-writer.js.map +1 -0
  88. package/dist/cjs/utils/url-search-param-sync.d.ts +4 -0
  89. package/dist/cjs/utils/url-search-param-sync.d.ts.map +1 -0
  90. package/dist/cjs/utils/url-search-param-sync.js +241 -0
  91. package/dist/cjs/utils/url-search-param-sync.js.map +1 -0
  92. package/dist/cjs/utils/url-search-params.d.ts +48 -0
  93. package/dist/cjs/utils/url-search-params.d.ts.map +1 -0
  94. package/dist/cjs/utils/url-search-params.js +109 -0
  95. package/dist/cjs/utils/url-search-params.js.map +1 -0
  96. package/dist/esm/builders/ast-builders.d.ts +2 -0
  97. package/dist/esm/builders/ast-builders.d.ts.map +1 -1
  98. package/dist/esm/builders/ast-builders.js +40 -4
  99. package/dist/esm/builders/ast-builders.js.map +1 -1
  100. package/dist/esm/builders/hast-builders.d.ts +1 -0
  101. package/dist/esm/builders/hast-builders.d.ts.map +1 -1
  102. package/dist/esm/builders/hast-builders.js +9 -0
  103. package/dist/esm/builders/hast-builders.js.map +1 -1
  104. package/dist/esm/builders/style-builders.d.ts +8 -2
  105. package/dist/esm/builders/style-builders.d.ts.map +1 -1
  106. package/dist/esm/builders/style-builders.js +63 -18
  107. package/dist/esm/builders/style-builders.js.map +1 -1
  108. package/dist/esm/index.d.ts +5 -1
  109. package/dist/esm/index.d.ts.map +1 -1
  110. package/dist/esm/index.js +5 -1
  111. package/dist/esm/index.js.map +1 -1
  112. package/dist/esm/node-handlers/node-to-html/constants.d.ts.map +1 -1
  113. package/dist/esm/node-handlers/node-to-html/constants.js +2 -0
  114. package/dist/esm/node-handlers/node-to-html/constants.js.map +1 -1
  115. package/dist/esm/node-handlers/node-to-html/index.d.ts.map +1 -1
  116. package/dist/esm/node-handlers/node-to-html/index.js +71 -3
  117. package/dist/esm/node-handlers/node-to-html/index.js.map +1 -1
  118. package/dist/esm/node-handlers/node-to-html/types.d.ts +5 -1
  119. package/dist/esm/node-handlers/node-to-html/types.d.ts.map +1 -1
  120. package/dist/esm/node-handlers/node-to-html/utils.d.ts +2 -1
  121. package/dist/esm/node-handlers/node-to-html/utils.d.ts.map +1 -1
  122. package/dist/esm/node-handlers/node-to-html/utils.js +170 -13
  123. package/dist/esm/node-handlers/node-to-html/utils.js.map +1 -1
  124. package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  125. package/dist/esm/node-handlers/node-to-jsx/index.js +1460 -68
  126. package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
  127. package/dist/esm/node-handlers/node-to-jsx/types.d.ts +60 -6
  128. package/dist/esm/node-handlers/node-to-jsx/types.d.ts.map +1 -1
  129. package/dist/esm/node-handlers/node-to-jsx/utils.d.ts +73 -7
  130. package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  131. package/dist/esm/node-handlers/node-to-jsx/utils.js +857 -69
  132. package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
  133. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  134. package/dist/esm/utils/ast-utils.d.ts +142 -11
  135. package/dist/esm/utils/ast-utils.d.ts.map +1 -1
  136. package/dist/esm/utils/ast-utils.js +911 -75
  137. package/dist/esm/utils/ast-utils.js.map +1 -1
  138. package/dist/esm/utils/hast-utils.d.ts +8 -6
  139. package/dist/esm/utils/hast-utils.d.ts.map +1 -1
  140. package/dist/esm/utils/hast-utils.js +26 -3
  141. package/dist/esm/utils/hast-utils.js.map +1 -1
  142. package/dist/esm/utils/route-utils.d.ts +117 -0
  143. package/dist/esm/utils/route-utils.d.ts.map +1 -0
  144. package/dist/esm/utils/route-utils.js +179 -0
  145. package/dist/esm/utils/route-utils.js.map +1 -0
  146. package/dist/esm/utils/style-utils.d.ts +1 -0
  147. package/dist/esm/utils/style-utils.d.ts.map +1 -1
  148. package/dist/esm/utils/style-utils.js +179 -1
  149. package/dist/esm/utils/style-utils.js.map +1 -1
  150. package/dist/esm/utils/template-expression-balance.d.ts +58 -0
  151. package/dist/esm/utils/template-expression-balance.d.ts.map +1 -0
  152. package/dist/esm/utils/template-expression-balance.js +174 -0
  153. package/dist/esm/utils/template-expression-balance.js.map +1 -0
  154. package/dist/esm/utils/types.d.ts +3 -0
  155. package/dist/esm/utils/types.d.ts.map +1 -0
  156. package/dist/esm/utils/types.js +2 -0
  157. package/dist/esm/utils/types.js.map +1 -0
  158. package/dist/esm/utils/url-query-writer.d.ts +123 -0
  159. package/dist/esm/utils/url-query-writer.d.ts.map +1 -0
  160. package/dist/esm/utils/url-query-writer.js +229 -0
  161. package/dist/esm/utils/url-query-writer.js.map +1 -0
  162. package/dist/esm/utils/url-search-param-sync.d.ts +4 -0
  163. package/dist/esm/utils/url-search-param-sync.d.ts.map +1 -0
  164. package/dist/esm/utils/url-search-param-sync.js +213 -0
  165. package/dist/esm/utils/url-search-param-sync.js.map +1 -0
  166. package/dist/esm/utils/url-search-params.d.ts +48 -0
  167. package/dist/esm/utils/url-search-params.d.ts.map +1 -0
  168. package/dist/esm/utils/url-search-params.js +81 -0
  169. package/dist/esm/utils/url-search-params.js.map +1 -0
  170. package/package.json +4 -4
  171. package/src/builders/ast-builders.ts +80 -3
  172. package/src/builders/hast-builders.ts +11 -0
  173. package/src/builders/style-builders.ts +107 -27
  174. package/src/index.ts +18 -1
  175. package/src/node-handlers/node-to-html/constants.ts +2 -0
  176. package/src/node-handlers/node-to-html/index.ts +126 -6
  177. package/src/node-handlers/node-to-html/types.ts +6 -0
  178. package/src/node-handlers/node-to-html/utils.ts +228 -14
  179. package/src/node-handlers/node-to-jsx/index.ts +2219 -111
  180. package/src/node-handlers/node-to-jsx/types.ts +75 -32
  181. package/src/node-handlers/node-to-jsx/utils.ts +1142 -84
  182. package/src/utils/ast-utils.ts +1223 -127
  183. package/src/utils/hast-utils.ts +37 -9
  184. package/src/utils/route-utils.ts +214 -0
  185. package/src/utils/style-utils.ts +183 -1
  186. package/src/utils/template-expression-balance.ts +196 -0
  187. package/src/utils/types.ts +25 -0
  188. package/src/utils/url-query-writer.ts +318 -0
  189. package/src/utils/url-search-param-sync.ts +303 -0
  190. package/src/utils/url-search-params.ts +122 -0
@@ -1,7 +1,13 @@
1
+ import generate from '@babel/generator'
2
+ import * as types from '@babel/types'
1
3
  import {
2
4
  createBinaryExpression,
3
5
  createConditionIdentifier,
6
+ createStateChangeStatement,
7
+ createDynamicValueExpression,
8
+ makeControlUncontrolledWhenNoChangeHandler,
4
9
  } from '../../../src/node-handlers/node-to-jsx/utils'
10
+ import { UIDLStateDefinition, UIDLStateModifierEvent } from '@teleporthq/teleport-types'
5
11
  import { dynamicNode } from '@teleporthq/teleport-uidl-builders'
6
12
  import {
7
13
  JSXGenerationParams,
@@ -104,6 +110,78 @@ describe('createBinaryExpression', () => {
104
110
  prefix: true,
105
111
  })
106
112
  })
113
+
114
+ // A rendering condition exists to HIDE a node — it must never be able to throw.
115
+ // The bound value is nullish far more often than it looks (an optional CMS
116
+ // column, a category with no image, an unhydrated state), and a bare
117
+ // `x.length` / `x.includes(...)` took whole pages down with
118
+ // "Cannot read properties of null (reading 'length')".
119
+ describe('collection operators are null-safe', () => {
120
+ const codeOf = (
121
+ condition: { operation: string; operand?: string; containsField?: string },
122
+ type = 'array'
123
+ ) => generate(createBinaryExpression(condition, { key: 'imageUrl', type }) as types.Node).code
124
+
125
+ it('isNotEmpty / isEmpty coerce a nullish value to an empty collection', () => {
126
+ expect(codeOf({ operation: 'isNotEmpty' })).toBe('(imageUrl || []).length > 0')
127
+ expect(codeOf({ operation: 'isEmpty' })).toBe('(imageUrl || []).length === 0')
128
+ })
129
+
130
+ it('objects go through Object.keys on a non-null target', () => {
131
+ expect(codeOf({ operation: 'isNotEmpty' }, 'object')).toBe(
132
+ 'Object.keys(imageUrl || {}).length > 0'
133
+ )
134
+ })
135
+
136
+ it('length comparisons guard the same way', () => {
137
+ expect(codeOf({ operation: 'lengthGreaterThan', operand: '2' })).toBe(
138
+ '(imageUrl || []).length > 2'
139
+ )
140
+ })
141
+
142
+ it('contains / notContains guard the receiver of includes()', () => {
143
+ expect(codeOf({ operation: 'contains', operand: 'a' })).toBe('(imageUrl || []).includes("a")')
144
+ expect(codeOf({ operation: 'notContains', operand: 'a' })).toBe(
145
+ '!(imageUrl || []).includes("a")'
146
+ )
147
+ })
148
+
149
+ it('contains / notContains keep the authored type of the element operand', () => {
150
+ // An array state holds what a workflow pushed — strings off `data-*`
151
+ // attributes — so a static "5" must stay "5": parsed as the collection's
152
+ // type it became the number 5 and never matched ["5"].
153
+ expect(codeOf({ operation: 'contains', operand: '5' })).toBe('(imageUrl || []).includes("5")')
154
+ expect(codeOf({ operation: 'notContains', operand: 'true' })).toBe(
155
+ '!(imageUrl || []).includes("true")'
156
+ )
157
+ expect(codeOf({ operation: 'contains', operand: '5', containsField: 'id' })).toBe(
158
+ '(imageUrl || []).some(__item => __item.id === "5")'
159
+ )
160
+ // Length comparisons still read their operand as a number.
161
+ expect(codeOf({ operation: 'lengthEquals', operand: '2' })).toBe(
162
+ '(imageUrl || []).length === 2'
163
+ )
164
+ })
165
+
166
+ it('hasKey passes a non-null receiver to hasOwnProperty', () => {
167
+ expect(codeOf({ operation: 'hasKey', operand: 'a' }, 'object')).toBe(
168
+ 'Object.prototype.hasOwnProperty.call(imageUrl || {}, "a")'
169
+ )
170
+ })
171
+
172
+ it('a non-empty string still reads as non-empty (string semantics preserved)', () => {
173
+ // eslint-disable-next-line no-eval
174
+ const evaluate = (expr: string, imageUrl: unknown) =>
175
+ // tslint:disable-next-line function-constructor
176
+ new Function('imageUrl', `return ${expr}`)(imageUrl)
177
+
178
+ const expression = codeOf({ operation: 'isNotEmpty' })
179
+ expect(evaluate(expression, 'https://cdn/x.png')).toBe(true)
180
+ expect(evaluate(expression, '')).toBe(false)
181
+ expect(evaluate(expression, null)).toBe(false)
182
+ expect(evaluate(expression, undefined)).toBe(false)
183
+ })
184
+ })
107
185
  })
108
186
 
109
187
  describe('createConditionIdentifier', () => {
@@ -122,8 +200,15 @@ describe('createConditionIdentifier', () => {
122
200
  defaultValue: false,
123
201
  },
124
202
  },
203
+ globalStateDefinitions: {},
125
204
  dependencies: {},
126
205
  nodesLookup: {},
206
+ windowImports: {},
207
+ localeReferences: [],
208
+ localeAttributeReferences: [],
209
+ globalReferences: [],
210
+ globalStateReferences: [],
211
+ hoistedConstants: [],
127
212
  }
128
213
 
129
214
  const options: JSXGenerationOptions = {
@@ -144,10 +229,10 @@ describe('createConditionIdentifier', () => {
144
229
  })
145
230
 
146
231
  it('works on member expressions', () => {
147
- const node = dynamicNode('prop', 'fields.title')
232
+ const node = dynamicNode('prop', 'fields', ['title'])
148
233
  const result = createConditionIdentifier(node, params, options)
149
234
 
150
- expect(result.key).toBe('fields.title')
235
+ expect(result.key).toBe("fields?.['title']")
151
236
  expect(result.prefix).toBe('this.props')
152
237
  expect(result.type).toBe('object')
153
238
  })
@@ -161,8 +246,522 @@ describe('createConditionIdentifier', () => {
161
246
  expect(result.type).toBe('boolean')
162
247
  })
163
248
 
249
+ it('works with a local reference', () => {
250
+ const node = dynamicNode('local', 'item', ['type'])
251
+ const optionsWithLocalIdentifier = {
252
+ ...options,
253
+ localIdentifier: 'item',
254
+ }
255
+ const result = createConditionIdentifier(node, params, optionsWithLocalIdentifier)
256
+
257
+ expect(result.key).toBe('type')
258
+ expect(result.prefix).toBe('item')
259
+ expect(result.type).toBe('string')
260
+ })
261
+
262
+ it('resolves a page-root local reference against the details-page exposeAs name', () => {
263
+ const node = dynamicNode('local', 'id', ['id'])
264
+ const detailsOptions = {
265
+ ...options,
266
+ dynamicReferencePrefixMap: {
267
+ prop: 'props',
268
+ state: '',
269
+ local: '',
270
+ },
271
+ detailsPageExposeAsName: 'User',
272
+ }
273
+ const result = createConditionIdentifier(node, params, detailsOptions)
274
+
275
+ expect(result.key).toBe('User.id')
276
+ expect(result.prefix).toBe('props')
277
+ expect(result.type).toBe('string')
278
+ })
279
+
280
+ it('keeps repeater semantics for local refs inside a loop', () => {
281
+ const node = dynamicNode('local', 'item', ['type'])
282
+ const detailsInsideLoopOptions = {
283
+ ...options,
284
+ localIdentifier: 'item',
285
+ detailsPageExposeAsName: 'User',
286
+ }
287
+ const result = createConditionIdentifier(node, params, detailsInsideLoopOptions)
288
+
289
+ expect(result.key).toBe('type')
290
+ expect(result.prefix).toBe('item')
291
+ })
292
+
164
293
  it('throws an error for unknown reference type', () => {
165
- const node = dynamicNode('local', 'title')
294
+ const node = dynamicNode('locale', 'title')
166
295
  expect(() => createConditionIdentifier(node, params, options)).toThrowError()
167
296
  })
168
297
  })
298
+
299
+ describe('createStateChangeStatement — plan v14 type-aware emitter', () => {
300
+ // We render each AST back to source so the assertions read like the code
301
+ // the React component would actually contain.
302
+ const render = (stmt: ReturnType<typeof createStateChangeStatement>): string => {
303
+ if (!stmt) {
304
+ return ''
305
+ }
306
+ return generate(stmt).code.replace(/[\s;]+$/g, '')
307
+ }
308
+
309
+ const hooksOptions: JSXGenerationOptions = {
310
+ stateHandling: 'hooks',
311
+ dynamicReferencePrefixMap: { prop: '', state: '', local: '' },
312
+ }
313
+
314
+ const stateDefs = (
315
+ overrides: Record<string, UIDLStateDefinition>
316
+ ): Record<string, UIDLStateDefinition> => ({
317
+ isOpen: { type: 'boolean', defaultValue: false },
318
+ activeStep: { type: 'number', defaultValue: 1 },
319
+ quizAnswers: { type: 'object', defaultValue: {} },
320
+ tags: { type: 'array', defaultValue: [] },
321
+ selectedSkinType: { type: 'string', defaultValue: '' },
322
+ ...overrides,
323
+ })
324
+
325
+ it('toggles a boolean state with !x (existing behaviour preserved)', () => {
326
+ const event: UIDLStateModifierEvent = {
327
+ type: 'stateChange',
328
+ modifies: 'isOpen',
329
+ newState: '$toggle',
330
+ }
331
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
332
+ expect(render(stmt)).toBe('setIsOpen(!isOpen)')
333
+ })
334
+
335
+ it('refuses to toggle a numeric state (no setter emitted)', () => {
336
+ const event: UIDLStateModifierEvent = {
337
+ type: 'stateChange',
338
+ modifies: 'activeStep',
339
+ newState: '$toggle',
340
+ }
341
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
342
+ expect(stmt).toBeNull()
343
+ })
344
+
345
+ it('refuses to toggle an object state (no setter emitted)', () => {
346
+ const event: UIDLStateModifierEvent = {
347
+ type: 'stateChange',
348
+ modifies: 'quizAnswers',
349
+ newState: '$toggle',
350
+ }
351
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
352
+ expect(stmt).toBeNull()
353
+ })
354
+
355
+ it('refuses to overwrite object state with a primitive', () => {
356
+ const event: UIDLStateModifierEvent = {
357
+ type: 'stateChange',
358
+ modifies: 'quizAnswers',
359
+ newState: 'oily',
360
+ }
361
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
362
+ expect(stmt).toBeNull()
363
+ })
364
+
365
+ it('writes a literal value to a numeric state', () => {
366
+ const event: UIDLStateModifierEvent = {
367
+ type: 'stateChange',
368
+ modifies: 'activeStep',
369
+ newState: 2,
370
+ }
371
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
372
+ expect(render(stmt)).toBe('setActiveStep(2)')
373
+ })
374
+
375
+ it('writes a literal value to a string state', () => {
376
+ const event: UIDLStateModifierEvent = {
377
+ type: 'stateChange',
378
+ modifies: 'selectedSkinType',
379
+ newState: 'Oily',
380
+ }
381
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
382
+ expect(render(stmt)).toBe('setSelectedSkinType("Oily")')
383
+ })
384
+
385
+ it('emits a functional setter for $increment on numeric state', () => {
386
+ const event: UIDLStateModifierEvent = {
387
+ type: 'stateChange',
388
+ modifies: 'activeStep',
389
+ newState: { type: '$increment' },
390
+ }
391
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
392
+ expect(render(stmt)).toBe('setActiveStep(prev => prev + 1)')
393
+ })
394
+
395
+ it('emits a functional setter for $decrement with custom delta', () => {
396
+ const event: UIDLStateModifierEvent = {
397
+ type: 'stateChange',
398
+ modifies: 'activeStep',
399
+ newState: { type: '$decrement', delta: 2 },
400
+ }
401
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
402
+ expect(render(stmt)).toBe('setActiveStep(prev => prev - 2)')
403
+ })
404
+
405
+ it('honors negative delta on $increment as a decrement', () => {
406
+ const event: UIDLStateModifierEvent = {
407
+ type: 'stateChange',
408
+ modifies: 'activeStep',
409
+ newState: { type: '$increment', delta: -2 },
410
+ }
411
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
412
+ expect(render(stmt)).toBe('setActiveStep(prev => prev - 2)')
413
+ })
414
+
415
+ it('refuses $increment on non-numeric state', () => {
416
+ const event: UIDLStateModifierEvent = {
417
+ type: 'stateChange',
418
+ modifies: 'selectedSkinType',
419
+ newState: { type: '$increment' },
420
+ }
421
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
422
+ expect(stmt).toBeNull()
423
+ })
424
+
425
+ it('emits a $patch setter for an object state with a path', () => {
426
+ const event: UIDLStateModifierEvent = {
427
+ type: 'stateChange',
428
+ modifies: 'quizAnswers',
429
+ newState: { type: '$patch', path: 'skinType', value: 'Oily' },
430
+ }
431
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
432
+ expect(render(stmt)).toBe('setQuizAnswers(prev => ({\n ...prev,\n skinType: "Oily"\n}))')
433
+ })
434
+
435
+ it('refuses $patch when the state is not declared as object', () => {
436
+ const event: UIDLStateModifierEvent = {
437
+ type: 'stateChange',
438
+ modifies: 'activeStep',
439
+ newState: { type: '$patch', path: 'foo', value: 'bar' },
440
+ }
441
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
442
+ expect(stmt).toBeNull()
443
+ })
444
+
445
+ it('emits an $append setter for an array state', () => {
446
+ const event: UIDLStateModifierEvent = {
447
+ type: 'stateChange',
448
+ modifies: 'tags',
449
+ newState: { type: '$append', value: 'sale' },
450
+ }
451
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
452
+ expect(render(stmt)).toBe('setTags(prev => [...prev, "sale"])')
453
+ })
454
+
455
+ it('refuses $append when the state is not declared as array', () => {
456
+ const event: UIDLStateModifierEvent = {
457
+ type: 'stateChange',
458
+ modifies: 'quizAnswers',
459
+ newState: { type: '$append', value: 'x' },
460
+ }
461
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
462
+ expect(stmt).toBeNull()
463
+ })
464
+
465
+ it('refuses a stateChange targeting an undeclared state', () => {
466
+ const event: UIDLStateModifierEvent = {
467
+ type: 'stateChange',
468
+ modifies: 'doesNotExist',
469
+ newState: 1,
470
+ }
471
+ const stmt = createStateChangeStatement(event, stateDefs({}), hooksOptions)
472
+ expect(stmt).toBeNull()
473
+ })
474
+ })
475
+
476
+ describe('makeControlUncontrolledWhenNoChangeHandler', () => {
477
+ const attr = (name: string, value?: string): types.JSXAttribute =>
478
+ types.jsxAttribute(
479
+ types.jsxIdentifier(name),
480
+ value === undefined ? null : types.jsxExpressionContainer(types.identifier(value))
481
+ )
482
+
483
+ const buildTag = (name: string, attrs: types.JSXAttribute[]): types.JSXElement => {
484
+ const opening = types.jsxOpeningElement(types.jsxIdentifier(name), attrs, true)
485
+ return types.jsxElement(opening, null, [], true)
486
+ }
487
+
488
+ const attrNames = (tag: types.JSXElement): string[] =>
489
+ tag.openingElement.attributes
490
+ .filter((a): a is types.JSXAttribute => a.type === 'JSXAttribute')
491
+ .map((a) => (a.name.type === 'JSXIdentifier' ? a.name.name : ''))
492
+
493
+ it('converts value -> defaultValue on an input with no onChange (frozen edit field)', () => {
494
+ const tag = buildTag('input', [attr('value', 'props.titleItem?.title')])
495
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
496
+ expect(attrNames(tag)).toContain('defaultValue')
497
+ expect(attrNames(tag)).not.toContain('value')
498
+ // the bound expression is preserved
499
+ expect(generate(tag).code).toContain('defaultValue={props.titleItem?.title}')
500
+ })
501
+
502
+ it('leaves value untouched when an onChange handler is present (controlled)', () => {
503
+ const tag = buildTag('input', [attr('value', 'title'), attr('onChange', 'handler')])
504
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
505
+ expect(attrNames(tag)).toContain('value')
506
+ expect(attrNames(tag)).not.toContain('defaultValue')
507
+ })
508
+
509
+ it('leaves readOnly display inputs untouched', () => {
510
+ const tag = buildTag('input', [attr('value', 'x'), attr('readOnly')])
511
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
512
+ expect(attrNames(tag)).toContain('value')
513
+ expect(attrNames(tag)).not.toContain('defaultValue')
514
+ })
515
+
516
+ it('leaves disabled inputs untouched', () => {
517
+ const tag = buildTag('input', [attr('value', 'x'), attr('disabled')])
518
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
519
+ expect(attrNames(tag)).toContain('value')
520
+ })
521
+
522
+ it('converts checked -> defaultChecked on a checkbox with no onChange', () => {
523
+ const tag = buildTag('input', [attr('type', undefined), attr('checked', 'isSeries')])
524
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
525
+ expect(attrNames(tag)).toContain('defaultChecked')
526
+ expect(attrNames(tag)).not.toContain('checked')
527
+ })
528
+
529
+ it('converts value on textarea and select', () => {
530
+ const ta = buildTag('textarea', [attr('value', 'overview')])
531
+ makeControlUncontrolledWhenNoChangeHandler(ta, 'textarea')
532
+ expect(attrNames(ta)).toContain('defaultValue')
533
+
534
+ const sel = buildTag('select', [attr('value', 'genre')])
535
+ makeControlUncontrolledWhenNoChangeHandler(sel, 'select')
536
+ expect(attrNames(sel)).toContain('defaultValue')
537
+ })
538
+
539
+ it('ignores non-form elements (a div keeps value)', () => {
540
+ const tag = buildTag('div', [attr('value', 'x')])
541
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'div')
542
+ expect(attrNames(tag)).toContain('value')
543
+ expect(attrNames(tag)).not.toContain('defaultValue')
544
+ })
545
+
546
+ // A `type="..."` attribute carries a StringLiteral value, not an expression.
547
+ const typeAttr = (value: string): types.JSXAttribute =>
548
+ types.jsxAttribute(types.jsxIdentifier('type'), types.stringLiteral(value))
549
+
550
+ it('formats a datetime-local prefill to YYYY-MM-DDTHH:mm so the value is not rejected', () => {
551
+ const tag = buildTag('input', [
552
+ typeAttr('datetime-local'),
553
+ attr('value', 'props.eventItem?.start_time'),
554
+ ])
555
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
556
+ expect(attrNames(tag)).toContain('defaultValue')
557
+ expect(generate(tag).code).toContain(
558
+ 'defaultValue={String(props.eventItem?.start_time || "").slice(0, 16)}'
559
+ )
560
+ })
561
+
562
+ it('formats a date prefill to YYYY-MM-DD', () => {
563
+ const tag = buildTag('input', [typeAttr('date'), attr('value', 'props.item?.day')])
564
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
565
+ expect(generate(tag).code).toContain(
566
+ 'defaultValue={String(props.item?.day || "").slice(0, 10)}'
567
+ )
568
+ })
569
+
570
+ it('formats a month prefill to YYYY-MM', () => {
571
+ const tag = buildTag('input', [typeAttr('month'), attr('value', 'props.item?.period')])
572
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
573
+ expect(generate(tag).code).toContain(
574
+ 'defaultValue={String(props.item?.period || "").slice(0, 7)}'
575
+ )
576
+ })
577
+
578
+ it('formats a time prefill to HH:mm, stripping any leading date', () => {
579
+ const tag = buildTag('input', [typeAttr('time'), attr('value', 'props.item?.opens_at')])
580
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
581
+ const code = generate(tag).code
582
+ expect(code).toContain('.replace(')
583
+ expect(code).toContain('.slice(0, 5)')
584
+ })
585
+
586
+ it('does NOT reformat a plain text input (only date/time types are wrapped)', () => {
587
+ const tag = buildTag('input', [typeAttr('text'), attr('value', 'props.item?.title')])
588
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
589
+ expect(generate(tag).code).toContain('defaultValue={props.item?.title}')
590
+ })
591
+
592
+ it('leaves a controlled datetime-local (with onChange) untouched — no wrap, keeps value', () => {
593
+ const tag = buildTag('input', [
594
+ typeAttr('datetime-local'),
595
+ attr('value', 'props.item?.start_time'),
596
+ attr('onChange', 'handler'),
597
+ ])
598
+ makeControlUncontrolledWhenNoChangeHandler(tag, 'input')
599
+ expect(attrNames(tag)).toContain('value')
600
+ expect(attrNames(tag)).not.toContain('defaultValue')
601
+ })
602
+ })
603
+
604
+ describe('createDynamicValueExpression — ctx reference (never emits an undeclared identifier)', () => {
605
+ const ctxRef = (refPath: string[], fallbackValue?: unknown) =>
606
+ ({
607
+ type: 'dynamic',
608
+ content: { referenceType: 'ctx', id: 'TQ_q-GFGA6h4I', refPath, fallbackValue },
609
+ } as any)
610
+
611
+ const code = (node: ReturnType<typeof createDynamicValueExpression>) => generate(node).code
612
+
613
+ it('resolves an unresolvable ctx ref on a details page to props.<exposeAs>.<refPath>', () => {
614
+ const opts = {
615
+ dynamicReferencePrefixMap: { prop: 'props', state: '', local: '' },
616
+ detailsPageExposeAsName: 'webinar',
617
+ } as any
618
+ // run 1b6eb5ba: the countdown target — must be props.webinar?.scheduled_at,
619
+ // NOT the undeclared identifier `tQQGFGA6h4I`.
620
+ expect(code(createDynamicValueExpression(ctxRef(['scheduled_at'], '2026-12-31'), opts))).toBe(
621
+ 'props.webinar?.scheduled_at'
622
+ )
623
+ })
624
+
625
+ it('falls back to the declared fallbackValue literal when no context/exposeAs resolves', () => {
626
+ const opts = { dynamicReferencePrefixMap: { prop: 'props', state: '', local: '' } } as any
627
+ expect(code(createDynamicValueExpression(ctxRef(['scheduled_at'], '2026-12-31'), opts))).toBe(
628
+ '"2026-12-31"'
629
+ )
630
+ })
631
+
632
+ it('falls back to undefined (never a bare identifier) with no exposeAs and no fallbackValue', () => {
633
+ const opts = { dynamicReferencePrefixMap: { prop: 'props', state: '', local: '' } } as any
634
+ expect(code(createDynamicValueExpression(ctxRef(['scheduled_at']), opts))).toBe('undefined')
635
+ })
636
+
637
+ it('still resolves via the ctx render-prop prefix when one is configured', () => {
638
+ const opts = { dynamicReferencePrefixMap: { ctx: 'row', prop: 'props' } } as any
639
+ expect(code(createDynamicValueExpression(ctxRef(['scheduled_at']), opts))).toBe(
640
+ 'row?.scheduled_at'
641
+ )
642
+ })
643
+ })
644
+
645
+ // An `expr` reference carries raw JavaScript, not the name of a binding. The
646
+ // domain-to-UIDL mapper emits the literal `false` whenever a condition's
647
+ // state/prop reference cannot be resolved (an orphaned or renamed definition),
648
+ // documenting it as "renders nothing" — but the sanitiser that keeps a state
649
+ // named `class` compiling used to rewrite it to `false_`, and the generated
650
+ // page died server-side with `ReferenceError: false_ is not defined` before
651
+ // rendering a single element.
652
+ describe('createBinaryExpression — literal expr references', () => {
653
+ const render = (
654
+ key: string,
655
+ condition: { operation: string; operand?: string | number | boolean }
656
+ ): string =>
657
+ generate(
658
+ createBinaryExpression(condition, { key, type: 'boolean', isExpression: true }) as types.Node
659
+ ).code
660
+
661
+ // `'='` is what the domain-to-UIDL mapper actually writes for an equality
662
+ // condition, so this is the exact shape that crashed the generated page.
663
+ it('emits the unresolved-reference fallback as the literal false', () => {
664
+ expect(render('false', { operation: '=', operand: false })).toBe('false === false')
665
+ expect(render('false', { operation: '=', operand: true })).toBe('false === true')
666
+ })
667
+
668
+ it('keeps the boolean short forms for a strict-equality condition', () => {
669
+ expect(render('true', { operation: '===', operand: true })).toBe('true')
670
+ expect(render('false', { operation: '===', operand: false })).toBe('!false')
671
+ })
672
+
673
+ it('emits literal null, undefined and numbers', () => {
674
+ expect(render('null', { operation: '!==', operand: 'x' })).toBe(`null !== "x"`)
675
+ expect(render('undefined', { operation: '!==', operand: 'x' })).toBe(`undefined !== "x"`)
676
+ expect(render('0', { operation: '>', operand: 1 })).toBe('0 > 1')
677
+ expect(render('-2.5', { operation: '<', operand: 1 })).toBe('-2.5 < 1')
678
+ })
679
+
680
+ it('never sanitises a literal into an undefined binding', () => {
681
+ expect(render('false', { operation: '=', operand: false })).not.toContain('false_')
682
+ })
683
+
684
+ it('leaves a real expression body untouched', () => {
685
+ expect(render('chatMessage?.sender', { operation: '===', operand: 'ai' })).toBe(
686
+ `chatMessage?.sender === "ai"`
687
+ )
688
+ })
689
+
690
+ it('still sanitises a reserved BINDING name, which is not an expression', () => {
691
+ expect(
692
+ generate(
693
+ createBinaryExpression(
694
+ { operation: '===', operand: true },
695
+ {
696
+ key: 'class',
697
+ type: 'boolean',
698
+ }
699
+ ) as types.Node
700
+ ).code
701
+ ).toBe('class_')
702
+ })
703
+ })
704
+
705
+ describe('createConditionIdentifier — expr references are flagged as source', () => {
706
+ const params: JSXGenerationParams = {
707
+ propDefinitions: {},
708
+ stateDefinitions: {},
709
+ globalStateDefinitions: {},
710
+ dependencies: {},
711
+ nodesLookup: {},
712
+ windowImports: {},
713
+ localeReferences: [],
714
+ localeAttributeReferences: [],
715
+ globalReferences: [],
716
+ globalStateReferences: [],
717
+ hoistedConstants: [],
718
+ }
719
+ const options: JSXGenerationOptions = {
720
+ dynamicReferencePrefixMap: { prop: 'props', state: '', local: '' },
721
+ }
722
+
723
+ it('marks an expr VALUE reference', () => {
724
+ const result = createConditionIdentifier({ type: 'expr', content: 'false' }, params, options)
725
+
726
+ expect(result).toEqual({ key: 'false', type: 'boolean', isExpression: true })
727
+ })
728
+
729
+ it('does not mark a state reference', () => {
730
+ const result = createConditionIdentifier(dynamicNode('state', 'isActive'), params, options)
731
+
732
+ expect(result.isExpression).toBeUndefined()
733
+ })
734
+ })
735
+
736
+ /*
737
+ An operand that is itself a BINDING rather than a literal — "is this row the
738
+ selected one", where the left side is the row being repeated and the right
739
+ side is a state holding the selected id. It is the only shape that can
740
+ express a comparison between two live values in a rendering condition, and
741
+ the Custom Pages live preview draws its selection ring with it.
742
+
743
+ Both runtimes have to agree: the editor's renderer resolves the same operand
744
+ through `state?.[operand.content.id]`.
745
+ */
746
+ describe('createBinaryExpression — an operand that is a dynamic reference', () => {
747
+ const render = (operand: unknown, operation = '='): string =>
748
+ generate(
749
+ createBinaryExpression({ operation, operand } as never, {
750
+ key: 'block?.id',
751
+ type: 'string',
752
+ isExpression: true,
753
+ }) as types.Node
754
+ ).code
755
+
756
+ it('compares against the state itself, not against its name as a string', () => {
757
+ expect(render(dynamicNode('state', 'activeEntry'))).toBe('block?.id === activeEntry')
758
+ })
759
+
760
+ it('reaches a prop operand through props', () => {
761
+ expect(render(dynamicNode('prop', 'selectedId'))).toBe('block?.id === props.selectedId')
762
+ })
763
+
764
+ it('still treats a plain string operand as a literal', () => {
765
+ expect(render('activeEntry')).toBe('block?.id === "activeEntry"')
766
+ })
767
+ })