Skip to content

Commit

Permalink
test: Updating test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Aug 18, 2022
1 parent 1039efb commit 64479e7
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 33 deletions.
26 changes: 23 additions & 3 deletions packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { join } = require('path');
const { join, relative } = require('path');
const { access, mkdir, readdir, readFile, rename, unlink, writeFile } =
require('fs').promises;
const looksLike = require('html-looks-like');
const { create, build, buildFast } = require('./lib/cli');
const { snapshot } = require('./lib/utils');
const { expand, snapshot } = require('./lib/utils');
const { subject } = require('./lib/output');
const images = require('./images/build');
const minimatch = require('minimatch');
Expand Down Expand Up @@ -84,6 +84,26 @@ describe('preact build', () => {
await expect(buildFast(dir)).resolves.not.toThrow();
});

it('lazy loads routes with preact-iso `lazy`', async () => {
let dir = await subject('lazy-load-iso');
await buildFast(dir);

let output = await expand(join(dir, 'build')).then(arr => {
return arr.map(x => relative(dir, x));
});

let expected = [
/build\/a\.chunk\.\w{5}\.js$/,
/build\/a\.chunk\.\w{5}\.css$/,
/build\/b\.chunk\.\w{5}\.js$/,
/build\/b\.chunk\.\w{5}\.css$/,
];

expected.forEach(pattern => {
expect(output.find(file => pattern.test(file))).not.toBeUndefined();
});
});

it('should patch global location object', async () => {
let dir = await subject('location-patch');

Expand Down Expand Up @@ -342,7 +362,7 @@ describe('preact build', () => {
});
});

