@domql/element 3.1.2 → 3.2.3

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 (90) hide show
  1. package/README.md +4 -6
  2. package/__tests__/checkIfOnUpdate.test.js +12 -12
  3. package/__tests__/children.test.js +34 -38
  4. package/__tests__/inheritStateUpdates.test.js +12 -12
  5. package/__tests__/renderElement.test.js +11 -11
  6. package/__tests__/resetElement.test.js +6 -6
  7. package/__tests__/set.test.js +32 -36
  8. package/__tests__/throughExecProps.test.js +12 -12
  9. package/__tests__/throughInitialDefine.test.js +16 -16
  10. package/__tests__/throughInitialExec.test.js +16 -16
  11. package/__tests__/throughUpdatedDefine.test.js +14 -14
  12. package/__tests__/throughUpdatedExec.test.js +17 -16
  13. package/__tests__/update.test.js +54 -54
  14. package/children.js +5 -6
  15. package/create.js +18 -20
  16. package/dist/cjs/children.js +4 -2
  17. package/dist/cjs/create.js +16 -16
  18. package/dist/cjs/extend.js +88 -0
  19. package/dist/cjs/iterate.js +76 -20
  20. package/dist/cjs/mixins/content.js +73 -0
  21. package/dist/cjs/mixins/html.js +1 -1
  22. package/dist/cjs/mixins/registry.js +2 -2
  23. package/dist/cjs/mixins/scope.js +1 -1
  24. package/dist/cjs/mixins/state.js +2 -2
  25. package/dist/cjs/mixins/text.js +4 -4
  26. package/dist/cjs/node.js +15 -15
  27. package/dist/cjs/set.js +22 -27
  28. package/dist/cjs/update.js +24 -33
  29. package/dist/cjs/utils/applyParam.js +4 -4
  30. package/dist/cjs/utils/extendUtils.js +132 -0
  31. package/dist/cjs/utils/propEvents.js +39 -0
  32. package/dist/esm/children.js +5 -3
  33. package/dist/esm/create.js +16 -16
  34. package/dist/esm/extend.js +74 -0
  35. package/dist/esm/iterate.js +77 -22
  36. package/dist/esm/mixins/content.js +53 -0
  37. package/dist/esm/mixins/html.js +1 -1
  38. package/dist/esm/mixins/registry.js +1 -1
  39. package/dist/esm/mixins/scope.js +1 -1
  40. package/dist/esm/mixins/state.js +3 -3
  41. package/dist/esm/mixins/text.js +4 -4
  42. package/dist/esm/node.js +15 -15
  43. package/dist/esm/set.js +23 -28
  44. package/dist/esm/update.js +24 -33
  45. package/dist/esm/utils/applyParam.js +5 -5
  46. package/dist/esm/utils/extendUtils.js +119 -0
  47. package/dist/esm/utils/propEvents.js +19 -0
  48. package/extend.js +98 -0
  49. package/iterate.js +80 -25
  50. package/mixins/content.js +65 -0
  51. package/mixins/html.js +1 -1
  52. package/mixins/registry.js +1 -1
  53. package/mixins/scope.js +1 -1
  54. package/mixins/state.js +3 -3
  55. package/mixins/text.js +4 -4
  56. package/node.js +15 -15
  57. package/package.json +8 -8
  58. package/set.js +23 -28
  59. package/update.js +25 -34
  60. package/utils/applyParam.js +5 -5
  61. package/utils/extendUtils.js +149 -0
  62. package/utils/propEvents.js +19 -0
  63. package/dist/cjs/__tests__/checkIfOnUpdate.test.js +0 -73
  64. package/dist/cjs/__tests__/children.test.js +0 -177
  65. package/dist/cjs/__tests__/define.test.js +0 -75
  66. package/dist/cjs/__tests__/inheritStateUpdates.test.js +0 -62
  67. package/dist/cjs/__tests__/renderElement.test.js +0 -138
  68. package/dist/cjs/__tests__/resetElement.test.js +0 -35
  69. package/dist/cjs/__tests__/set.test.js +0 -256
  70. package/dist/cjs/__tests__/throughExecProps.test.js +0 -62
  71. package/dist/cjs/__tests__/throughInitialDefine.test.js +0 -79
  72. package/dist/cjs/__tests__/throughInitialExec.test.js +0 -73
  73. package/dist/cjs/__tests__/throughUpdatedDefine.test.js +0 -69
  74. package/dist/cjs/__tests__/throughUpdatedExec.test.js +0 -84
  75. package/dist/cjs/__tests__/tree.test.js +0 -11
  76. package/dist/cjs/__tests__/update.test.js +0 -222
  77. package/dist/esm/__tests__/checkIfOnUpdate.test.js +0 -73
  78. package/dist/esm/__tests__/children.test.js +0 -177
  79. package/dist/esm/__tests__/define.test.js +0 -53
  80. package/dist/esm/__tests__/inheritStateUpdates.test.js +0 -62
  81. package/dist/esm/__tests__/renderElement.test.js +0 -116
  82. package/dist/esm/__tests__/resetElement.test.js +0 -35
  83. package/dist/esm/__tests__/set.test.js +0 -256
  84. package/dist/esm/__tests__/throughExecProps.test.js +0 -62
  85. package/dist/esm/__tests__/throughInitialDefine.test.js +0 -79
  86. package/dist/esm/__tests__/throughInitialExec.test.js +0 -73
  87. package/dist/esm/__tests__/throughUpdatedDefine.test.js +0 -69
  88. package/dist/esm/__tests__/throughUpdatedExec.test.js +0 -84
  89. package/dist/esm/__tests__/tree.test.js +0 -11
  90. package/dist/esm/__tests__/update.test.js +0 -222
