@augment-vir/test
Version:
A universal testing suite that works with Mocha style test runners _and_ Node.js's built-in test runner.
61 lines (60 loc) • 2.17 kB
JavaScript
import { assert, check, waitUntil } from '@augment-vir/assert';
import { itCasesWithContext, } from '../augments/universal-testing-suite/it-cases-with-context.js';
import { renderElement } from './render-element.js';
import { extractElementText } from './symlinked/element-text.js';
export function elementCases(elementDefinition, testCases, options = {}) {
itCasesWithContext(testRenderElement, () => {
/** All assertions are done in the test callback. */
}, testCases.map((testCase) => {
return {
it: testCase.it,
inputs: [
elementDefinition,
testCase,
options,
],
throws: undefined,
};
}));
}
async function testRenderElement(testContext, elementDefinition, testCase, options) {
const eventKeys = Array.from(testCase.expect?.events?.keys() || []);
const events = new Map(eventKeys.map((key) => {
return [
key,
[],
];
}));
const inputs = await testCase.createInputs?.(testContext);
const instance = await renderElement(elementDefinition, inputs);
eventKeys.forEach((eventKey) => {
instance.addEventListener(eventKey.type, (event) => {
events.get(eventKey)?.push(event.detail);
});
});
await testCase.act?.(instance);
await waitUntil.isTrue(() => {
const text = extractElementText(instance);
if (check.isArray(testCase.expect?.text)) {
assert.hasValues(text, testCase.expect.text);
}
else if (check.isString(testCase.expect?.text)) {
assert.hasValue(text, testCase.expect.text);
}
if (testCase.expect?.events) {
testCase.expect.events.forEach((expectedEvents, eventKey) => {
const actualEvents = events.get(eventKey);
assert.isDefined(actualEvents);
assert.deepEquals(actualEvents, expectedEvents);
});
}
return true;
}, {
interval: {
milliseconds: 100,
},
timeout: options.timeout || {
seconds: 10,
},
});
}