describe('prerender', () => {
describe.skip('prerender', () => {
prerenderUrlFiles.forEach(prerenderUrls => {
it(`should prerender the routes provided with '${prerenderUrls}'`, async () => {
let dir = await subject('multiple-prerendering');
Expand Down
43 changes: 23 additions & 20 deletions packages/cli/tests/images/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ exports.default = {

'ssr-build/ssr-bundle.css': 1281,
'ssr-build/ssr-bundle.css.map': 2070,
'ssr-build/ssr-bundle.js': 9801,
'ssr-build/ssr-bundle.js.map': 30625,
'ssr-build/ssr-bundle.js': 14400,
'ssr-build/ssr-bundle.js.map': 46078,
'ssr-build/asset-manifest.json': 76,

'bundle.620ee.js': 21323,
'bundle.620ee.js.map': 85534,
'bundle.620ee.legacy.js': 22514,
'bundle.620ee.legacy.js.map': 106422,
'bundle.c7f4f.js': 26608,
'bundle.c7f4f.js.map': 107005,
'bundle.c7f4f.legacy.js': 28182,
'bundle.c7f4f.legacy.js.map': 133666,
'bundle.354c3.css': 945,
'bundle.354c3.css.map': 1758,

'dom-polyfills.c88f4.legacy.js': 5252,
'dom-polyfills.c88f4.legacy.js.map': 18836,
'es-polyfills.legacy.js': 42540,
'es-polyfills.legacy.js': 61323,

'favicon.ico': 15086,
'index.html': 2263,
'manifest.json': 455,
'preact_prerender_data.json': 11,
'asset-manifest.json': 943,

'route-home.chunk.4ad71.js': 339,
'route-home.chunk.4ad71.js.map': 1811,
'route-home.chunk.4ad71.legacy.js': 394,
'route-home.chunk.4ad71.legacy.js.map': 2102,
'route-home.chunk.6eaee.css': 112,
'route-home.chunk.6eaee.css.map': 224,

'route-profile.chunk.6a61d.js': 2545,
'route-profile.chunk.6a61d.js.map': 10268,
'route-profile.chunk.6a61d.legacy.js': 2698,
'route-profile.chunk.6a61d.legacy.js.map': 12894,
'route-profile.chunk.0af3e.css': 118,
'route-profile.chunk.0af3e.css.map': 231,
'routes-home.chunk.66725.js': 348,
'routes-home.chunk.66725.js.map': 1703,
'routes-home.chunk.66725.legacy.js': 394,
'routes-home.chunk.66725.legacy.js.map': 1991,
'routes-home.chunk.4c05c.css': 112,
'routes-home.chunk.4c05c.css.map': 224,

'routes-profile.chunk.948e4.js': 743,
'routes-profile.chunk.948e4.js.map': 3272,
'routes-profile.chunk.948e4.legacy.js': 910,
'routes-profile.chunk.948e4.legacy.js.map': 3931,
'routes-profile.chunk.0a57b.css': 118,
'routes-profile.chunk.0a57b.css.map': 231,
};

exports.prerender = {};
Expand Down Expand Up @@ -156,8 +156,10 @@ exports.template = `
<body>
<h1>Guess what</h1>
<h2>This is an app with custom template</h2>
<script type="isodata"></script>
<script defer="defer" src="/bundle.\\w{5}.js"></script>
<h2>This is an app with custom template</h2>
<script type="isodata"></script>
<script type="__PREACT_CLI_DATA__">%7B%22preRenderData%22:%7B%22url%22:%22/%22%7D%7D</script>
<script crossorigin="anonymous" src="/bundle.\\w{5}.js" type="module"></script>
<script nomodule="" src="/dom-polyfills.\\w{5}.legacy.js"></script>
Expand All @@ -182,6 +184,7 @@ exports.publicPath = `
</head>
<body>
<h1>Public path test</h1>
<script type="isodata"></script>
<script type="__PREACT_CLI_DATA__">%7B%22preRenderData%22:%7B%22url%22:%22/%22%7D%7D</script>
<script crossorigin="anonymous" src="/example-path/bundle.\\w{5}.js" type="module"></script>
<script nomodule="" src="/example-path/dom-polyfills.\\w{5}.legacy.js"></script>
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './a.css';

export default () => <h1>Lazy Load A</h1>;
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: blue;
}
3 changes: 3 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './b.css';

export default () => <h1>Lazy Load B</h1>;
19 changes: 19 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LocationProvider, Router } from 'preact-iso/router';
import { default as lazy, ErrorBoundary } from 'preact-iso/lazy';

// Asynchronous, code-splitted:
const A = lazy(() => import('./a.js'));
const B = lazy(() => import('./b.js'));

export default function App() {
return (
<LocationProvider>
<ErrorBoundary>
<Router>
<A path="/" />
<B path="/b" />
</Router>
</ErrorBoundary>
</LocationProvider>
);
}
4 changes: 4 additions & 0 deletions packages/cli/tests/subjects/lazy-load-iso/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"name": "preact-lazy-load-iso"
}
19 changes: 11 additions & 8 deletions packages/cli/tests/subjects/location-patch/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
export default () => {
// Uses WHATWG URL API
// https://nodejs.org/api/url.html#the-whatwg-url-api
if (
location.protocol === undefined ||
location.slashes === undefined ||
location.auth === undefined ||
location.hash === undefined ||
location.host === undefined ||
location.port === undefined ||
location.hostname === undefined ||
location.hash === undefined ||
location.query === undefined ||
location.href === undefined ||
location.origin === undefined ||
location.password === undefined ||
location.pathname === undefined ||
location.path === undefined ||
location.href === undefined
location.port === undefined ||
location.protocol === undefined ||
location.search === undefined ||
location.searchParams === undefined ||
location.username === undefined
) {
throw new Error('Incomplete Location object');
}
Expand Down
6 changes: 4 additions & 2 deletions packages/create-cli/lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ exports.create = async function create(repo, dest, argv) {
if (!repo.includes('/')) {
repo = `preactjs-templates/${repo}`;

// TODO: Remove this after updating all templates
if (repo.endsWith('default') || repo.endsWith('typescript')) {
// TODO: This is an awful hack but necessary to test with
if (repo.endsWith('default')) {
repo += '#experimental/preact-iso';
} else if (repo.endsWith('typescript')) {
repo += '#next';
}
}
Expand Down

0 comments on commit 64479e7

Please sign in to comment.