package/update.js CHANGED
@@ -45,7 +45,7 @@ const UPDATE_DEFAULT_OPTIONS = {
45
45
  exclude: METHODS_EXL
46
46
  }
47
47
 
48
- export const update = async function (params = {}, opts) {
48
+ export const update = function (params = {}, opts) {
49
49
  const calleeElementCache = opts?.calleeElement
50
50
  const options = deepClone(
51
51
  isObject(opts)
@@ -66,7 +66,7 @@ export const update = async function (params = {}, opts) {
66
66
  if (snapshotHasUpdated) return
67
67
 
68
68
  if (!options.preventListeners) {
69
- await triggerEventOnUpdate('startUpdate', params, element, options)
69
+ triggerEventOnUpdate('startUpdate', params, element, options)
70
70
  }
71
71
 
72
72
  const { parent, node, key } = element
@@ -87,10 +87,10 @@ export const update = async function (params = {}, opts) {
87
87
  // apply new updates
88
88
  params = propertizeUpdate.call(element, params)
89
89
 
90
- const inheritState = await inheritStateUpdates(element, options)
90
+ const inheritState = inheritStateUpdates(element, options)
91
91
  if (inheritState === false) return
92
92
 
93
- const ifFails = await checkIfOnUpdate(element, parent, options)
93
+ const ifFails = checkIfOnUpdate(element, parent, options)
94
94
  if (ifFails) return
95
95
 
96
96
  if (ref.__if && !options.preventPropsUpdate) {
@@ -103,7 +103,7 @@ export const update = async function (params = {}, opts) {
103
103
 
104
104
  if (!options.preventBeforeUpdateListener && !options.preventListeners) {
105
105
  const simulate = { ...params, ...element }
106
- const beforeUpdateReturns = await triggerEventOnUpdate(
106
+ const beforeUpdateReturns = triggerEventOnUpdate(
107
107
  'beforeUpdate',
108
108
  params,
109
109
  simulate,
@@ -121,7 +121,7 @@ export const update = async function (params = {}, opts) {
121
121
  throughUpdatedDefine(element)
122
122
 
123
123
  if (!options.isForced && !options.preventListeners) {
124
- await triggerEventOn('beforeClassAssign', element, options)
124
+ triggerEventOn('beforeClassAssign', element, options)
125
125
  }
126
126
 
127
127
  if (!ref.__if) return false
@@ -177,7 +177,7 @@ export const update = async function (params = {}, opts) {
177
177
 
178
178
  if (preventStateUpdate === 'once') options.preventStateUpdate = false
179
179
 
180
- const isElement = await applyParam(param, element, options)
180
+ const isElement = applyParam(param, element, options)
181
181
  if (isElement) {
182
182
  const { hasDefine, hasContextDefine } = isElement
183
183
  const canUpdate =
@@ -190,43 +190,43 @@ export const update = async function (params = {}, opts) {
190
190
  options.onEachUpdate(param, element, element.state, element.context)
191
191
  }
192
192
 
193
- const childUpdateCall = async () =>
194
- await update.call(prop, params[prop], {
193
+ const childUpdateCall = () =>
194
+ update.call(prop, params[prop], {
195
195
  ...options,
196
196
  currentSnapshot: snapshotOnCallee,
197
197
  calleeElement
198
198
  })
199
199
 
200
200
  if (lazyLoad) {
201
- window.requestAnimationFrame(async () => {
201
+ window.requestAnimationFrame(() => {
202
202
  // eslint-disable-line
203
- await childUpdateCall()
203
+ childUpdateCall()
204
204
  // handle lazy load
205
205
  if (!options.preventUpdateListener) {
206
- await triggerEventOn('lazyLoad', element, options)
206
+ triggerEventOn('lazyLoad', element, options)
207
207
  }
208
208
  })
209
- } else await childUpdateCall()
209
+ } else childUpdateCall()
210
210
  }
211
211
  }
212
212
 
213
213
  if (!preventContentUpdate) {
214
214
  const children = params.children || element.children
215
215
  const content = children
216
- ? await setChildren(children, element, opts)
216
+ ? setChildren(children, element, opts)
217
217
  : element.children || params.content
218
218
 
219
219
  if (content) {
220
- await setContent(content, element, options)
220
+ setContent(content, element, options)
221
221
  }
222
222
  }
223
223
 
224
224
  if (!preventUpdateListener) {
225
- await triggerEventOn('update', element, options)
225
+ triggerEventOn('update', element, options)
226
226
  }
227
227
  }
228
228
 
229
- const checkIfOnUpdate = async (element, parent, options) => {
229
+ const checkIfOnUpdate = (element, parent, options) => {
230
230
  if ((!isFunction(element.if) && !isFunction(element.props?.if)) || !parent) {
231
231
  return
232
232
  }
@@ -285,7 +285,7 @@ const checkIfOnUpdate = async (element, parent, options) => {
285
285
 
286
286
  delete element.__ref
287
287
  delete element.parent
288
- const createdElement = await create(
288
+ const createdElement = create(
289
289
  element,
290
290
  parent,
291
291
  element.key,
@@ -298,11 +298,7 @@ const checkIfOnUpdate = async (element, parent, options) => {
298
298
  element.on &&
299
299
  isFunction(element.on.update)
300
300
  ) {
301
- await applyEvent(
302
- element.on.update,
303
- createdElement,
304
- createdElement.state
305
- )
301
+ applyEvent(element.on.update, createdElement, createdElement.state)
306
302
  }
307
303
  return createdElement
308
304
  }
@@ -326,7 +322,7 @@ const checkIfOnUpdate = async (element, parent, options) => {
326
322
  * @param {boolean} [options.preventStateUpdateListener] - If true, prevent the 'stateUpdate' event listener.
327
323
  * @returns {boolean} - If returns false, it breaks the update function
328
324
  */
329
- const inheritStateUpdates = async (element, options) => {
325
+ const inheritStateUpdates = (element, options) => {
330
326
  const { __ref: ref } = element
331
327
  const stateKey = ref.__state
332
328
  const { parent, state } = element
@@ -361,7 +357,7 @@ const inheritStateUpdates = async (element, options) => {
361
357
 
362
358
  // Trigger on.beforeStateUpdate event
363
359
  if (!options.preventBeforeStateUpdateListener && !options.preventListeners) {
364
- const initStateReturns = await triggerEventOnUpdate(
360
+ const initStateReturns = triggerEventOnUpdate(
365
361
  'beforeStateUpdate',
366
362
  keyInParentState,
367
363
  element,
@@ -371,22 +367,17 @@ const inheritStateUpdates = async (element, options) => {
371
367
  }
372
368
 
373
369
  // Recreate the state again
374
- const newState = await createStateUpdate(element, parent, options)
370
+ const newState = createStateUpdate(element, parent, options)
375
371
 
376
372
  // Trigger on.stateUpdate event
377
373
  if (!options.preventStateUpdateListener && !options.preventListeners) {
378
- await triggerEventOnUpdate(
379
- 'stateUpdate',
380
- newState.parse(),
381
- element,
382
- options
383
- )
374
+ triggerEventOnUpdate('stateUpdate', newState.parse(), element, options)
384
375
  }
385
376
  }
386
377
 
387
- const createStateUpdate = async (element, parent, options) => {
378
+ const createStateUpdate = (element, parent, options) => {
388
379
  const __stateChildren = element.state.__children
389
- const newState = await createState(element, parent)
380
+ const newState = createState(element, parent)
390
381
  element.state = newState
391
382
  for (const child in __stateChildren) {
392
383
  // check this for inherited states
@@ -1,11 +1,11 @@
1
1
  'use strict'
2
2
 
3
- import { isFunction } from '@domql/utils'
3
+ import { exec, isFunction } from '@domql/utils'
4
4
  import { REGISTRY } from '../mixins/index.js'
5
5
 
6
- export const applyParam = async (param, element, options) => {
6
+ export const applyParam = (param, element, options) => {
7
7
  const { node, context, __ref: ref } = element
8
- const prop = element[param]
8
+ const prop = exec(element[param], element)
9
9
 
10
10
  const { onlyUpdate } = options
11
11
 
@@ -25,9 +25,9 @@ export const applyParam = async (param, element, options) => {
25
25
 
26
26
  if (isGlobalTransformer && !hasContextDefine && hasOnlyUpdate) {
27
27
  if (isFunction(isGlobalTransformer)) {
28
- await isGlobalTransformer(prop, element, node, options)
29
- return
28
+ isGlobalTransformer(prop, element, node, options)
30
29
  }
30
+ return
31
31
  }
32
32
 
33
33
  return { hasDefine, hasContextDefine }
@@ -0,0 +1,149 @@
1
+ 'use strict'
2
+
3
+ import {
4
+ isArray,
5
+ isFunction,
6
+ isObject,
7
+ isString,
8
+ deepClone,
9
+ isNotProduction
10
+ } from '@domql/utils'
11
+
12
+ export const generateHash = () => Math.random().toString(36).substring(2)
13
+
14
+ // hashing
15
+ export const extendStackRegistry = {}
16
+ export const extendCachedRegistry = {}
17
+
18
+ export const getHashedExtend = extend => {
19
+ return extendStackRegistry[extend.__hash]
20
+ }
21
+
22
+ export const setHashedExtend = (extend, stack) => {
23
+ const hash = generateHash()
24
+ if (!isString(extend)) {
25
+ extend.__hash = hash
26
+ }
27
+ extendStackRegistry[hash] = stack
28
+ return stack
29
+ }
30
+
31
+ export const getExtendStackRegistry = (extend, stack) => {
32
+ if (extend.__hash) {
33
+ return stack.concat(getHashedExtend(extend))
34
+ }
35
+ return setHashedExtend(extend, stack) // stack .concat(hashedExtend)
36
+ }
37
+
38
+ // stacking
39
+ export const extractArrayExtend = (extend, stack, context) => {
40
+ extend.forEach(each => flattenExtend(each, stack, context))
41
+ return stack
42
+ }
43
+
44
+ export const deepExtend = (extend, stack, context) => {
45
+ const extendOflattenExtend = extend.extend
46
+ if (extendOflattenExtend) {
47
+ flattenExtend(extendOflattenExtend, stack, context)
48
+ }
49
+ return stack
50
+ }
51
+
52
+ export const flattenExtend = (extend, stack, context) => {
53
+ if (!extend) return stack
54
+ if (isArray(extend)) return extractArrayExtend(extend, stack, context)
55
+ if (isString(extend)) extend = fallbackStringExtend(extend, context)
56
+ stack.push(extend)
57
+ if (extend.extend) deepExtend(extend, stack, context)
58
+ return stack
59
+ }
60
+
61
+ export const deepMergeExtend = (element, extend) => {
62
+ for (const e in extend) {
63
+ if (['parent', 'node', '__element'].indexOf(e) > -1) continue
64
+ const elementProp = element[e]
65
+ const extendProp = extend[e]
66
+ if (elementProp === undefined) {
67
+ element[e] = extendProp
68
+ } else if (isObject(elementProp) && isObject(extendProp)) {
69
+ deepMergeExtend(elementProp, extendProp)
70
+ } else if (isArray(elementProp) && isArray(extendProp)) {
71
+ element[e] = elementProp.concat(extendProp)
72
+ } else if (isArray(elementProp) && isObject(extendProp)) {
73
+ const obj = deepMergeExtend({}, elementProp)
74
+ element[e] = deepMergeExtend(obj, extendProp)
75
+ } else if (elementProp === undefined && isFunction(extendProp)) {
76
+ element[e] = extendProp
77
+ }
78
+ }
79
+ return element
80
+ }
81
+
82
+ export const cloneAndMergeArrayExtend = stack => {
83
+ return stack.reduce((a, c) => {
84
+ return deepMergeExtend(a, deepClone(c))
85
+ }, {})
86
+ }
87
+
88
+ export const fallbackStringExtend = (
89
+ extend,
90
+ context,
91
+ options = {},
92
+ variant
93
+ ) => {
94
+ const COMPONENTS = (context && context.components) || options.components
95
+ const PAGES = (context && context.pages) || options.pages
96
+ if (isString(extend)) {
97
+ const componentExists =
98
+ COMPONENTS &&
99
+ (COMPONENTS[extend + '.' + variant] ||
100
+ COMPONENTS[extend] ||
101
+ COMPONENTS['smbls.' + extend])
102
+ const pageExists = PAGES && extend.startsWith('/') && PAGES[extend]
103
+ if (componentExists) return componentExists
104
+ else if (pageExists) return pageExists
105
+ else {
106
+ if (options.verbose && isNotProduction()) {
107
+ console.warn('Extend is string but component was not found:', extend)
108
+ }
109
+ return {}
110
+ }
111
+ }
112
+ return extend
113
+ }
114
+
115
+ // joint stacks
116
+ export const jointStacks = (extendStack, childExtendStack) => {
117
+ return []
118
+ .concat(extendStack.slice(0, 1))
119
+ .concat(childExtendStack.slice(0, 1))
120
+ .concat(extendStack.slice(1))
121
+ .concat(childExtendStack.slice(1))
122
+ }
123
+
124
+ // init
125
+ export const getExtendStack = (extend, context) => {
126
+ if (!extend) return []
127
+ if (extend.__hash) return getHashedExtend(extend) || []
128
+ const stack = flattenExtend(extend, [], context)
129
+ return getExtendStackRegistry(extend, stack)
130
+ }
131
+
132
+ export const getExtendMerged = extend => {
133
+ const stack = getExtendStack(extend)
134
+ return cloneAndMergeArrayExtend(stack)
135
+ }
136
+
137
+ // export const replaceStringsWithComponents = (stack, context, options) => {
138
+ // const COMPONENTS = (context && context.components) || options.components
139
+ // return stack.map(v => {
140
+ // if (isString(v)) {
141
+ // const component = COMPONENTS[v]
142
+ // return component
143
+ // }
144
+ // if (isString(v.extend)) {
145
+ // v.extend = getExtendMerged(COMPONENTS[v.extend])
146
+ // }
147
+ // return v
148
+ // })
149
+ // }
@@ -0,0 +1,19 @@
1
+ 'use strict'
2
+
3
+ import { isFunction, lowercaseFirstLetter } from '@domql/utils'
4
+
5
+ export const propagateEventsFromProps = (element) => {
6
+ const { props, on } = element
7
+ const eventKeysFromProps = Object.keys(props).filter(key => key.startsWith('on'))
8
+ eventKeysFromProps.forEach(v => {
9
+ const eventName = lowercaseFirstLetter(v.split('on')[1])
10
+ const origEvent = on[eventName]
11
+ const funcFromProps = props[v]
12
+ if (isFunction(origEvent)) {
13
+ on[eventName] = (...args) => {
14
+ const originalEventRetunrs = origEvent(...args)
15
+ if (originalEventRetunrs !== false) return funcFromProps(...args)
16
+ }
17
+ } else on[eventName] = funcFromProps
18
+ })
19
+ }
@@ -1,73 +0,0 @@
1
- var import_update = require("../update");
2
- describe("checkIfOnUpdate via update()", () => {
3
- let element, parent, options;
4
- beforeEach(() => {
5
- parent = {
6
- node: document.createElement("div"),
7
- props: {},
8
- state: {}
9
- };
10
- element = {
11
- __ref: {
12
- __if: void 0,
13
- __state: null,
14
- __hasRootState: false,
15
- __execProps: {},
16
- contentElementKey: "content"
17
- },
18
- parent,
19
- props: {},
20
- state: {
21
- update: (el, st) => {
22
- return st;
23
- }
24
- },
25
- context: {
26
- defaultExtends: {}
27
- },
28
- node: document.createElement("div"),
29
- if: () => true,
30
- previousElement: () => {
31
- return {};
32
- },
33
- nextElement: () => {
34
- return {};
35
- },
36
- removeContent: () => {
37
- return true;
38
- }
39
- };
40
- options = {};
41
- });
42
- it("uses props.if when element.if missing", async () => {
43
- delete element.if;
44
- element.props.if = () => false;
45
- await import_update.update.call(element, {}, options);
46
- expect(element.node).toEqual(document.createElement("div"));
47
- });
48
- it("retains state when __hasRootState=true", async () => {
49
- element.__ref.__hasRootState = true;
50
- element.state.critical = true;
51
- element.__ref.__if = false;
52
- await import_update.update.call(element, {}, options);
53
- expect(element.state.critical).toBe(true);
54
- expect(element.state.preserved).toBeUndefined();
55
- });
56
- it("processes nested content with parseDeep", async () => {
57
- element.content = {
58
- parseDeep: () => ({ parsed: true }),
59
- existing: "data"
60
- };
61
- await import_update.update.call(element, {}, options);
62
- expect(element.content.parsed).toBe(true);
63
- expect(element.content.existing).toBeUndefined();
64
- });
65
- it("reattaches after previous sibling", async () => {
66
- const prevNode = document.createElement("span");
67
- parent.node.appendChild(prevNode);
68
- await import_update.update.call(element, {}, options);
69
- const newElement = parent.node.children[0];
70
- expect(newElement).toEqual(document.createElement("span"));
71
- expect(newElement.previousSibling).toBe(null);
72
- });
73
- });
@@ -1,177 +0,0 @@
1
- var import_globals = require("@jest/globals");
2
- var import_children = require("../children");
3
- describe("children", () => {
4
- let element, node;
5
- beforeEach(() => {
6
- element = {
7
- __ref: {},
8
- state: {},
9
- props: {},
10
- call: import_globals.jest.fn(),
11
- removeContent: import_globals.jest.fn(),
12
- content: null
13
- };
14
- node = {};
15
- });
16
- it("handles null/undefined params", async () => {
17
- const result = await (0, import_children.setChildren)(null, element, node);
18
- expect(result).toBeUndefined();
19
- });
20
- it("handles direct string children", async () => {
21
- const result = await (0, import_children.setChildren)("Hello World", element, node);
22
- expect(result).toEqual({ tag: "fragment", 0: { text: "Hello World" } });
23
- });
24
- it("handles numeric children", async () => {
25
- const result = await (0, import_children.setChildren)(42, element, node);
26
- expect(result).toEqual({ tag: "fragment", 0: { text: 42 } });
27
- });
28
- it("handles array of primitive values with childrenAs prop", async () => {
29
- const result = await (0, import_children.setChildren)(["one", "two"], element, node);
30
- expect(result).toEqual({
31
- tag: "fragment",
32
- 0: { text: "one" },
33
- 1: { text: "two" }
34
- });
35
- });
36
- it("handles array of primitive values with childrenAs state", async () => {
37
- element.props.childrenAs = "state";
38
- const result = await (0, import_children.setChildren)(["one", "two"], element, node);
39
- expect(result).toEqual({
40
- tag: "fragment",
41
- 0: { state: { value: "one" } },
42
- 1: { state: { value: "two" } }
43
- });
44
- });
45
- it("caches children and detects changes", async () => {
46
- const children1 = [{ id: 1 }, { id: 2 }];
47
- const children2 = [{ id: 1 }, { id: 2 }];
48
- const children3 = [{ id: 1 }, { id: 3 }];
49
- await (0, import_children.setChildren)(children1, element, node);
50
- expect(element.__ref.__childrenCache).toEqual(children1);
51
- expect(element.__ref.__noChildrenDifference).toBeUndefined();
52
- await (0, import_children.setChildren)(children2, element, node);
53
- expect(element.__ref.__noChildrenDifference).toBe(true);
54
- await (0, import_children.setChildren)(children3, element, node);
55
- expect(element.__ref.__noChildrenDifference).toBeUndefined();
56
- expect(element.__ref.__childrenCache).toEqual(children3);
57
- });
58
- it("handles mixed React and normal components", async () => {
59
- const mixedChildren = [
60
- { type: "div", text: "Normal" },
61
- { $$typeof: Symbol("react") },
62
- { type: "span", text: "Another" }
63
- ];
64
- await (0, import_children.setChildren)(mixedChildren, element, node);
65
- expect(element.call).toHaveBeenCalledWith(
66
- "renderReact",
67
- [mixedChildren[1]],
68
- element
69
- );
70
- });
71
- it("handles state-based children", async () => {
72
- element.state = {
73
- items: ["a", "b"],
74
- parse: () => ["parsed a", "parsed b"]
75
- };
76
- const result = await (0, import_children.setChildren)("state", element, node);
77
- expect(result).toEqual({
78
- tag: "fragment",
79
- 0: { text: "parsed a" },
80
- 1: { text: "parsed b" }
81
- });
82
- });
83
- it("handles async function parameters", async () => {
84
- const asyncParam = async () => ["async1", "async2"];
85
- const result = await (0, import_children.setChildren)(asyncParam, element, node);
86
- expect(result).toEqual({
87
- tag: "fragment",
88
- 0: { text: "async1" },
89
- 1: { text: "async2" }
90
- });
91
- });
92
- it("handles nested object structures", async () => {
93
- const nestedChildren = {
94
- header: { text: "Title" },
95
- content: {
96
- nested: { text: "Content" }
97
- }
98
- };
99
- const result = await (0, import_children.setChildren)(nestedChildren, element, node);
100
- expect(result).toEqual({
101
- tag: "fragment",
102
- 0: { text: "Title" },
103
- 1: { nested: { text: "Content" } }
104
- });
105
- });
106
- it("handles empty arrays and objects", async () => {
107
- let result = await (0, import_children.setChildren)([], element, node);
108
- expect(result).toEqual({
109
- tag: "fragment"
110
- });
111
- result = await (0, import_children.setChildren)({}, element, node);
112
- expect(result).toEqual({
113
- tag: "fragment"
114
- });
115
- });
116
- it("handles falsy values in arrays", async () => {
117
- const result = await (0, import_children.setChildren)(
118
- [null, void 0, false, 0, ""],
119
- element,
120
- node
121
- );
122
- expect(result).toEqual({
123
- tag: "fragment",
124
- 3: { text: 0 },
125
- 4: { text: "" }
126
- });
127
- });
128
- it("handles React components with falsy values in array", async () => {
129
- const mixedChildren = [
130
- null,
131
- { $$typeof: Symbol("react") },
132
- void 0,
133
- { $$typeof: Symbol("react") },
134
- false
135
- ];
136
- await (0, import_children.setChildren)(mixedChildren, element, node);
137
- expect(element.call).toHaveBeenCalledWith(
138
- "renderReact",
139
- [mixedChildren[1], mixedChildren[3]],
140
- element
141
- );
142
- });
143
- it("handles nested state parsing", async () => {
144
- element.state = {
145
- nested: {
146
- items: ["c", "d"]
147
- }
148
- };
149
- element.state.nested.__proto__.parse = () => ["parsed c", "parsed d"];
150
- const result = await (0, import_children.setChildren)("nested", element, node);
151
- expect(result).toEqual({
152
- tag: "fragment",
153
- 0: { state: ["c", "d"] }
154
- });
155
- });
156
- it("handles mixed state and regular objects", async () => {
157
- element.state = {
158
- header: { parse: () => "Header" },
159
- footer: { parse: () => "Footer" }
160
- };
161
- const result = await (0, import_children.setChildren)(
162
- {
163
- header: "header",
164
- content: { text: "Content" },
165
- footer: "footer"
166
- },
167
- element,
168
- node
169
- );
170
- expect(result).toEqual({
171
- tag: "fragment",
172
- 0: { text: "header" },
173
- 1: { text: "Content" },
174
- 2: { text: "footer" }
175
- });
176
- });
177
- });
@@ -1,75 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __copyProps = (to, from, except, desc) => {
8
- if (from && typeof from === "object" || typeof from === "function") {
9
- for (let key of __getOwnPropNames(from))
10
- if (!__hasOwnProp.call(to, key) && key !== except)
11
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
12
- }
13
- return to;
14
- };
15
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
- // If the importer is in node compatibility mode or this is not an ESM
17
- // file that has been converted to a CommonJS file using a Babel-
18
- // compatible transform (i.e. "__esModule" has not been set), then set
19
- // "default" to the CommonJS "module.exports" for node compatibility.
20
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
- mod
22
- ));
23
- var import_define = __toESM(require("../define"), 1);
24
- var import_mixins = require("../mixins");
25
- describe("default function (registry updater)", () => {
26
- let originalRegistry;
27
- beforeEach(() => {
28
- originalRegistry = { ...import_mixins.REGISTRY };
29
- });
30
- afterEach(() => {
31
- Object.keys(import_mixins.REGISTRY).forEach((key) => {
32
- import_mixins.REGISTRY[key] = originalRegistry[key];
33
- });
34
- });
35
- it("should add new params to REGISTRY when overwrite is true", () => {
36
- const params = { newKey: "newValue", anotherKey: "anotherValue" };
37
- const options = { overwrite: true };
38
- (0, import_define.default)(params, options);
39
- expect(import_mixins.REGISTRY.newKey).toBe("newValue");
40
- expect(import_mixins.REGISTRY.anotherKey).toBe("anotherValue");
41
- });
42
- it("should not modify REGISTRY when trying to overwrite existing keys without overwrite option", () => {
43
- const params = { attr: "newValue", text: "newText" };
44
- try {
45
- (0, import_define.default)(params);
46
- } catch (e) {
47
- }
48
- expect(import_mixins.REGISTRY.attr).toBe(originalRegistry.attr);
49
- expect(import_mixins.REGISTRY.text).toBe(originalRegistry.text);
50
- });
51
- it("should overwrite existing keys when overwrite is true", () => {
52
- const params = { attr: "newValue", text: "newText" };
53
- const options = { overwrite: true };
54
- (0, import_define.default)(params, options);
55
- expect(import_mixins.REGISTRY.attr).toBe("newValue");
56
- expect(import_mixins.REGISTRY.text).toBe("newText");
57
- });
58
- it("should handle empty params object without errors", () => {
59
- const params = {};
60
- const options = { overwrite: true };
61
- expect(() => (0, import_define.default)(params, options)).not.toThrow();
62
- expect(import_mixins.REGISTRY).toEqual(originalRegistry);
63
- });
64
- it("should handle empty options object without errors", () => {
65
- const params = { newKey: "newValue" };
66
- expect(() => (0, import_define.default)(params, {})).not.toThrow();
67
- expect(import_mixins.REGISTRY.newKey).toBe("newValue");
68
- });
69
- it("should not add new keys when params is empty", () => {
70
- const params = {};
71
- const options = { overwrite: true };
72
- (0, import_define.default)(params, options);
73
- expect(import_mixins.REGISTRY).toEqual(originalRegistry);
74
- });
75
- });