-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathview.test.tsx
More file actions
182 lines (173 loc) · 5.62 KB
/
view.test.tsx
File metadata and controls
182 lines (173 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) 2024 Huawei Technologies Co.,Ltd.
*
* openInula is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
import { describe, expect, it } from 'vitest';
import { transform } from './mock';
describe('view generation', () => {
it('should generation single html', () => {
const code = transform(/*js*/ `
const Comp = Component(() => {
let text = 'hello world';
console.log(text);
return <div>{text}</div>
})
`);
expect(code).toMatchInlineSnapshot(`
"function Comp() {
const $$self = $$compBuilder();
let text = 'hello world';
console.log(text);
return $$self.prepare().init($$createHTMLNode("div", null, $$createExpNode(() => text, () => [text], 1)));
}"
`);
});
it('should generate html properties and update', () => {
const code = transform(/*js*/ `
const Comp = Component(() => {
let text = 'hello world';
let color = 'red';
return <div className={text} id={text} style={{color}}>{text}</div>
})
`);
expect(code).toMatchInlineSnapshot(`
"function Comp() {
const $$self = $$compBuilder();
let text = 'hello world';
let color = 'red';
return $$self.prepare().init($$createHTMLNode("div", $$node => {
$$setHTMLProp($$node, "className", () => text, [text], 1);
$$setHTMLProp($$node, "id", () => text, [text], 1);
$$setStyle($$node, () => ({
color
}), [color], 2);
}, $$createExpNode(() => text, () => [text], 1)));
}"
`);
});
it('should generate multiple html', () => {
const code = transform(/*js*/ `
const Comp = Component(() => {
let text = 'hello world';
return (
<div>
<div>{text}</div>
<div>{text}</div>
</div>
)
})
`);
expect(code).toMatchInlineSnapshot(`
"const _$t = function () {
const $node0 = $$createElement("div");
const $node1 = $$createElement("div");
$node0.appendChild($node1);
const $node2 = $$createElement("div");
$node0.appendChild($node2);
return $node0;
}();
function Comp() {
const $$self = $$compBuilder();
let text = 'hello world';
return $$self.prepare().init($$createTemplateNode(_$t, null, [0, $$createExpNode(() => text, () => [text], 1), 0], [0, $$createExpNode(() => text, () => [text], 1), 1]));
}"
`);
});
it('should support fragment', () => {
const code = transform(/*js*/ `
const Comp = Component(() => {
let text = 'hello world';
return (
<>
<div>{text}</div>
<div>{text}</div>
</>
)
})
`);
expect(code).toMatchInlineSnapshot(`
"function Comp() {
const $$self = $$compBuilder();
let text = 'hello world';
return $$self.prepare().init($$createFragmentNode($$createHTMLNode("div", null, $$createExpNode(() => text, () => [text], 1)), $$createHTMLNode("div", null, $$createExpNode(() => text, () => [text], 1))));
}"
`);
});
it('should generate conditional html', () => {
const code = transform(/*js*/ `
const Comp = Component(() => {
let text = 'hello world';
let show = true;
return (
<div>
<if cond={show}>
<div>{text}</div>
</if>
<else>
<h1>else</h1>
</else>
</div>
);
});
`);
expect(code).toMatchInlineSnapshot(`
"function Comp() {
const $$self = $$compBuilder();
let text = 'hello world';
let show = true;
return $$self.prepare().init($$createHTMLNode("div", null, $$createConditionalNode($$node => {
if ($$node.cachedCondition(0, () => show, [show])) {
if ($$node.branch(0)) return [];
return [$$createHTMLNode("div", null, $$createExpNode(() => text, () => [text], 1))];
} else {
if ($$node.branch(1)) return [];
return [$$createHTMLNode("h1", $$node => {
$$node.textContent = "else";
})];
}
}, 2)));
}"
`);
});
it('should generate loop html', () => {
const code = transform(/*js*/ `
const Comp = Component(() => {
let list = ['hello', 'world'];
return (
<div>
<for each={list}>
{(item, index) => <div key={index}>{item}</div>})}
</for>
</div>
);
});
`);
expect(code).toMatchInlineSnapshot(`
"function Comp() {
const $$self = $$compBuilder();
let list = ['hello', 'world'];
return $$self.prepare().init($$createHTMLNode("div", null, $$createForNode(() => list, () => {
return list?.map?.((item, index) => index);
}, ($$n, updateItemFuncArr, item, $$key, index) => {
updateItemFuncArr[index] = (newItem, newIdx) => {
item = newItem;
index = newIdx;
};
return [$$createHTMLNode("div", $$node => {
$$node.setAttribute("key", index);
}, $$createExpNode(() => item, () => [item], 1))];
}, 1)));
}"
`);
});
});