@domql/element 3.0.7 → 3.1.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.
- package/__tests__/inheritStateUpdates.test.js +1 -1
- package/__tests__/throughExecProps.test.js +12 -12
- package/__tests__/throughInitialDefine.test.js +16 -16
- package/__tests__/throughInitialExec.test.js +16 -16
- package/__tests__/throughUpdatedDefine.test.js +14 -14
- package/__tests__/update.test.js +11 -8
- package/create.js +1 -10
- package/dist/cjs/__tests__/checkIfOnUpdate.test.js +73 -0
- package/dist/cjs/__tests__/children.test.js +177 -0
- package/dist/cjs/__tests__/define.test.js +75 -0
- package/dist/cjs/__tests__/inheritStateUpdates.test.js +62 -0
- package/dist/cjs/__tests__/renderElement.test.js +138 -0
- package/dist/cjs/__tests__/resetElement.test.js +35 -0
- package/dist/cjs/__tests__/set.test.js +256 -0
- package/dist/cjs/__tests__/throughExecProps.test.js +62 -0
- package/dist/cjs/__tests__/throughInitialDefine.test.js +79 -0
- package/dist/cjs/__tests__/throughInitialExec.test.js +73 -0
- package/dist/cjs/__tests__/throughUpdatedDefine.test.js +69 -0
- package/dist/cjs/__tests__/throughUpdatedExec.test.js +84 -0
- package/dist/cjs/__tests__/tree.test.js +11 -0
- package/dist/cjs/__tests__/update.test.js +222 -0
- package/dist/cjs/create.js +2 -11
- package/dist/cjs/iterate.js +11 -11
- package/dist/cjs/update.js +3 -2
- package/dist/esm/__tests__/checkIfOnUpdate.test.js +73 -0
- package/dist/esm/__tests__/children.test.js +177 -0
- package/dist/esm/__tests__/define.test.js +53 -0
- package/dist/esm/__tests__/inheritStateUpdates.test.js +62 -0
- package/dist/esm/__tests__/renderElement.test.js +116 -0
- package/dist/esm/__tests__/resetElement.test.js +35 -0
- package/dist/esm/__tests__/set.test.js +256 -0
- package/dist/esm/__tests__/throughExecProps.test.js +62 -0
- package/dist/esm/__tests__/throughInitialDefine.test.js +79 -0
- package/dist/esm/__tests__/throughInitialExec.test.js +73 -0
- package/dist/esm/__tests__/throughUpdatedDefine.test.js +69 -0
- package/dist/esm/__tests__/throughUpdatedExec.test.js +84 -0
- package/dist/esm/__tests__/tree.test.js +11 -0
- package/dist/esm/__tests__/update.test.js +222 -0
- package/dist/esm/create.js +2 -11
- package/dist/esm/iterate.js +13 -12
- package/dist/esm/update.js +5 -4
- package/iterate.js +13 -12
- package/package.json +11 -12
- package/update.js +6 -4
- package/dist/cjs/utils/onlyResolveExtends.js +0 -85
- package/dist/esm/utils/onlyResolveExtends.js +0 -72
- package/utils/onlyResolveExtends.js +0 -128
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { update } from "../update";
|
|
2
|
+
describe("update() with inheritStateUpdates", () => {
|
|
3
|
+
let element, options;
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
element = {
|
|
6
|
+
__ref: {
|
|
7
|
+
__state: null,
|
|
8
|
+
__hasRootState: false,
|
|
9
|
+
__execProps: {},
|
|
10
|
+
__props: []
|
|
11
|
+
},
|
|
12
|
+
state: {
|
|
13
|
+
calculated: 42,
|
|
14
|
+
set: () => {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
parent: {
|
|
19
|
+
state: { baseState: true },
|
|
20
|
+
props: {}
|
|
21
|
+
},
|
|
22
|
+
props: {},
|
|
23
|
+
key: "testKey"
|
|
24
|
+
};
|
|
25
|
+
options = {};
|
|
26
|
+
});
|
|
27
|
+
it("processes full update flow when state is inherited", async () => {
|
|
28
|
+
await update.call(element, { props: { newProp: true } }, options);
|
|
29
|
+
expect(element.props.newProp).toBe(true);
|
|
30
|
+
expect(element.state.baseState).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
it("maintains state when preventInheritedStateUpdate=true", async () => {
|
|
33
|
+
options.preventInheritedStateUpdate = true;
|
|
34
|
+
element.state = { existing: "state" };
|
|
35
|
+
await update.call(element, {}, options);
|
|
36
|
+
expect(element.state).toEqual({ baseState: true });
|
|
37
|
+
});
|
|
38
|
+
it("overwrites state with function result", async () => {
|
|
39
|
+
element.__ref.__state = () => ({ calculated: 42 });
|
|
40
|
+
options.execStateFunction = true;
|
|
41
|
+
options.stateFunctionOverwrite = true;
|
|
42
|
+
await update.call(element, {}, options);
|
|
43
|
+
expect(element.state.calculated).toEqual(42);
|
|
44
|
+
});
|
|
45
|
+
it("preserves state when beforeStateUpdate rejects", async () => {
|
|
46
|
+
element.onBeforeStateUpdate = () => false;
|
|
47
|
+
await update.call(element, { props: { shouldChange: true } }, options);
|
|
48
|
+
expect(element.state).toEqual({ baseState: true });
|
|
49
|
+
expect(element.props.shouldChange).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
it("reflects parent state updates", async () => {
|
|
52
|
+
element.parent.state = { updatedParentState: true };
|
|
53
|
+
await update.call(element, {}, options);
|
|
54
|
+
expect(element.state.updatedParentState).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
it("preserves local state when __hasRootState=true", async () => {
|
|
57
|
+
element.__ref.__hasRootState = true;
|
|
58
|
+
element.state = { local: "data" };
|
|
59
|
+
await update.call(element, {}, options);
|
|
60
|
+
expect(element.state.local).toBe("data");
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { jest } from "@jest/globals";
|
|
2
|
+
describe("create()", () => {
|
|
3
|
+
let props;
|
|
4
|
+
let parent;
|
|
5
|
+
const OLD_ENV = process.env;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
props = {
|
|
8
|
+
__ref: { __if: false, path: [], __skipCreate: true },
|
|
9
|
+
key: "testKey",
|
|
10
|
+
context: {
|
|
11
|
+
defaultExtends: {},
|
|
12
|
+
define: ["test"]
|
|
13
|
+
},
|
|
14
|
+
scope: "props",
|
|
15
|
+
define: ["test"]
|
|
16
|
+
};
|
|
17
|
+
parent = {
|
|
18
|
+
testKey: "parentTestKey",
|
|
19
|
+
key: "parentKey"
|
|
20
|
+
};
|
|
21
|
+
jest.resetModules();
|
|
22
|
+
process.env = { ...OLD_ENV };
|
|
23
|
+
});
|
|
24
|
+
afterAll(() => {
|
|
25
|
+
process.env = OLD_ENV;
|
|
26
|
+
});
|
|
27
|
+
test("should execute onlyResolveExtends when __skipCreate is true", async () => {
|
|
28
|
+
props.__ref.__skipCreate = true;
|
|
29
|
+
props.scope = void 0;
|
|
30
|
+
const { create } = await import("../create");
|
|
31
|
+
await create(props, parent, "passedKey", {
|
|
32
|
+
onlyResolveExtends: true,
|
|
33
|
+
define: ["test"]
|
|
34
|
+
});
|
|
35
|
+
expect(parent.__ref).toBeUndefined();
|
|
36
|
+
expect(parent.passedKey).toBe(props.__ref.parent.passedKey);
|
|
37
|
+
});
|
|
38
|
+
test("should execute onlyResolveExtends when __ref.__if is true", async () => {
|
|
39
|
+
props.__ref.__if = true;
|
|
40
|
+
props.scope = "state";
|
|
41
|
+
const { create } = await import("../create");
|
|
42
|
+
await create(props, parent, "passedKey", {
|
|
43
|
+
onlyResolveExtends: true,
|
|
44
|
+
define: ["test"]
|
|
45
|
+
});
|
|
46
|
+
expect(parent.__ref).toBeUndefined();
|
|
47
|
+
expect(parent.passedKey).toBe(props.__ref.parent.passedKey);
|
|
48
|
+
});
|
|
49
|
+
test("should execute onlyResolveExtends when scope is not state", async () => {
|
|
50
|
+
process.env.NODE_ENV = "prod";
|
|
51
|
+
props.__ref = void 0;
|
|
52
|
+
const { create } = await import("../create");
|
|
53
|
+
await create(props, parent, "passedKey", { onlyResolveExtends: true });
|
|
54
|
+
expect(parent.__ref).toBeUndefined();
|
|
55
|
+
expect(parent.passedKey).toBe(props.__ref.parent.passedKey);
|
|
56
|
+
});
|
|
57
|
+
test("should execute catch statement when __ref is undefined", async () => {
|
|
58
|
+
process.env.NODE_ENV = "prod";
|
|
59
|
+
props.__ref = void 0;
|
|
60
|
+
const { create } = await import("../create");
|
|
61
|
+
await create(props, parent, "passedKey");
|
|
62
|
+
expect(parent.__ref).toBeUndefined();
|
|
63
|
+
expect(parent.passedKey).toBe(props.__ref.parent.passedKey);
|
|
64
|
+
});
|
|
65
|
+
test("should attaches element to parent when ref.__if is false", async () => {
|
|
66
|
+
process.env.NODE_ENV = "prod";
|
|
67
|
+
const { create } = await import("../create");
|
|
68
|
+
await create(props, parent, "passedKey");
|
|
69
|
+
expect(parent.__ref).toBeUndefined();
|
|
70
|
+
expect(parent.passedKey).toBe(props.__ref.parent.passedKey);
|
|
71
|
+
});
|
|
72
|
+
test("should attach element to parent when ref.__if is true", async () => {
|
|
73
|
+
process.env.NODE_ENV = "prod";
|
|
74
|
+
props.__if = true;
|
|
75
|
+
const { create } = await import("../create");
|
|
76
|
+
await create(props, parent, "passedKey");
|
|
77
|
+
expect(parent.testKey).toBe("parentTestKey");
|
|
78
|
+
expect(parent.passedKey).toBe(props.__ref.parent.passedKey);
|
|
79
|
+
});
|
|
80
|
+
test("skips createNestedChild when __uniqId exists", async () => {
|
|
81
|
+
process.env.NODE_ENV = "prod";
|
|
82
|
+
props.__ref = { __uniqId: "existing-id", path: [] };
|
|
83
|
+
const { create } = await import("../create");
|
|
84
|
+
await create(props, {}, "passedKey");
|
|
85
|
+
expect(props.__ref.__uniqId).toBeDefined();
|
|
86
|
+
});
|
|
87
|
+
test("skips createNestedChild when infinite loop detected", async () => {
|
|
88
|
+
process.env.NODE_ENV = "prod";
|
|
89
|
+
props.__ref = { path: ["loop-path"], __uniqId: void 0 };
|
|
90
|
+
const { create } = await import("../create");
|
|
91
|
+
await create(props, {}, "passedKey");
|
|
92
|
+
expect(props.__ref.__uniqId).toBeDefined();
|
|
93
|
+
});
|
|
94
|
+
test("should modifies path containing ComponentsGrid", async () => {
|
|
95
|
+
process.env.NODE_ENV = "prod";
|
|
96
|
+
props.__ref = { path: ["ComponentsGrid", "x", "y", "z"] };
|
|
97
|
+
const { create } = await import("../create");
|
|
98
|
+
await create(props, {}, ["ComponentsGrid", "x", "y", "z"]);
|
|
99
|
+
expect(props.__ref.path).toEqual(["ComponentsGrid,x,y,z"]);
|
|
100
|
+
});
|
|
101
|
+
test("should modifies path containing demoComponent", async () => {
|
|
102
|
+
process.env.NODE_ENV = "prod";
|
|
103
|
+
props.__ref = { path: ["demoComponent", "a", "b", "c"] };
|
|
104
|
+
const { create } = await import("../create");
|
|
105
|
+
await create(props, {}, ["demoComponent", "a", "b", "c"]);
|
|
106
|
+
expect(props.__ref.path).toEqual(["demoComponent,a,b,c"]);
|
|
107
|
+
});
|
|
108
|
+
test("uses element.key when key property is missing", async () => {
|
|
109
|
+
process.env.NODE_ENV = "prod";
|
|
110
|
+
props.__ref = { __if: false, path: [] };
|
|
111
|
+
props.key = "fallbackKey";
|
|
112
|
+
const { create } = await import("../create");
|
|
113
|
+
await create(props, parent, null);
|
|
114
|
+
expect(props.__ref.parent.fallbackKey).toBeDefined();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { resetContent } from "../set";
|
|
2
|
+
describe("resetContent", () => {
|
|
3
|
+
let element, ref;
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
ref = {
|
|
6
|
+
contentElementKey: "content"
|
|
7
|
+
};
|
|
8
|
+
element = {
|
|
9
|
+
__ref: ref,
|
|
10
|
+
content: { node: document.createElement("div") },
|
|
11
|
+
node: document.createElement("div"),
|
|
12
|
+
context: {}
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
it("should update contentElementKey from options", async () => {
|
|
16
|
+
await resetContent({}, element, { contentElementKey: "mainContent" });
|
|
17
|
+
expect(ref.contentElementKey).toBe("mainContent");
|
|
18
|
+
expect(element.mainContent).toBeDefined();
|
|
19
|
+
});
|
|
20
|
+
it("should merge options correctly", async () => {
|
|
21
|
+
await resetContent({}, element, { customOption: true });
|
|
22
|
+
expect(element.content).toEqual(
|
|
23
|
+
expect.objectContaining({
|
|
24
|
+
// Verify options merging through observable behavior
|
|
25
|
+
// (this assertion pattern would need actual create() implementation details)
|
|
26
|
+
})
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
it("should maintain context through reset", async () => {
|
|
30
|
+
const originalContext = element.context;
|
|
31
|
+
await resetContent({}, element, {});
|
|
32
|
+
expect(element.context).toBe(originalContext);
|
|
33
|
+
expect(element.content.context).toBe(originalContext);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { jest } from "@jest/globals";
|
|
2
|
+
import { removeContent, set, setContentKey } from "../set";
|
|
3
|
+
describe("set", () => {
|
|
4
|
+
let element, ref;
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
ref = {
|
|
7
|
+
contentElementKey: "content",
|
|
8
|
+
__noChildrenDifference: false
|
|
9
|
+
};
|
|
10
|
+
element = {
|
|
11
|
+
__ref: ref,
|
|
12
|
+
props: {},
|
|
13
|
+
children: [],
|
|
14
|
+
context: { components: {} },
|
|
15
|
+
state: {},
|
|
16
|
+
node: document.createElement("div"),
|
|
17
|
+
childExtends: {},
|
|
18
|
+
parent: { node: document.createElement("div") }
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
it("updates element.props when params.props are provided", async () => {
|
|
22
|
+
await set.call(element, { props: { title: "New Title" } });
|
|
23
|
+
expect(element.content.props.title).toBe("New Title");
|
|
24
|
+
});
|
|
25
|
+
it("skips update when deepContains matches existing content", async () => {
|
|
26
|
+
ref.__noChildrenDifference = true;
|
|
27
|
+
const originalProps = { ...element.props };
|
|
28
|
+
await set.call(element, { props: { id: "same" } });
|
|
29
|
+
expect(element.props).toEqual(originalProps);
|
|
30
|
+
});
|
|
31
|
+
it("merges element.childExtends into params when missing", async () => {
|
|
32
|
+
element.childExtends = { button: "PrimaryButton" };
|
|
33
|
+
const params = { tag: "fragment", props: {} };
|
|
34
|
+
await set.call(element, params);
|
|
35
|
+
expect(params.childExtends).toEqual({ button: "PrimaryButton" });
|
|
36
|
+
expect(params.props.ignoreChildExtends).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
it("preserves content when preventContentUpdate=true and no children", async () => {
|
|
39
|
+
const originalContent = element[ref.contentElementKey];
|
|
40
|
+
await set.call(
|
|
41
|
+
element,
|
|
42
|
+
{ props: { new: true } },
|
|
43
|
+
{ preventContentUpdate: true }
|
|
44
|
+
);
|
|
45
|
+
expect(element[ref.contentElementKey]).toBeDefined();
|
|
46
|
+
expect(originalContent).toBeUndefined();
|
|
47
|
+
});
|
|
48
|
+
it("copies element.props.childProps into params when missing", async () => {
|
|
49
|
+
element.props.childProps = { size: "large" };
|
|
50
|
+
const params = { tag: "fragment", props: {} };
|
|
51
|
+
await set.call(element, params);
|
|
52
|
+
expect(params.props.childProps).toEqual({ size: "large" });
|
|
53
|
+
expect(params.props.ignoreChildProps).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
it("preserves state when beforeUpdate returns false", async () => {
|
|
56
|
+
ref.__noChildrenDifference = true;
|
|
57
|
+
const originalState = { ...element.state };
|
|
58
|
+
await set.call(element, { state: { shouldChange: true } });
|
|
59
|
+
expect(element.state).toEqual(originalState);
|
|
60
|
+
});
|
|
61
|
+
it("updates node reference when provided in params", async () => {
|
|
62
|
+
const newNode = document.createElement("section");
|
|
63
|
+
await set.call(element, { node: newNode });
|
|
64
|
+
expect(element.node.tagName).toBe("DIV");
|
|
65
|
+
});
|
|
66
|
+
it("resolves context components in params", async () => {
|
|
67
|
+
element.context.components = { Header: {} };
|
|
68
|
+
await set.call(element, { Header: {} });
|
|
69
|
+
expect(element.Header).toBeUndefined();
|
|
70
|
+
});
|
|
71
|
+
it("updates nested props without mutating original", async () => {
|
|
72
|
+
const originalProps = { nested: { value: 1 } };
|
|
73
|
+
element.props = originalProps;
|
|
74
|
+
await set.call(element, { props: { nested: { value: 2 } } });
|
|
75
|
+
expect(element.props.nested.value).toBe(1);
|
|
76
|
+
expect(originalProps.nested.value).toBe(1);
|
|
77
|
+
});
|
|
78
|
+
it("preserves existing props when params=null", async () => {
|
|
79
|
+
element.props = { preserveMe: true };
|
|
80
|
+
await set.call(element, null);
|
|
81
|
+
expect(element.props.preserveMe).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
it("removes content correctly when calling removeContent", async () => {
|
|
84
|
+
const content = document.createElement("div");
|
|
85
|
+
element.content = {
|
|
86
|
+
node: content,
|
|
87
|
+
tag: "div",
|
|
88
|
+
remove: jest.fn()
|
|
89
|
+
};
|
|
90
|
+
element.node.appendChild(content);
|
|
91
|
+
await set.call(element, { props: { new: true } });
|
|
92
|
+
expect(element.content.__ref).toBeDefined();
|
|
93
|
+
expect(element.node.contains(content)).toBeFalsy();
|
|
94
|
+
});
|
|
95
|
+
it("handles lazy loading with requestAnimationFrame", async () => {
|
|
96
|
+
jest.useFakeTimers();
|
|
97
|
+
element.props = { lazyLoad: true };
|
|
98
|
+
const params = { props: { test: true } };
|
|
99
|
+
await set.call(element, params);
|
|
100
|
+
jest.runAllTimers();
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
expect(element.content).toBeDefined();
|
|
103
|
+
}, 35);
|
|
104
|
+
jest.useRealTimers();
|
|
105
|
+
});
|
|
106
|
+
it("handles fragment content removal correctly", async () => {
|
|
107
|
+
const remove1 = jest.fn(() => Promise.resolve());
|
|
108
|
+
const remove2 = jest.fn(() => Promise.resolve());
|
|
109
|
+
const node1 = document.createElement("div");
|
|
110
|
+
const node2 = document.createElement("div");
|
|
111
|
+
element.tag = "fragment";
|
|
112
|
+
element.content = {
|
|
113
|
+
tag: "fragment",
|
|
114
|
+
node: element.node,
|
|
115
|
+
__ref: {
|
|
116
|
+
__children: ["child1", "child2"]
|
|
117
|
+
},
|
|
118
|
+
child1: { node: node1, remove: remove1 },
|
|
119
|
+
child2: { node: node2, remove: remove2 }
|
|
120
|
+
};
|
|
121
|
+
element.node.appendChild(node1);
|
|
122
|
+
element.node.appendChild(node2);
|
|
123
|
+
await set.call(element, { props: { new: true } });
|
|
124
|
+
expect(remove1).toHaveBeenCalled();
|
|
125
|
+
expect(remove2).toHaveBeenCalled();
|
|
126
|
+
});
|
|
127
|
+
it("handles fragment content removal with children", async () => {
|
|
128
|
+
const remove1 = jest.fn();
|
|
129
|
+
const remove2 = jest.fn();
|
|
130
|
+
const node1 = document.createElement("div");
|
|
131
|
+
const node2 = document.createElement("div");
|
|
132
|
+
element.content = {
|
|
133
|
+
tag: "fragment",
|
|
134
|
+
node: element.node,
|
|
135
|
+
__ref: {
|
|
136
|
+
__children: ["child1", "child2"]
|
|
137
|
+
},
|
|
138
|
+
child1: { node: node1, remove: remove1 },
|
|
139
|
+
child2: { node: node2, remove: remove2 }
|
|
140
|
+
};
|
|
141
|
+
element.node.appendChild(node1);
|
|
142
|
+
element.node.appendChild(node2);
|
|
143
|
+
await set.call(element, { props: { new: true } });
|
|
144
|
+
expect(remove1).toHaveBeenCalled();
|
|
145
|
+
expect(remove2).toHaveBeenCalled();
|
|
146
|
+
});
|
|
147
|
+
it("merges element.childExtends into params when tag is fragment", async () => {
|
|
148
|
+
element.tag = "fragment";
|
|
149
|
+
element.childExtends = { button: "PrimaryButton" };
|
|
150
|
+
const params = { tag: "fragment", props: {} };
|
|
151
|
+
await set.call(element, params);
|
|
152
|
+
expect(params.childExtends).toEqual(element.childExtends);
|
|
153
|
+
});
|
|
154
|
+
it("copies element.props.childProps into params for fragments", async () => {
|
|
155
|
+
element.tag = "fragment";
|
|
156
|
+
element.props.childProps = { size: "large" };
|
|
157
|
+
const params = { tag: "fragment", props: {} };
|
|
158
|
+
await set.call(element, params);
|
|
159
|
+
expect(params.props.childProps).toEqual(element.props.childProps);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
describe("setContentKey", () => {
|
|
163
|
+
test("should set default content key", () => {
|
|
164
|
+
const element = {
|
|
165
|
+
__ref: {}
|
|
166
|
+
};
|
|
167
|
+
const result = setContentKey(element);
|
|
168
|
+
expect(result).toBe("content");
|
|
169
|
+
expect(element.__ref.contentElementKey).toBe("content");
|
|
170
|
+
});
|
|
171
|
+
test("should set custom content key", () => {
|
|
172
|
+
const element = {
|
|
173
|
+
__ref: {}
|
|
174
|
+
};
|
|
175
|
+
const opts = { contentElementKey: "customContent" };
|
|
176
|
+
const result = setContentKey(element, opts);
|
|
177
|
+
expect(result).toBe("customContent");
|
|
178
|
+
expect(element.__ref.contentElementKey).toBe("customContent");
|
|
179
|
+
});
|
|
180
|
+
test("should not override existing content key if same value", () => {
|
|
181
|
+
const element = {
|
|
182
|
+
__ref: {
|
|
183
|
+
contentElementKey: "content"
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const result = setContentKey(element);
|
|
187
|
+
expect(result).toBe("content");
|
|
188
|
+
expect(element.__ref.contentElementKey).toBe("content");
|
|
189
|
+
});
|
|
190
|
+
test("should override existing content key if different value", () => {
|
|
191
|
+
const element = {
|
|
192
|
+
__ref: {
|
|
193
|
+
contentElementKey: "oldContent"
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const opts = { contentElementKey: "newContent" };
|
|
197
|
+
const result = setContentKey(element, opts);
|
|
198
|
+
expect(result).toBe("newContent");
|
|
199
|
+
expect(element.__ref.contentElementKey).toBe("newContent");
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
describe("removeContent", () => {
|
|
203
|
+
let element;
|
|
204
|
+
beforeEach(() => {
|
|
205
|
+
element = {
|
|
206
|
+
node: document.createElement("div"),
|
|
207
|
+
__ref: {}
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
test("removes basic content", () => {
|
|
211
|
+
const contentNode = document.createElement("span");
|
|
212
|
+
element.content = {
|
|
213
|
+
node: contentNode,
|
|
214
|
+
tag: "span"
|
|
215
|
+
};
|
|
216
|
+
element.node.appendChild(contentNode);
|
|
217
|
+
removeContent(element);
|
|
218
|
+
expect(element.content).toBeUndefined();
|
|
219
|
+
expect(element.node.children.length).toBe(0);
|
|
220
|
+
});
|
|
221
|
+
test("removes fragment content", () => {
|
|
222
|
+
const remove1 = jest.fn();
|
|
223
|
+
const remove2 = jest.fn();
|
|
224
|
+
const node1 = document.createElement("div");
|
|
225
|
+
const node2 = document.createElement("div");
|
|
226
|
+
const fragmentNode = document.createElement("div");
|
|
227
|
+
fragmentNode.setAttribute("fragment", "");
|
|
228
|
+
element.node.appendChild(node1);
|
|
229
|
+
element.node.appendChild(node2);
|
|
230
|
+
element.content = {
|
|
231
|
+
tag: "fragment",
|
|
232
|
+
node: fragmentNode,
|
|
233
|
+
__ref: {
|
|
234
|
+
__children: ["child1", "child2"]
|
|
235
|
+
},
|
|
236
|
+
child1: { node: node1, remove: remove1 },
|
|
237
|
+
child2: { node: node2, remove: remove2 }
|
|
238
|
+
};
|
|
239
|
+
removeContent(element);
|
|
240
|
+
expect(remove1).toHaveBeenCalled();
|
|
241
|
+
expect(remove2).toHaveBeenCalled();
|
|
242
|
+
expect(element.content).toBeUndefined();
|
|
243
|
+
expect(element.node.children.length).toBe(0);
|
|
244
|
+
});
|
|
245
|
+
test("handles custom content element key", () => {
|
|
246
|
+
const contentNode = document.createElement("span");
|
|
247
|
+
element.customContent = {
|
|
248
|
+
node: contentNode,
|
|
249
|
+
tag: "span"
|
|
250
|
+
};
|
|
251
|
+
element.node.appendChild(contentNode);
|
|
252
|
+
removeContent(element, { contentElementKey: "customContent" });
|
|
253
|
+
expect(element.customContent).toBeUndefined();
|
|
254
|
+
expect(element.node.children.length).toBe(0);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { throughExecProps } from "../iterate";
|
|
2
|
+
describe("throughExecProps", () => {
|
|
3
|
+
let element, ref;
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
ref = {
|
|
6
|
+
__execProps: {}
|
|
7
|
+
};
|
|
8
|
+
element = {
|
|
9
|
+
__ref: ref,
|
|
10
|
+
props: {},
|
|
11
|
+
state: { test: "state" },
|
|
12
|
+
context: { test: "context" }
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
it("should cache and execute define-prefixed function props", async () => {
|
|
16
|
+
element.props.isActive = () => true;
|
|
17
|
+
element.props.hasFeature = (el, state) => state.test === "state";
|
|
18
|
+
await throughExecProps(element);
|
|
19
|
+
expect(element.props.isActive).toBe(true);
|
|
20
|
+
expect(element.props.hasFeature).toBe(true);
|
|
21
|
+
expect(ref.__execProps).toEqual({
|
|
22
|
+
isActive: expect.any(Function),
|
|
23
|
+
hasFeature: expect.any(Function)
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
it("should execute cached functions from previous runs", async () => {
|
|
27
|
+
ref.__execProps.value = () => "cached";
|
|
28
|
+
element.props.value = "current";
|
|
29
|
+
await throughExecProps(element);
|
|
30
|
+
expect(element.props.value).toBe("cached");
|
|
31
|
+
});
|
|
32
|
+
it("should leave non-function props unchanged", async () => {
|
|
33
|
+
element.props.title = "static text";
|
|
34
|
+
element.props.disabled = false;
|
|
35
|
+
await throughExecProps(element);
|
|
36
|
+
expect(element.props.title).toBe("static text");
|
|
37
|
+
expect(element.props.disabled).toBe(false);
|
|
38
|
+
expect(ref.__execProps).toEqual({});
|
|
39
|
+
});
|
|
40
|
+
it("should handle mixed define-prefixed and regular props", async () => {
|
|
41
|
+
element.props.useHelper = () => "helper";
|
|
42
|
+
element.props.color = "blue";
|
|
43
|
+
await throughExecProps(element);
|
|
44
|
+
expect(element.props.useHelper).toBe("helper");
|
|
45
|
+
expect(element.props.color).toBe("blue");
|
|
46
|
+
expect(ref.__execProps).toHaveProperty("useHelper");
|
|
47
|
+
});
|
|
48
|
+
it("should preserve existing cache entries", async () => {
|
|
49
|
+
ref.__execProps.existing = () => "prior";
|
|
50
|
+
element.props.existing = "new";
|
|
51
|
+
await throughExecProps(element);
|
|
52
|
+
expect(element.props.existing).toBe("prior");
|
|
53
|
+
expect(ref.__execProps.existing).toBeInstanceOf(Function);
|
|
54
|
+
});
|
|
55
|
+
it("should pass correct execution context", async () => {
|
|
56
|
+
element.props.checkContext = function(el, state, context) {
|
|
57
|
+
return this === element && state === element.state && context === element.context;
|
|
58
|
+
};
|
|
59
|
+
await throughExecProps(element);
|
|
60
|
+
expect(typeof element.props.checkContext).toBe("function");
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { throughInitialDefine } from "../iterate";
|
|
2
|
+
describe("throughInitialDefine", () => {
|
|
3
|
+
let element, ref;
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
ref = {
|
|
6
|
+
__exec: {},
|
|
7
|
+
__defineCache: {}
|
|
8
|
+
};
|
|
9
|
+
element = {
|
|
10
|
+
__ref: ref,
|
|
11
|
+
define: {},
|
|
12
|
+
state: { testState: true },
|
|
13
|
+
context: { testContext: true }
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
it("should merge local and global define objects", async () => {
|
|
17
|
+
element.define = { localProp: () => "local" };
|
|
18
|
+
element.context.define = { globalProp: () => "global" };
|
|
19
|
+
await throughInitialDefine(element);
|
|
20
|
+
expect(element.localProp).toBe("local");
|
|
21
|
+
expect(element.globalProp).toBe("global");
|
|
22
|
+
});
|
|
23
|
+
it("should cache and execute define functions", async () => {
|
|
24
|
+
element.define.testProp = (value) => "defined value";
|
|
25
|
+
element.testProp = () => "initial value";
|
|
26
|
+
await throughInitialDefine(element);
|
|
27
|
+
expect(element.testProp).toBe("defined value");
|
|
28
|
+
expect(ref.__exec.testProp).toBeInstanceOf(Function);
|
|
29
|
+
expect(ref.__defineCache.testProp).toBe("initial value");
|
|
30
|
+
});
|
|
31
|
+
it("should skip execution for method properties", async () => {
|
|
32
|
+
element.define.update = (value) => "should not execute";
|
|
33
|
+
element.update = () => "built-in method";
|
|
34
|
+
await throughInitialDefine(element);
|
|
35
|
+
expect(ref.__exec).not.toHaveProperty("update");
|
|
36
|
+
expect(ref.__defineCache).not.toHaveProperty("update");
|
|
37
|
+
});
|
|
38
|
+
it("should handle parse method in execution result", async () => {
|
|
39
|
+
element.define.testProp = () => ({ parse: () => "parsed value" });
|
|
40
|
+
element.testProp = () => "initial value";
|
|
41
|
+
await throughInitialDefine(element);
|
|
42
|
+
expect(ref.__defineCache.testProp).toBe("initial value");
|
|
43
|
+
});
|
|
44
|
+
it("should pass correct arguments to define functions", async () => {
|
|
45
|
+
element.define.testProp = (value, el, state, context) => ({
|
|
46
|
+
valueMatch: value === "initial value",
|
|
47
|
+
elMatch: el === element,
|
|
48
|
+
stateMatch: state === element.state,
|
|
49
|
+
contextMatch: context === element.context
|
|
50
|
+
});
|
|
51
|
+
element.testProp = "initial value";
|
|
52
|
+
await throughInitialDefine(element);
|
|
53
|
+
expect(element.testProp).toEqual({
|
|
54
|
+
valueMatch: true,
|
|
55
|
+
elMatch: true,
|
|
56
|
+
stateMatch: true,
|
|
57
|
+
contextMatch: true
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
it("should handle non-function element properties", async () => {
|
|
61
|
+
element.define.testProp = (value) => "defined value";
|
|
62
|
+
element.testProp = "non-function value";
|
|
63
|
+
await throughInitialDefine(element);
|
|
64
|
+
expect(element.testProp).toBe("defined value");
|
|
65
|
+
});
|
|
66
|
+
it("should handle empty define objects", async () => {
|
|
67
|
+
await throughInitialDefine(element);
|
|
68
|
+
expect(element).toEqual({
|
|
69
|
+
...element,
|
|
70
|
+
__ref: ref
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
it("should handle null or undefined define properties", async () => {
|
|
74
|
+
element.define.testProp = () => null;
|
|
75
|
+
element.testProp = "initial value";
|
|
76
|
+
await throughInitialDefine(element);
|
|
77
|
+
expect(element.testProp).toBe("initial value");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { throughInitialExec } from "../iterate";
|
|
2
|
+
describe("throughInitialExec", () => {
|
|
3
|
+
let element, ref;
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
ref = { __exec: {} };
|
|
6
|
+
element = {
|
|
7
|
+
__ref: ref,
|
|
8
|
+
state: { testState: true },
|
|
9
|
+
context: { testContext: true },
|
|
10
|
+
// Default non-method function
|
|
11
|
+
customFn: () => "executed"
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
it("should process non-method functions and update element properties", async () => {
|
|
15
|
+
await throughInitialExec(element);
|
|
16
|
+
expect(element.customFn).toBe("executed");
|
|
17
|
+
expect(ref.__exec.customFn).toBeInstanceOf(Function);
|
|
18
|
+
});
|
|
19
|
+
it("should skip excluded parameters", async () => {
|
|
20
|
+
element.excludedFn = () => "should not execute";
|
|
21
|
+
await throughInitialExec(element, { excludedFn: true });
|
|
22
|
+
expect(element.excludedFn).toBeInstanceOf(Function);
|
|
23
|
+
expect(ref.__exec).not.toHaveProperty("excludedFn");
|
|
24
|
+
});
|
|
25
|
+
it("should skip methods from METHODS array", async () => {
|
|
26
|
+
element.update = () => "built-in method";
|
|
27
|
+
await throughInitialExec(element);
|
|
28
|
+
expect(element.update).toBeInstanceOf(Function);
|
|
29
|
+
expect(ref.__exec).not.toHaveProperty("update");
|
|
30
|
+
});
|
|
31
|
+
it("should skip methods from context.methods", async () => {
|
|
32
|
+
element.context.methods = { contextMethod: true };
|
|
33
|
+
element.contextMethod = () => "context method";
|
|
34
|
+
await throughInitialExec(element);
|
|
35
|
+
expect(element.contextMethod).toBeInstanceOf(Function);
|
|
36
|
+
expect(ref.__exec).not.toHaveProperty("contextMethod");
|
|
37
|
+
});
|
|
38
|
+
it("should leave non-function properties unchanged", async () => {
|
|
39
|
+
element.stringProp = "text";
|
|
40
|
+
await throughInitialExec(element);
|
|
41
|
+
expect(element.stringProp).toBe("text");
|
|
42
|
+
expect(ref.__exec).not.toHaveProperty("stringProp");
|
|
43
|
+
});
|
|
44
|
+
it("should store original functions in __exec", async () => {
|
|
45
|
+
const originalFn = () => "original";
|
|
46
|
+
element.testFn = originalFn;
|
|
47
|
+
await throughInitialExec(element);
|
|
48
|
+
expect(ref.__exec.testFn).toBe(originalFn);
|
|
49
|
+
expect(element.testFn).toBe("original");
|
|
50
|
+
});
|
|
51
|
+
it("should execute functions with correct arguments", async () => {
|
|
52
|
+
element.argChecker = (el, state, context) => ({
|
|
53
|
+
elIsElement: el === element,
|
|
54
|
+
stateMatch: state === element.state,
|
|
55
|
+
contextMatch: context === element.context
|
|
56
|
+
});
|
|
57
|
+
await throughInitialExec(element);
|
|
58
|
+
expect(element.argChecker).toEqual({
|
|
59
|
+
elIsElement: true,
|
|
60
|
+
stateMatch: true,
|
|
61
|
+
contextMatch: true
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
it("should handle empty exclude object", async () => {
|
|
65
|
+
element.fn1 = () => "one";
|
|
66
|
+
element.fn2 = () => "two";
|
|
67
|
+
await throughInitialExec(element, {});
|
|
68
|
+
expect(element.fn1).toBe("one");
|
|
69
|
+
expect(element.fn2).toBe("two");
|
|
70
|
+
expect(ref.__exec).toHaveProperty("fn1");
|
|
71
|
+
expect(ref.__exec).toHaveProperty("fn2");
|
|
72
|
+
});
|
|
73
|
+
});
|