Skip to content

Commit

Permalink
chore: refactor test suites (#36)
Browse files Browse the repository at this point in the history
* test: Switch to uvu for Node/non-browser tests

* test: Switch to wtr for browser tests

* test: Remove leftover test file

* chore: sort deps
  • Loading branch information
rschristian authored Aug 23, 2024
1 parent fa83cd8 commit 5309eb8
Show file tree
Hide file tree
Showing 10 changed files with 2,814 additions and 2,439 deletions.
5 changes: 4 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"moduleResolution": "NodeNext",
"noEmit": true,
"allowJs": true,
"checkJs": true
"checkJs": true,
"jsx": "react",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
}
}
26 changes: 12 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,25 @@
"README.md"
],
"scripts": {
"test": "node --experimental-vm-modules node_modules/.bin/jest"
},
"jest": {
"testEnvironment": "jsdom",
"testEnvironmentOptions": {
"customExportConditions": [
"node",
"node-addons"
]
}
"test": "yarn test:node && yarn test:browser",
"test:browser": "wtr test/*.test.js",
"test:node": "uvu test/node"
},
"peerDependencies": {
"preact": ">=10",
"preact-render-to-string": ">=6.4.0"
},
"devDependencies": {
"@types/mocha": "^10.0.7",
"@types/sinon-chai": "^3.2.12",
"@web/dev-server-esbuild": "^1.0.2",
"@web/test-runner": "^0.18.3",
"chai": "^5.1.1",
"htm": "^3.1.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jsdom": "^22.1.0",
"preact": "10.15.1",
"preact-render-to-string": "^6.4.0"
"preact-render-to-string": "^6.4.0",
"sinon": "^18.0.0",
"sinon-chai": "^4.0.0",
"uvu": "^0.5.6"
}
}
36 changes: 0 additions & 36 deletions test/location-stub.test.js

This file was deleted.

107 changes: 0 additions & 107 deletions test/match.test.js

This file was deleted.

38 changes: 38 additions & 0 deletions test/node/location-stub.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';

import { locationStub } from '../../src/prerender.js';

test.before.each(() => {
if (globalThis.location) {
delete globalThis.location;
}
});

test('Contains all Location instance properties', () => {
locationStub('/foo/bar?baz=qux#quux');

[
// 'ancestorOrigins', // Not supported by FireFox and sees little use, but we could add an empty val if it's needed
'hash',
'host',
'hostname',
'href',
'origin',
'pathname',
'port',
'protocol',
'search',
].forEach(key => {
assert.ok(Object.hasOwn(globalThis.location, key), `missing: ${key}`);
});
});

// Do we need to support `assign`, `reload`, and/or `replace`?
test('Support bound methods', () => {
locationStub('/foo/bar?baz=qux#quux');

assert.equal(globalThis.location.toString(), 'http://localhost/foo/bar?baz=qux#quux')
});

test.run();
29 changes: 29 additions & 0 deletions test/node/prerender.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { html } from 'htm/preact';

import { default as prerender } from '../../src/prerender.js';

test('extracts links', async () => {
const App = () => html`
<div>
<a href="/foo">foo</a>
<a href="/bar" target="_blank">bar</a>
<a href="/baz" target="_self">baz</a>
</div>
`;


const { links } = await prerender(html`<${App} />`);
assert.equal(links.size, 2, `incorrect number of links found: ${links.size}`);
assert.ok(links.has('/foo'), `missing: /foo`);
assert.ok(links.has('/baz'), `missing: /baz`);
});

test('appends iso data script', async () => {
const { html: h } = await prerender(html`<div />`);
// Empty for now, but used for hydration vs render detection
assert.match(h, /<script type="isodata"><\/script>/, 'missing iso data script tag');
});

test.run();
110 changes: 110 additions & 0 deletions test/node/router-match.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';

// @ts-expect-error - no types, not meant for public use
import { exec } from '../../src/router.js';

function execPath(path, pattern, opts) {
return exec(path, pattern, { path, query: {}, params: {}, ...(opts || {}) });
}

test('Base route', () => {
const accurateResult = execPath('/', '/');
assert.equal(accurateResult, { path: '/', params: {}, query: {} });

const inaccurateResult = execPath('/user/1', '/');
assert.equal(inaccurateResult, undefined);
});

test('Param route', () => {
const accurateResult = execPath('/user/2', '/user/:id');
assert.equal(accurateResult, { path: '/user/2', params: { id: '2' }, id: '2', query: {} });

const inaccurateResult = execPath('/', '/user/:id');
assert.equal(inaccurateResult, undefined);
});

test('Param rest segment', () => {
const accurateResult = execPath('/user/foo', '/user/*');
assert.equal(accurateResult, { path: '/user/foo', params: {}, query: {}, rest: '/foo' });

const inaccurateResult = execPath('/', '/user/:id/*');
assert.equal(inaccurateResult, undefined);
});

test('Param route with rest segment', () => {
const accurateResult = execPath('/user/2/foo', '/user/:id/*');
assert.equal(accurateResult, { path: '/user/2/foo', params: { id: '2' }, id: '2', query: {}, rest: '/foo' });

const accurateResult2 = execPath('/user/2/foo/bar/bob', '/user/:id/*');
assert.equal(accurateResult2, {
path: '/user/2/foo/bar/bob',
params: { id: '2' },
id: '2',
query: {},
rest: '/foo/bar/bob'
});

const inaccurateResult = execPath('/', '/user/:id/*');
assert.equal(inaccurateResult, undefined);
});

test('Optional param route', () => {
const accurateResult = execPath('/user', '/user/:id?');
assert.equal(accurateResult, { path: '/user', params: { id: undefined }, id: undefined, query: {} });

const inaccurateResult = execPath('/', '/user/:id?');
assert.equal(inaccurateResult, undefined);
});

test('Optional rest param route "/:x*"', () => {
const accurateResult = execPath('/user', '/user/:id?');
assert.equal(accurateResult, { path: '/user', params: { id: undefined }, id: undefined, query: {} });

const inaccurateResult = execPath('/', '/user/:id?');
assert.equal(inaccurateResult, undefined);
});

test('Rest param route "/:x+"', () => {
const matchedResult = execPath('/user/foo', '/user/:id+');
assert.equal(matchedResult, { path: '/user/foo', params: { id: 'foo' }, id: 'foo', query: {} });

const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id+');
assert.equal(matchedResultWithSlash, {
path: '/user/foo/bar',
params: { id: 'foo/bar' },
id: 'foo/bar',
query: {}
});

const emptyResult = execPath('/user', '/user/:id+');
assert.equal(emptyResult, undefined);

const mismatchedResult = execPath('/', '/user/:id+');
assert.equal(mismatchedResult, undefined);
});

test('Handles leading/trailing slashes', () => {
const result = execPath('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/');
assert.equal(result, {
path: '/about-late/_SEGMENT1_/_SEGMENT2_/',
params: {
seg1: '_SEGMENT1_',
seg2: '_SEGMENT2_'
},
seg1: '_SEGMENT1_',
seg2: '_SEGMENT2_',
query: {}
});
});

test('should not overwrite existing properties', () => {
const result = execPath('/foo/bar', '/:path/:query', { path: '/custom-path' });
assert.equal(result, {
params: { path: 'foo', query: 'bar' },
path: '/custom-path',
query: {}
});
});

test.run();
Loading

0 comments on commit 5309eb8

Please sign in to comment.