forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (75 loc) · 2.31 KB
/
index.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
import assert from 'assert';
import path from 'path';
import fs from 'fs';
import {
showOutput,
loadConfig,
loadSvelte,
env,
setupHtmlEqual
} from '../helpers.js';
let compileOptions = null;
function getName(filename) {
const base = path.basename(filename).replace('.html', '');
return base[0].toUpperCase() + base.slice(1);
}
describe('hydration', () => {
before(() => {
const svelte = loadSvelte();
require.extensions['.html'] = function(module, filename) {
const options = Object.assign(
{ filename, name: getName(filename), hydratable: true, format: 'cjs' },
compileOptions
);
const { js } = svelte.compile(fs.readFileSync(filename, 'utf-8'), options);
return module._compile(js.code, filename);
};
return setupHtmlEqual();
});
function runTest(dir) {
if (dir[0] === '.') return;
const config = loadConfig(`./hydration/samples/${dir}/_config.js`);
if (config.solo && process.env.CI) {
throw new Error('Forgot to remove `solo: true` from test');
}
(config.skip ? it.skip : config.solo ? it.only : it)(dir, () => {
const cwd = path.resolve(`test/hydration/samples/${dir}`);
compileOptions = config.compileOptions || {};
compileOptions.shared = path.resolve('shared.js');
compileOptions.dev = config.dev;
compileOptions.hydrate = true;
const window = env();
try {
global.window = window;
let SvelteComponent;
try {
SvelteComponent = require(`${cwd}/main.html`);
} catch (err) {
throw err;
}
const target = window.document.body;
target.innerHTML = fs.readFileSync(`${cwd}/_before.html`, 'utf-8');
const snapshot = config.snapshot ? config.snapshot(target) : {};
const component = new SvelteComponent({
target,
hydrate: true,
data: config.data
});
assert.htmlEqual(target.innerHTML, fs.readFileSync(`${cwd}/_after.html`, 'utf-8'));
if (config.test) {
config.test(assert, target, snapshot, component, window);
} else {
component.destroy();
assert.equal(target.innerHTML, '');
}
} catch (err) {
showOutput(cwd, { shared: 'svelte/shared.js' }); // eslint-disable-line no-console
throw err;
}
if (config.show) showOutput(cwd, { shared: 'svelte/shared.js' });
});
}
fs.readdirSync('test/hydration/samples').forEach(dir => {
runTest(dir, null);
});
});