Skip to content

Commit 399d7ec

Browse files
committed
test: Updating test suite
1 parent 20fdf67 commit 399d7ec

File tree

11 files changed

+96
-35
lines changed

11 files changed

+96
-35
lines changed

packages/cli/tests/build.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { join } = require('path');
1+
const { join, relative } = require('path');
22
const { access, mkdir, readdir, readFile, rename, writeFile } =
33
require('fs').promises;
44
const looksLike = require('html-looks-like');
55
const { create, build, buildFast } = require('./lib/cli');
6-
const { snapshot } = require('./lib/utils');
6+
const { expand, snapshot } = require('./lib/utils');
77
const { subject } = require('./lib/output');
88
const images = require('./images/build');
99
const minimatch = require('minimatch');
@@ -77,6 +77,26 @@ describe('preact build', () => {
7777
await expect(buildFast(dir)).resolves.not.toThrow();
7878
});
7979

80+
it('lazy loads routes with preact-iso `lazy`', async () => {
81+
let dir = await subject('lazy-load-iso');
82+
await buildFast(dir);
83+
84+
let output = await expand(join(dir, 'build')).then(arr => {
85+
return arr.map(x => relative(dir, x));
86+
});
87+
88+
let expected = [
89+
/build\/a\.chunk\.\w{5}\.js$/,
90+
/build\/a\.chunk\.\w{5}\.css$/,
91+
/build\/b\.chunk\.\w{5}\.js$/,
92+
/build\/b\.chunk\.\w{5}\.css$/,
93+
];
94+
95+
expected.forEach(pattern => {
96+
expect(output.find(file => pattern.test(file))).not.toBeUndefined();
97+
});
98+
});
99+
80100
it('should patch global location object', async () => {
81101
let dir = await subject('location-patch');
82102

packages/cli/tests/images/build.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,40 @@ exports.default = {
99

1010
'ssr-build/ssr-bundle.css': 1281,
1111
'ssr-build/ssr-bundle.css.map': 2070,
12-
'ssr-build/ssr-bundle.js': 10863,
13-
'ssr-build/ssr-bundle.js.map': 49128,
12+
'ssr-build/ssr-bundle.js': 8729,
13+
'ssr-build/ssr-bundle.js.map': 42913,
1414
'ssr-build/asset-manifest.json': 76,
1515

16-
'bundle.79d07.js': 21560,
17-
'bundle.79d07.js.map': 85822,
18-
'bundle.79d07.legacy.js': 22549,
19-
'bundle.79d07.legacy.js.map': 106841,
16+
'bundle.65625.js': 21088,
17+
'bundle.65625.js.map': 86032,
18+
'bundle.65625.legacy.js': 22434,
19+
'bundle.65625.legacy.js.map': 108309,
2020
'bundle.354c3.css': 945,
2121
'bundle.354c3.css.map': 1758,
2222

23-
'dom-polyfills.8a933.legacy.js': 5221,
24-
'dom-polyfills.8a933.legacy.js.map': 18676,
25-
'es-polyfills.legacy.js': 42690,
23+
'dom-polyfills.8a933.legacy.js': 5252,
24+
'dom-polyfills.8a933.legacy.js.map': 18836,
25+
'es-polyfills.legacy.js': 61323,
2626

2727
'favicon.ico': 15086,
28-
'index.html': 2264,
28+
'index.html': 2137,
2929
'manifest.json': 455,
3030
'preact_prerender_data.json': 11,
3131
'asset-manifest.json': 943,
3232

33-
'route-home.chunk.dd4a3.js': 347,
34-
'route-home.chunk.dd4a3.js.map': 1848,
35-
'route-home.chunk.dd4a3.legacy.js': 388,
36-
'route-home.chunk.dd4a3.legacy.js.map': 2136,
37-
'route-home.chunk.6eaee.css': 112,
38-
'route-home.chunk.6eaee.css.map': 224,
39-
40-
'route-profile.chunk.0d30e.js': 3198,
41-
'route-profile.chunk.0d30e.js.map': 12743,
42-
'route-profile.chunk.0d30e.legacy.js': 3335,
43-
'route-profile.chunk.0d30e.legacy.js.map': 16137,
44-
'route-profile.chunk.0af3e.css': 118,
45-
'route-profile.chunk.0af3e.css.map': 231,
33+
'routes-home.chunk.55be1.js': 348,
34+
'routes-home.chunk.55be1.js.map': 1703,
35+
'routes-home.chunk.55be1.legacy.js': 394,
36+
'routes-home.chunk.55be1.legacy.js.map': 1991,
37+
'routes-home.chunk.4c05c.css': 112,
38+
'routes-home.chunk.4c05c.css.map': 224,
39+
40+
'routes-profile.chunk.e20b5.js': 743,
41+
'routes-profile.chunk.e20b5.js.map': 3272,
42+
'routes-profile.chunk.e20b5.legacy.js': 910,
43+
'routes-profile.chunk.e20b5.legacy.js.map': 3931,
44+
'routes-profile.chunk.0a57b.css': 118,
45+
'routes-profile.chunk.0a57b.css.map': 231,
4646
};
4747

4848
exports.prerender = {};

packages/cli/tests/lib/cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const build = (exports.build = async function (cwd, options) {
2828

2929
await mkdir(join(cwd, 'node_modules'), { recursive: true }); // ensure exists, avoid exit()
3030
await linkPackage('preact', cwd);
31+
await linkPackage('preact-iso', cwd);
3132
await linkPackage('preact-render-to-string', cwd);
3233

3334
let opts = Object.assign({ cwd }, argv, options);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './a.css';
2+
3+
export default () => <h1>Lazy Load A</h1>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: blue;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './b.css';
2+
3+
export default () => <h1>Lazy Load B</h1>;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { LocationProvider, Router } from 'preact-iso/router';
2+
import { default as lazy, ErrorBoundary } from 'preact-iso/lazy';
3+
4+
// Asynchronous, code-splitted:
5+
const A = lazy(() => import('./a.js'));
6+
const B = lazy(() => import('./b.js'));
7+
8+
export default function App() {
9+
return (
10+
<LocationProvider>
11+
<ErrorBoundary>
12+
<Router>
13+
<A path="/" />
14+
<B path="/b" />
15+
</Router>
16+
</ErrorBoundary>
17+
</LocationProvider>
18+
);
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"private": true,
3+
"name": "preact-lazy-load-iso"
4+
}

packages/cli/tests/subjects/location-patch/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
export default () => {
2+
// Uses WHATWG URL API
3+
// https://nodejs.org/api/url.html#the-whatwg-url-api
24
if (
3-
location.protocol === undefined ||
4-
location.slashes === undefined ||
5-
location.auth === undefined ||
5+
location.hash === undefined ||
66
location.host === undefined ||
7-
location.port === undefined ||
87
location.hostname === undefined ||
9-
location.hash === undefined ||
10-
location.query === undefined ||
8+
location.href === undefined ||
9+
location.origin === undefined ||
10+
location.password === undefined ||
1111
location.pathname === undefined ||
12-
location.path === undefined ||
13-
location.href === undefined
12+
location.port === undefined ||
13+
location.protocol === undefined ||
14+
location.search === undefined ||
15+
location.searchParams === undefined ||
16+
location.username === undefined
1417
) {
1518
throw new Error('Incomplete Location object');
1619
}

0 commit comments

Comments
 (0)