Skip to content

Commit dd59d5b

Browse files
committed
Added tests for script tag transport method
1 parent 035dad4 commit dd59d5b

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

src/Modulo.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ window.moduloPrevious = window.modulo;
1212
window.Modulo = class Modulo {
1313
constructor() {
1414
window._moduloID = (window._moduloID || 0) + 1;
15+
this.window = window;
1516
this.id = window._moduloID; // Every Modulo instance gets a unique ID.
1617
this._configSteps = 0; // Used to check for an infinite loop during load
1718
this.registry = { cparts: { }, coreDefs: { }, utils: { }, core: { },
@@ -909,10 +910,11 @@ modulo.register('core', class FetchQueue {
909910
resolve(this.data[src], src); // (sync route)
910911
} else if (!(src in this.queue)) { // No cache, no queue
911912
this.queue[src] = [ resolve ]; // First time, create the queue Array
912-
let { callback, filePadding, force } = this.modulo.config.fetchqueue;
913-
if (src.startsWith('file://') || force === 'file') {
914-
const auto = this.modulo.registry.stripWord(filePadding.prefix);
915-
window[callback || auto] = str => { this.__data = str };
913+
const { force, filePadding } = this.modulo.config.fetchqueue;
914+
if (filePadding && (src.startsWith('file:/') || force === 'file')) {
915+
const { prefix, callbackName } = filePadding; // JSONP callback
916+
const auto = this.modulo.registry.utils.stripWord(prefix);
917+
window[callbackName || auto] = str => { this.__data = str };
916918
const elem = window.document.createElement('SCRIPT');
917919
elem.onload = () => this.receiveData(this.__data, src);
918920
elem.src = src + (src.endsWith('/') ? 'index.html' : '');
@@ -931,7 +933,7 @@ modulo.register('core', class FetchQueue {
931933

932934
receiveData(text, src) { // Receive data, optionally trimming padding
933935
const { prefix, suffix } = this.modulo.config.fetchqueue.filePadding;
934-
if (text.startsWith(prefix) && prefix && text.trim().endsWith(suffix)) {
936+
if (text && text.startsWith(prefix) && prefix && text.trim().endsWith(suffix)) {
935937
text = text.trim().slice(prefix.length, 0 - suffix.length); // Clean
936938
}
937939
this.data[src] = text; // Keep retrieved data cached here for sync route

tests/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
<Library -src="./cparts/component_namespace.test.html"></Library>
2626

2727
<!-- Also, do doc tests (makes unmocked, real HTTP requests to APIs) -->
28+
<!--
2829
<Library -src="/libraries/docseg.html"></Library>
2930
<Library -src="/libraries/eg.html"></Library>
31+
-->
3032
</template>
3133
<script src="/js/Modulo.js"></script>
3234
<script src="/demos/mdu/cparts/TestSuite.js"></script>

tests/multi-file-tests/loading_src.test.html

+44-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
-src="/demos/tests/multi-file-tests/loading_src_4.json"
3232
></StaticData>
3333

34-
<TestSuite>
34+
<TestSuite solo>
3535
<Test name="Test StaticData and Template loaded">
3636
<Script name="static data loaded">
3737
const actual = JSON.stringify(staticdata);
@@ -73,6 +73,49 @@
7373
</Script>
7474

7575
</Test>
76+
<Test name="Test Script-tag file protocol loading">
77+
78+
<Script name="Creates script tag with expected attributes">
79+
window.DOCTYPE_MODULO = () => { throw new Error('DOCTYPE_MODULO leaking'); };
80+
81+
modulo.config.fetchqueue.force = 'file'; // force to use file protocol
82+
modulo.register('core', modulo.registry.core.FetchQueue); // Ensure is constructed
83+
const p = modulo.fetchQueue.fetch('/demos/tests/multi-file-tests/loading_src_4.json');
84+
p.then(data => { throw new Error('Should not see this since we disable onload') });
85+
const scriptTag = window.document.querySelector('script[src="/demos/tests/multi-file-tests/loading_src_4.json"]')
86+
scriptTag.onload = () => {} ; // prevent onload
87+
delete modulo.config.fetchqueue.force; // unset just in case
88+
const actual = scriptTag && scriptTag.outerHTML;
89+
scriptTag.remove(); // clean up so it doesnt mess up later tests
90+
const expected = '<script src="/demos/tests/multi-file-tests/loading_src_4.json" ' +
91+
'modulo-asset="y"><' + '/script>';
92+
assert: actual === expected
93+
</Script>
94+
95+
96+
<Script name="Calls receive data when script loads">
97+
window.DOCTYPE_MODULO = () => { throw new Error('DOCTYPE_MODULO leaking'); };
98+
99+
modulo.config.fetchqueue.force = 'file'; // force to use file protocol
100+
modulo.register('core', modulo.registry.core.FetchQueue); // Ensure is constructed
101+
const p = modulo.fetchQueue.fetch('/demos/tests/multi-file-tests/loading_src_4.json');
102+
103+
let cbCount = 0;
104+
p.then(() => { cbCount++; });
105+
delete modulo.config.fetchqueue.force; // unset just in case
106+
107+
//const scriptTag = window.document.querySelector('script[src="/demos/tests/multi-file-tests/loading_src_4.json"]')
108+
const scriptTag = window.document.querySelector('script[src="/demos/tests/multi-file-tests/loading_src_4.json"]')
109+
scriptTag.onload(); // Trigger cbCount
110+
scriptTag.onload = () => {}; // prevent future onload events
111+
112+
// Assign to no-op, to prevent errors (due to how the test
113+
// works, there will be still stray setTimeouts)
114+
window.DOCTYPE_MODULO = () => { };
115+
assert: cbCount === 1
116+
</Script>
117+
118+
</Test>
76119
</TestSuite>
77120
</Component>
78121

0 commit comments

Comments
 (0)