Skip to content

Commit

Permalink
test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Casheeew committed Jan 6, 2024
1 parent 60276d4 commit ea1a287
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 184 deletions.
2 changes: 1 addition & 1 deletion test/data/html/dom-text-scanner.html
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,4 @@ <h1>DOMTextScanner Tests</h1>
</test-case>

</body>
</html>
</html>
407 changes: 263 additions & 144 deletions test/document-util.test.js

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions test/dom-text-scanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import {fileURLToPath} from 'node:url';
import path from 'path';
import {describe, expect} from 'vitest';
import {afterAll, describe, expect, test} from 'vitest';
import {parseJson} from '../dev/json.js';
import {DOMTextScanner} from '../ext/js/dom/dom-text-scanner.js';
import {createDomTest} from './fixtures/dom-test.js';
import {setupDomTest} from './fixtures/dom-test.js';

const dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -101,11 +101,13 @@ function createAbsoluteGetComputedStyle(window) {
};
}


const test = createDomTest(path.join(dirname, 'data/html/dom-text-scanner.html'));
const domTestEnv = await setupDomTest(path.join(dirname, 'data/html/dom-text-scanner.html'));

describe('DOMTextScanner', () => {
test('Seek tests', ({window}) => {
const {window, teardown} = domTestEnv;
afterAll(() => teardown(global));

test('Seek tests', () => {
const {document} = window;
window.getComputedStyle = createAbsoluteGetComputedStyle(window);

Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ function prepareWindow(window) {
document.caretRangeFromPoint = () => null;
}

/**
*
* @param {string} [htmlFilePath]
* @returns {Promise<{window: import('jsdom').DOMWindow; teardown: (global: unknown) => import('vitest').Awaitable<void>}>}
*/
export async function setupDomTest(htmlFilePath) {
const html = typeof htmlFilePath === 'string' ? fs.readFileSync(htmlFilePath, {encoding: 'utf8'}) : '<!DOCTYPE html>';
const env = builtinEnvironments.jsdom;
const {teardown} = await env.setup(global, {jsdom: {html}});
const window = /** @type {import('jsdom').DOMWindow} */ (/** @type {unknown} */ (global.window));
prepareWindow(window);
return {window, teardown};
}

/**
* @param {string} [htmlFilePath]
* @returns {import('vitest').TestAPI<{window: import('jsdom').DOMWindow}>}
Expand Down
27 changes: 13 additions & 14 deletions test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import fs from 'fs';
import {fileURLToPath} from 'node:url';
import path from 'path';
import {expect, test, vi} from 'vitest';
import {expect, test, describe, vi} from 'vitest';
import {OptionsUtil} from '../ext/js/data/options-util.js';
import {TemplatePatcher} from '../ext/js/templates/template-patcher.js';
import {chrome, fetch} from './mocks/common.js';
Expand Down Expand Up @@ -627,7 +627,7 @@ async function testUpdate() {

/** */
async function testDefault() {
test('Default', async () => {
describe('Default', () => {
/** @type {((options: import('options-util').IntermediateOptions) => void)[]} */
const data = [
(options) => options,
Expand All @@ -639,27 +639,22 @@ async function testDefault() {
}
];

const optionsUtil = new OptionsUtil();
await optionsUtil.prepare();
test.each(data)('default-test-%#', async (modify) => {
const optionsUtil = new OptionsUtil();
await optionsUtil.prepare();

for (const modify of data) {
const options = optionsUtil.getDefault();

const optionsModified = structuredClone(options);
modify(optionsModified);

const optionsUpdated = await optionsUtil.update(structuredClone(optionsModified));
expect(structuredClone(optionsUpdated)).toStrictEqual(structuredClone(options));
}
});
});
}

/** */
async function testFieldTemplatesUpdate() {
test('FieldTemplatesUpdate', async () => {
const optionsUtil = new OptionsUtil();
await optionsUtil.prepare();

describe('FieldTemplatesUpdate', () => {
const templatePatcher = new TemplatePatcher();
/**
* @param {string} fileName
Expand Down Expand Up @@ -1576,7 +1571,11 @@ async function testFieldTemplatesUpdate() {
];

const updatesPattern = /<<<UPDATE-ADDITIONS>>>/g;
for (const {old, expected, oldVersion, newVersion} of data) {

test.each(data)('field-templates-update-test-%#', async ({old, expected, oldVersion, newVersion}) => {
const optionsUtil = new OptionsUtil();
await optionsUtil.prepare();

const options = /** @type {import('core').SafeAny} */ (createOptionsTestData1());
options.profiles[0].options.anki.fieldTemplates = old;
options.version = oldVersion;
Expand All @@ -1586,7 +1585,7 @@ async function testFieldTemplatesUpdate() {
const optionsUpdated = structuredClone(await optionsUtil.update(options, newVersion));
const fieldTemplatesActual = optionsUpdated.profiles[0].options.anki.fieldTemplates;
expect(fieldTemplatesActual).toStrictEqual(expected2);
}
});
});
}

Expand Down
14 changes: 7 additions & 7 deletions test/profile-conditions-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

/* eslint-disable no-multi-spaces */

import {expect, test} from 'vitest';
import {describe, expect, test} from 'vitest';
import {ProfileConditionsUtil} from '../ext/js/background/profile-conditions-util.js';

/** */
function testNormalizeContext() {
test('NormalizeContext', () => {
describe('NormalizeContext', () => {
/** @type {{context: import('settings').OptionsContext, expected: import('profile-conditions-util').NormalizedOptionsContext}[]} */
const data = [
// Empty
Expand Down Expand Up @@ -51,17 +51,17 @@ function testNormalizeContext() {
}
];

for (const {context, expected} of data) {
test.each(data)('normalize-context-test-%#', ({context, expected}) => {
const profileConditionsUtil = new ProfileConditionsUtil();
const actual = profileConditionsUtil.normalizeContext(context);
expect(actual).toStrictEqual(expected);
}
});
});
}

/** */
function testSchemas() {
test('Schemas', () => {
describe('Schemas', () => {
/** @type {{conditionGroups: import('settings').ProfileConditionGroup[], expectedSchema?: import('ext/json-schema').Schema, inputs?: {expected: boolean, context: import('settings').OptionsContext}[]}[]} */
const data = [
// Empty
Expand Down Expand Up @@ -1100,7 +1100,7 @@ function testSchemas() {
}
];

for (const {conditionGroups, expectedSchema, inputs} of data) {
test.each(data)('schemas-test-%#', ({conditionGroups, expectedSchema, inputs}) => {
const profileConditionsUtil = new ProfileConditionsUtil();
const schema = profileConditionsUtil.createSchema(conditionGroups);
if (typeof expectedSchema !== 'undefined') {
Expand All @@ -1113,7 +1113,7 @@ function testSchemas() {
expect(actual).toStrictEqual(expected);
}
}
}
});
});
}

Expand Down
26 changes: 13 additions & 13 deletions test/text-source-map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {expect, test} from 'vitest';
import {describe, expect, test} from 'vitest';
import {TextSourceMap} from '../ext/js/general/text-source-map.js';

/** */
function testSource() {
test('Source', () => {
describe('Source', () => {
const data = [
['source1'],
['source2'],
['source3']
];

for (const [source] of data) {
test.each(data)('source-test-%#', (source) => {
const sourceMap = new TextSourceMap(source);
expect(source).toStrictEqual(sourceMap.source);
}
});
});
}

/** */
function testEquals() {
test('Equals', () => {
describe('Equals', () => {
/** @type {[args1: [source1: string, mapping1: ?(number[])], args2: [source2: string, mapping2: ?(number[])], expectedEquals: boolean][]} */
const data = [
[['source1', null], ['source1', null], true],
Expand Down Expand Up @@ -69,19 +69,19 @@ function testEquals() {
[['source3', [1, 1, 1, 1, 1, 1, 1]], ['source6', [1, 1, 1, 1, 1, 1, 1]], false]
];

for (const [[source1, mapping1], [source2, mapping2], expectedEquals] of data) {
test.each(data)('equals-test-%#', ([source1, mapping1], [source2, mapping2], expectedEquals) => {
const sourceMap1 = new TextSourceMap(source1, mapping1);
const sourceMap2 = new TextSourceMap(source2, mapping2);
expect(sourceMap1.equals(sourceMap1)).toBe(true);
expect(sourceMap2.equals(sourceMap2)).toBe(true);
expect(sourceMap1.equals(sourceMap2)).toStrictEqual(expectedEquals);
}
});
});
}

/** */
function testGetSourceLength() {
test('GetSourceLength', () => {
describe('GetSourceLength', () => {
/** @type {[args: [source: string, mapping: number[]], finalLength: number, expectedValue: number][]} */
const data = [
[['source', [1, 1, 1, 1, 1, 1]], 1, 1],
Expand All @@ -101,16 +101,16 @@ function testGetSourceLength() {
[['source', [6, 6]], 1, 6]
];

for (const [[source, mapping], finalLength, expectedValue] of data) {
test.each(data)('get-source-length-test-%#', ([source, mapping], finalLength, expectedValue) => {
const sourceMap = new TextSourceMap(source, mapping);
expect(sourceMap.getSourceLength(finalLength)).toStrictEqual(expectedValue);
}
});
});
}

/** */
function testCombineInsert() {
test('CombineInsert', () => {
describe('CombineInsert', () => {
/** @type {[args: [source: string, mapping: ?(number[])], expectedArgs: [expectedSource: string, expectedMapping: ?(number[])], operations: [operation: string, arg1: number, arg2: number][]][]} */
const data = [
// No operations
Expand Down Expand Up @@ -214,7 +214,7 @@ function testCombineInsert() {
]
];

for (const [[source, mapping], [expectedSource, expectedMapping], operations] of data) {
test.each(data)('combine-insert-test-%#', ([source, mapping], [expectedSource, expectedMapping], operations) => {
const sourceMap = new TextSourceMap(source, mapping);
const expectedSourceMap = new TextSourceMap(expectedSource, expectedMapping);
for (const [operation, ...args] of operations) {
Expand All @@ -228,7 +228,7 @@ function testCombineInsert() {
}
}
expect(sourceMap.equals(expectedSourceMap)).toBe(true);
}
});
});
}

Expand Down

0 comments on commit ea1a287

Please sign in to comment.