-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.test.js
200 lines (162 loc) · 7.22 KB
/
loader.test.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint-disable max-len */
import compiler from './compiler';
describe('text content', () => {
test('should properly generate template for text-content', async () => {
const stats = await compiler('./fixtures/only-text.rhtml');
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":["only text!"]};"',
);
});
});
describe('`csp` option', () => {
test("shouldn't produces functions when `csp` parseOptions is set to `false`", async () => {
const stats = await compiler('./fixtures/template-with-expression.rhtml', {
parseOptions: {
csp: false,
},
});
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":[{"t":7,"e":"div","f":[{"t":2,"x":{"r":["test"],"s":"_0+1"}}]}]};"',
);
});
test('should produces functions when `csp` parseOptions is set to `true`', async () => {
const stats = await compiler('./fixtures/template-with-expression.rhtml', {
parseOptions: {
csp: true,
},
});
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":[{"t":7,"e":"div","f":[{"t":2,"x":{"r":["test"],"s":"_0+1"}}]}],"e":{"_0+1":function (_0){return(_0+1);}}};"',
);
});
});
describe('esModule', () => {
test('should return module.export = when `esModule` is false', async () => {
const stats = await compiler('./fixtures/only-text.rhtml', {
esModule: false,
});
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"module.exports = {"v":4,"t":["only text!"]};"',
);
});
test('should return `export default` when `esModule` is true', async () => {
const stats = await compiler('./fixtures/only-text.rhtml', {
esModule: true,
});
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":["only text!"]};"',
);
});
test('should return `export default` when `esModule` is not set', async () => {
const stats = await compiler('./fixtures/only-text.rhtml');
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":["only text!"]};"',
);
});
});
describe('sources handling', () => {
test('should properly generate template when image tag with src is present', async () => {
const stats = await compiler('./fixtures/image.rhtml');
const output = stats.toJson({ source: true }).modules[0].modules[0];
expect(output.source).toMatchInlineSnapshot(`
"import res0 from "./image-local.png";
export default {"v":4,"t":["Text ",{"t":7,"e":"img","m":[{"n":"src","f":"" + res0 + "","t":13,"g":1}]}," Text"]};"
`);
});
test('should accept attrs (space separated)', async () => {
const stats = await compiler('./fixtures/image-and-script.rhtml', {
attrs: 'script:src img:src',
});
const output = stats.toJson({ source: true }).modules[0].modules[0];
expect(output.source).toMatchInlineSnapshot(`
"import res0 from "./assets/script.js";
import res1 from "./image-local.png";
export default {"v":4,"t":["Text ",{"t":7,"e":"script","m":[{"n":"src","f":"" + res0 + "","t":13,"g":1}]}," ",{"t":7,"e":"img","m":[{"n":"src","f":"" + res1 + "","t":13,"g":1}]}]};"
`);
});
test('should accept attrs (array)', async () => {
const stats = await compiler('./fixtures/image-and-script.rhtml', {
attrs: ['script:src', 'img:src'],
});
const output = stats.toJson({ source: true }).modules[0].modules[0];
expect(output.source).toMatchInlineSnapshot(`
"import res0 from "./assets/script.js";
import res1 from "./image-local.png";
export default {"v":4,"t":["Text ",{"t":7,"e":"script","m":[{"n":"src","f":"" + res0 + "","t":13,"g":1}]}," ",{"t":7,"e":"img","m":[{"n":"src","f":"" + res1 + "","t":13,"g":1}]}]};"
`);
});
test('should not process nothing if `attrs` is `false`', async () => {
const stats = await compiler('./fixtures/image-and-script.rhtml', {
attrs: false,
});
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":["Text ",{"t":7,"e":"script","m":[{"n":"src","f":"./assets/script.js","t":13,"g":1}]}," ",{"t":7,"e":"img","m":[{"n":"src","f":"image-local.png","t":13,"g":1}]}]};"',
);
});
test('should process all tag when specify only one attribute', async () => {
const stats = await compiler('./fixtures/image-and-script.rhtml', {
attrs: ':src',
});
const output = stats.toJson({ source: true }).modules[0].modules[0];
expect(output.source).toMatchInlineSnapshot(`
"import res0 from "./assets/script.js";
import res1 from "./image-local.png";
export default {"v":4,"t":["Text ",{"t":7,"e":"script","m":[{"n":"src","f":"" + res0 + "","t":13,"g":1}]}," ",{"t":7,"e":"img","m":[{"n":"src","f":"" + res1 + "","t":13,"g":1}]}]};"
`);
});
test('should ignore hash fragments in URLs', async () => {
const stats = await compiler('./fixtures/image-with-hash.rhtml', {
esModule: true,
});
const output = stats.toJson({ source: true }).modules[0].modules[0];
expect(output.source).toMatchInlineSnapshot(`
"import res0 from "./image-local.png";
export default {"v":4,"t":[{"t":7,"e":"img","m":[{"n":"src","f":"" + res0 + "#hash","t":13,"g":1}]}]};"
`);
});
test("should ignore anchor with 'mailto:' in the href attribute", async () => {
const stats = await compiler('./fixtures/link-mail-to.rhtml');
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":[{"t":7,"e":"a","m":[{"n":"href","f":"mailto:username@exampledomain.com","t":13,"g":1}],"f":["Mail to"]}]};"',
);
});
test('should accept root from options', async () => {
const stats = await compiler('./fixtures/image-root.rhtml', {
root: './assets',
});
const output = stats.toJson({ source: true }).modules[0].modules[0];
expect(output.source).toMatchInlineSnapshot(`
"import res0 from "./assets/image.png";
export default {"v":4,"t":["Text ",{"t":7,"e":"img","m":[{"n":"src","f":"" + res0 + "","t":13,"g":1}]}," Text"]};"
`);
});
test('should handle braces with a computation', async () => {
const stats = await compiler('./fixtures/image-with-computed-src.rhtml', {
root: './assets',
});
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":[{"t":7,"e":"img","m":[{"n":"src","f":[{"t":2,"r":"computed"}],"t":13}]}]};"',
);
});
test('should handle braces with a partial computation', async () => {
const stats = await compiler(
'./fixtures/image-with-partial-computed-src.rhtml',
{
root: './assets',
},
);
const output = stats.toJson({ source: true }).modules[0];
expect(output.source).toMatchInlineSnapshot(
'"export default {"v":4,"t":[{"t":7,"e":"img","m":[{"n":"src","f":["/image-",{"t":2,"r":"partialComputation"},".png"],"t":13}]}]};"',
);
});
});