A few simple tests

This commit is contained in:
Tony Garnock-Jones 2024-06-04 11:48:40 +02:00
parent d9fe78edf7
commit 44fe1ff7b5
5 changed files with 109 additions and 1 deletions

View File

@ -0,0 +1,7 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
export default {
preset: 'ts-jest',
testEnvironment: './patched-jsdom.mjs',
};

View File

@ -22,7 +22,9 @@
"rollup": "rollup -c",
"rollup:watch": "rollup -c -w",
"clean": "rm -rf lib/ dist/ index.js index.js.map",
"veryclean": "yarn run clean && rm -rf node_modules"
"veryclean": "yarn run clean && rm -rf node_modules",
"test": "../../node_modules/.bin/jest",
"test:watch": "yarn test --watch"
},
"dependencies": {
"@syndicate-lang/core": "^0.34.8"

View File

@ -0,0 +1,14 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2024 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
// https://github.com/jsdom/jsdom/issues/2524
import { TextEncoder, TextDecoder } from 'util';
import $JSDOMEnvironment from 'jest-environment-jsdom';
export default class JSDOMEnvironment extends $JSDOMEnvironment {
constructor(... args) {
const { global } = super(... args);
if (!global.TextEncoder) global.TextEncoder = TextEncoder;
if (!global.TextDecoder) global.TextDecoder = TextDecoder;
}
}

View File

@ -0,0 +1,47 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2024 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { template } from '../src/html';
import './test-utils';
describe('basic templating', () => {
it('should produce a node', () => {
const x = document.createElement('x');
x.appendChild(document.createTextNode('y'));
expect(template()`<x>y</x>`).toEqual([x]);
});
it('should substitute a string', () => {
const x = document.createElement('x');
const y = 'abc';
x.appendChild(document.createTextNode('abc'));
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
});
it('should substitute a node', () => {
const x = document.createElement('x');
const z = document.createElement('z');
z.appendChild(document.createTextNode('q'));
x.appendChild(z);
const y = template()`<z>q</z>`;
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
});
it('should substitute an array of strings', () => {
const x = document.createElement('x');
const y = ['abc', 'def'];
x.appendChild(document.createTextNode('abcdef'));
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
});
it('should substitute an array of strings and nodes', () => {
const x = document.createElement('x');
const y = ['abc', template()`<z>q</z>`, 'def'];
const z = document.createElement('z');
z.appendChild(document.createTextNode('q'));
x.appendChild(document.createTextNode('abc'));
x.appendChild(z);
x.appendChild(document.createTextNode('def'));
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
});
});

View File

@ -0,0 +1,38 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { is, preserves } from '@preserves/core';
declare global {
namespace jest {
interface Matchers<R> {
is(expected: any): R;
toThrowFilter(f: (e: Error) => boolean): R;
}
}
}
expect.extend({
is(actual, expected) {
return is(actual, expected)
? { message: () => preserves`expected ${actual} not to be Preserves.is to ${expected}`,
pass: true }
: { message: () => preserves`expected ${actual} to be Preserves.is to ${expected}`,
pass: false };
},
toThrowFilter(thunk, f) {
try {
thunk();
return { message: () => preserves`expected an exception`, pass: false };
} catch (e) {
if (f(e)) {
return { message: () => preserves`expected an exception not matching the filter`,
pass: true };
} else {
return { message: () => preserves`expected an exception matching the filter: ${(e as any)?.constructor?.name}`,
pass: false };
}
}
}
});