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