Skip to content

Commit 68f1f37

Browse files
wip(tests): enhance tests
1 parent 9fd8d62 commit 68f1f37

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
lines changed

tests/basic.test.js

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,10 @@ test('should handle custom blocks asynchronously', async () => {
423423
</script>
424424
425425
<foo>bar</foo>
426-
427426
`,
428-
'/optionsOverride.js': `
429427

430-
export default options => {
428+
'/boot.js': `
429+
export default function boot({ options, createApp, mountApp }) {
431430
432431
options.customBlockHandler = async (block, filename, options) => {
433432
@@ -438,6 +437,8 @@ test('should handle custom blocks asynchronously', async () => {
438437
component.bazComponentProperty = 'baz';
439438
}
440439
}
440+
441+
mountApp( createApp(options) );
441442
}
442443
`
443444
}
@@ -449,6 +450,62 @@ test('should handle custom blocks asynchronously', async () => {
449450
});
450451

451452

453+
test('should use cache', async () => {
454+
455+
const { page, output } = await createPage({
456+
files: {
457+
...defaultFiles,
458+
'/component.vue': `
459+
<script>
460+
export default {
461+
mounted() {
462+
console.log('mounted')
463+
}
464+
}
465+
</script>
466+
<foo>bar</foo>
467+
`,
468+
'/boot.js': `
469+
export default function boot({ options, createApp, mountApp, Vue }) {
470+
471+
const myCache = {};
472+
473+
Object.assign(options, {
474+
compiledCache: {
475+
set(key, str) {
476+
477+
console.log('cache.set')
478+
myCache[key] = str;
479+
},
480+
get(key) {
481+
482+
console.log('cache.get')
483+
return myCache[key];
484+
},
485+
}
486+
});
487+
488+
489+
mountApp(createApp(options), 'elt1');
490+
491+
options.moduleCache = {
492+
vue: Vue
493+
};
494+
495+
mountApp(createApp(options), 'elt2');
496+
}
497+
`
498+
}
499+
});
500+
501+
502+
expect(output.filter(e => e.type === 'log').map(e => e.content).flat().join(',')).toBe('cache.get,cache.set,mounted,cache.get,mounted');
503+
504+
await page.close();
505+
});
506+
507+
508+
452509
/*
453510
xtest('should not hang on cycles', async () => {
454511

tests/testsTools.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,23 @@ afterAll(async () => {
100100
const defaultFiles = {
101101
'/vue3-sfc-loader.js': Fs.readFileSync(Path.join(__dirname, '../dist/vue3-sfc-loader.js'), { encoding: 'utf-8' }),
102102
'/vue': Fs.readFileSync(Path.join(__dirname, '../node_modules/vue/dist/vue.global.js'), { encoding: 'utf-8' }),
103-
'/optionsOverride.js': `
104-
export default () => {};
105-
`,
106-
'/appOverride.js': `
107-
export default () => {};
103+
'/boot.js': `
104+
105+
export default function boot({ options, createApp, mountApp }) {
106+
107+
mountApp( createApp(options) );
108+
}
109+
108110
`,
111+
109112
'/index.html': `
110113
<!DOCTYPE html>
111114
<html><body>
112-
<div id="app"></div>
113115
<script src="vue"></script>
114116
<script src="vue3-sfc-loader.js"></script>
115117
<script type="module">
116118
117-
import optionsOverride from '/optionsOverride.js'
118-
import appOverride from '/appOverride.js'
119+
import boot from '/boot.js'
119120
120121
class HttpError extends Error {
121122
@@ -164,14 +165,28 @@ const defaultFiles = {
164165
}
165166
}
166167
167-
optionsOverride(options);
168-
169168
const { loadModule } = window['vue3-sfc-loader'];
170-
const app = Vue.createApp(Vue.defineAsyncComponent( () => loadModule('./component.vue', options) ));
171169
172-
appOverride(app);
173170
174-
app.mount('#app');
171+
function createApp(options) {
172+
173+
return Vue.createApp(Vue.defineAsyncComponent( () => loadModule('./component.vue', options) ));
174+
}
175+
176+
function mountApp(app, eltId = 'app') {
177+
178+
if ( !document.getElementById(eltId) ) {
179+
180+
const parent = document.body;
181+
const elt = document.createElement('div');
182+
elt.id = eltId;
183+
parent.insertBefore(elt, parent.firstChild);
184+
}
185+
186+
return app.mount('#' + eltId);
187+
}
188+
189+
boot({ options, createApp, mountApp, Vue });
175190
176191
//window._done && window._done();
177192

0 commit comments

Comments
 (0)