Skip to content

Commit 17c2140

Browse files
committed
Tests for Include
1 parent b3cdd74 commit 17c2140

8 files changed

+124
-12
lines changed

src/Modulo.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,12 @@ modulo.register('util', function newNode(innerHTML, tag) {
812812
modulo.register('util', function loadHead(modulo, loadMode, elem, knownBundled, doc = null) {
813813
doc = doc || window.document;
814814
const { newNode, hash } = modulo.registry.utils;
815-
const id = hash(elem.name || elem.src || elem.href || elem.innerHTML);
815+
const url = elem.getAttribute('src') || elem.getAttribute('href') || null;
816+
const id = hash(elem.name || url || elem.textContent);
816817
if (doc.getElementById(id) || knownBundled[id]) {
817818
return; // already exists, never add twice
818819
}
819-
knownBundled[id] = loadMode === 'bundle'; // (set to true if bundling this)
820+
knownBundled[id] = loadMode === 'bundle' ? url : false; // (if bundling this)
820821
const newElem = newNode(elem.innerHTML, elem.tagName);
821822
for (const attr of elem.attributes || []) { // Copy all attributes from old elem
822823
newElem.setAttributeNode(attr.cloneNode(true)); // ...to new elem
@@ -1444,29 +1445,34 @@ modulo.config.include = {
14441445
GlobalBundled: { },
14451446
LoadMode: 'bundle',
14461447
ServerTemplate: 'https://{{ server }}/{{ path }}',
1447-
TagTemplate: '{% if isCSS %}<link name="{{ package }}" rel="stylesheet" href="{{ url }}" />' +
1448-
'{% else %}<script name="{{ package }}" src="{{ url }}"></' + 'script>{% endif %}',
1449-
DefLoaders: [ 'DefTarget', 'DefinedAs', 'Src', 'Server', 'LoadMode' ],
1448+
TagTemplate: '{% if isCSS %}<link rel="stylesheet" href="{{ url }}" />' +
1449+
'{% else %}<script src="{{ url }}"></' + 'script>{% endif %}',
1450+
DefLoaders: [ 'DefTarget', 'DefinedAs', 'Src', 'Server' ],
1451+
DefBuilders: [ 'LoadMode' ],
14501452
};
14511453
modulo.register('cpart', class Include {
1452-
static LoadMode(modulo, def, value) { // Loads given Include def
1453-
const { loadHead, newNode, keyFilter } = modulo.registry.utils;
1454+
static Server(modulo, def, value) { // Loads given Include def
1455+
const { keyFilter } = modulo.registry.utils;
14541456
const lower = key => key[0].toLowerCase() === key[0]; // skip "-prefixed"
1455-
const ctx = { def, modulo, headContent: def.Content || '' };
1457+
const ctx = { def, modulo, server: value };
14561458
const render = code => new modulo.registry.cparts.Template(code).render(ctx);
1459+
def.Content = def.Content || '';
14571460
for (const [ pkg, v ] of Object.entries(keyFilter(def, lower))) {
14581461
ctx.path = v.contains('@') ? v : pkg + (v ? ('@' + v) : '');
14591462
ctx.isCSS = (ctx.path.contains('/') && ctx.path.endsWith('.css'));
14601463
ctx.url = render(def.ServerTemplate); // Render URL using ServerTemplate
1461-
ctx.headContent += render(def.TagTemplate); // Append tag to head Content
1464+
def.Content += render(def.TagTemplate); // Append tag to head Content
14621465
}
1463-
for (const elem of newNode(ctx.headContent).children) { // Loop through combined
1466+
}
1467+
static LoadMode(modulo, def, value) { // Loads given Include def
1468+
const { loadHead, newNode } = modulo.registry.utils;
1469+
for (const elem of newNode(def.Content).children) { // Loop through combined
14641470
loadHead(modulo, value, elem, modulo.config.include.GlobalBundled);
14651471
}
14661472
}
14671473
intitializedCallback(renderObj) {
14681474
const { Include } = this.modulo.registry.cparts; // Can I use self. reliably?
1469-
Include.LoadMode(this.modulo, this.attrs, 'lazy', { }); // Always load if ID not exist
1475+
Include.LoadMode(this.modulo, this.conf, 'lazy', { }); // Always load if ID not exist
14701476
}
14711477
});
14721478
modulo.register('coreDef', modulo.registry.cparts.Include); // Allow globally

tests/cparts/component_mode.test.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ <h1>Hello World</h1>
140140
</script>
141141

142142
<script name="HTML head element is as expected" skip-rerender>
143-
const expectedHead = '<link href="whatevs.css" id="xx6e87uf"><title id="xxu3uhhn">Hello Vanish</title>';
143+
const expectedHead = '<link href="whatevs.css" id="x1hbf95j"><title id="xxu3uhhn">Hello Vanish</title>';
144144
const actual = document.head.innerHTML;
145145
const testTitle = '<title>testworld</title>'; // used in new tests
146146

tests/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Library -src="./multi-file-tests/loading_style_src.test.html"></Library>
2424
<Library -src="./cparts/component_slot.test.html"></Library>
2525
<Library -src="./cparts/component_namespace.test.html"></Library>
26+
<Library -src="./multi-file-tests/include.test.html"></Library>
2627

2728
<!-- Also, do doc tests (makes unmocked, real HTTP requests to APIs) -->
2829
<!--
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
<script Configuration>
3+
modulo.config.include.quux = 'mux'
4+
/*
5+
const originalHash = modulo.registry.utils.hash;
6+
modulo.register('util', function hash (str) {
7+
if (str.startsWith('http')) {
8+
console.log(str);
9+
}
10+
str = str.replace(/^https?...[a-z0-9]+\.[a-z0-9]+:?[0-9]* ?/i, '');
11+
return originalHash(str);
12+
});
13+
*/
14+
</script>
15+
16+
<Include -name="IncludeTestGlobalInclude">
17+
<script src="/demos/tests/multi-file-tests/include_script_src.js"></script>
18+
<link rel="stylesheet" href="/demos/tests/multi-file-tests/include_style_src.css" />
19+
<script>
20+
modulo.config.include.E_SE = modulo.config.include.E_SE || 0;
21+
modulo.config.include.E_SE++;
22+
</script>
23+
</Include>
24+
25+
26+
<Component name="GlobalInclude_Test">
27+
28+
<TestSuite>
29+
<Test name="Global Include works">
30+
<Script name="Global settings register">
31+
assert: modulo.config.include.quux === 'mux'
32+
</Script>
33+
34+
<Script name="Global include runs">
35+
const se = modulo.config.include.GLOBAL_SIDEEFFECT;
36+
assert: se === 'include_script_src'
37+
</Script>
38+
39+
<Script name="Global include runs once">
40+
const count = modulo.config.include.GLOBAL_SIDEEFFECT_COUNT;
41+
assert: count === 1
42+
</Script>
43+
44+
<Script name="Global generates expected global bundle stuff">
45+
const gb = modulo.config.include.GlobalBundled;
46+
const actual = Object.entries(gb).filter(([k, v]) => v || v === null).map(([k, v]) => k).join(',');
47+
const expected = 'x1l3gfn1,xxbha5e5,x11e84tr';
48+
assert: actual === expected
49+
</Script>
50+
</Test>
51+
<Test name="Global Include of script tag works">
52+
<Script name="Global include runs once">
53+
const count = modulo.config.include.E_SE;
54+
assert: count === 1
55+
</Script>
56+
</Test>
57+
</TestSuite>
58+
</Component>
59+
60+
61+
<Component name="IncludeComponentLazy_Test">
62+
<Include -name="IncludeComponentLazy" -load-mode="lazy">
63+
<script src="/demos/tests/multi-file-tests/include_script_src.js"></script>
64+
<script src="/demos/tests/multi-file-tests/include_script_src_2.js"></script>
65+
<link rel="stylesheet" href="/demos/tests/multi-file-tests/include_style_src_2.css" />
66+
</Include>
67+
68+
<TestSuite>
69+
<Test name="Component Include works">
70+
<Script name="Component include runs">
71+
const se = modulo.config.include.SIDEEFFECT;
72+
assert: se === 'include_script_src_2'
73+
</Script>
74+
75+
<Script name="Component include runs once">
76+
const count = modulo.config.include.SIDEEFFECT_COUNT;
77+
assert: count === 1
78+
</Script>
79+
80+
<Script name="Component generates expected global bundle stuff">
81+
const gb = modulo.config.include.GlobalBundled;
82+
const actual = Array.from(Object.keys(gb)).join(',');
83+
const expected = 'x1l3gfn1,xxbha5e5,x11e84tr,x1047o1i,xx4grn08';
84+
assert: actual === expected
85+
</Script>
86+
</Test>
87+
</TestSuite>
88+
</Component>
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
modulo.config.include.GLOBAL_SIDEEFFECT_COUNT = modulo.config.include.GLOBAL_SIDEEFFECT_COUNT || 0;
3+
modulo.config.include.GLOBAL_SIDEEFFECT = 'include_script_src'
4+
modulo.config.include.GLOBAL_SIDEEFFECT_COUNT++;
5+
6+
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
modulo.config.include.SIDEEFFECT_COUNT = modulo.config.include.SIDEEFFECT_COUNT || 0;
3+
modulo.config.include.SIDEEFFECT = 'include_script_src_2'
4+
modulo.config.include.SIDEEFFECT_COUNT++;
5+
6+
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { --foobar: 'quuxmux'; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { --foobar2: 'quuxmux_2'; }

0 commit comments

Comments
 (0)