diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2024-01-31 08:38:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-31 13:38:30 +0000 |
commit | 87ed7c8affd3ade9d3cd2d9ed1a61dd5f224e473 (patch) | |
tree | be727294e31ef21e8a3f634734610e69e4a155ac /test | |
parent | 3e419aa562aab03ca20421aaf7e4d1a39194a5b4 (diff) |
Module refactoring (#588)
* Convert PronunciationGenerator into static functions
* Convert DictionaryDataUtil into static functions
* Convert AnkiNoteDataCreator into static functions
* Convert MediaUtil into static functions
* Convert RegexUtil into static functions
* Convert StringUtil into static functions
* Convert ArrayBufferUtil into static functions
* Convert AnkiUtil into static functions
* Convert PermissionsUtil into static functions
* Convert ProfileConditionsUtil into static functions
Diffstat (limited to 'test')
-rw-r--r-- | test/dictionary-data.test.js | 9 | ||||
-rw-r--r-- | test/dictionary-data.write.js | 6 | ||||
-rw-r--r-- | test/fixtures/translator-test.js | 21 | ||||
-rw-r--r-- | test/profile-conditions-util.test.js | 10 | ||||
-rw-r--r-- | test/utilities/anki.js | 6 |
5 files changed, 22 insertions, 30 deletions
diff --git a/test/dictionary-data.test.js b/test/dictionary-data.test.js index 438e1e97..9f8ba6f0 100644 --- a/test/dictionary-data.test.js +++ b/test/dictionary-data.test.js @@ -53,7 +53,10 @@ describe('Dictionary data', () => { expected3: expectedResults3[i] })); describe.each(testCases)('Test %#: $data.name', ({data, expected1, expected2, expected3}) => { - test('Test', async ({translator, ankiNoteDataCreator, expect}) => { + test('Test', async ({window, translator, expect}) => { + // The window property needs to be referenced for it to be initialized. + // It is needed for DOM access for structured content. + void window; switch (data.func) { case 'findTerms': { @@ -62,7 +65,7 @@ describe('Dictionary data', () => { const options = createFindOptions(dictionaryName, optionsPresets, data.options); const {dictionaryEntries, originalTextLength} = await translator.findTerms(mode, text, options); const renderResults = mode !== 'simple' ? await getTemplateRenderResults(dictionaryEntries, 'terms', mode, template, expect) : null; - const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mode)) : null; + const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, mode)) : null; expect.soft(originalTextLength).toStrictEqual(expected1.originalTextLength); expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries); expect.soft(noteDataList).toEqual(expected2.noteDataList); @@ -76,7 +79,7 @@ describe('Dictionary data', () => { const options = createFindOptions(dictionaryName, optionsPresets, data.options); const dictionaryEntries = await translator.findKanji(text, options); const renderResults = await getTemplateRenderResults(dictionaryEntries, 'kanji', 'split', template, expect); - const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, 'split')); + const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, 'split')); expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries); expect.soft(noteDataList).toEqual(expected2.noteDataList); expect.soft(renderResults).toStrictEqual(expected3.results); diff --git a/test/dictionary-data.write.js b/test/dictionary-data.write.js index bdf635c8..d88bd3cd 100644 --- a/test/dictionary-data.write.js +++ b/test/dictionary-data.write.js @@ -35,7 +35,7 @@ const dirname = path.dirname(fileURLToPath(import.meta.url)); const dictionaryName = 'Test Dictionary 2'; const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName); -test('Write dictionary data expected data', async ({translator, ankiNoteDataCreator, expect}) => { +test('Write dictionary data expected data', async ({translator, expect}) => { const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json'); /** @type {import('test/translator').TranslatorTestInputs} */ const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'})); @@ -63,7 +63,7 @@ test('Write dictionary data expected data', async ({translator, ankiNoteDataCrea const options = createFindOptions(dictionaryName, optionsPresets, data.options); const {dictionaryEntries, originalTextLength} = await translator.findTerms(mode, text, options); const renderResults = mode !== 'simple' ? await getTemplateRenderResults(dictionaryEntries, 'terms', mode, template, null) : null; - const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mode)) : null; + const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, mode)) : null; actualResults1.push({name, originalTextLength, dictionaryEntries}); actualResults2.push({name, noteDataList}); actualResults3.push({name, results: renderResults}); @@ -76,7 +76,7 @@ test('Write dictionary data expected data', async ({translator, ankiNoteDataCrea const options = createFindOptions(dictionaryName, optionsPresets, data.options); const dictionaryEntries = await translator.findKanji(text, options); const renderResults = await getTemplateRenderResults(dictionaryEntries, 'kanji', 'split', template, null); - const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, 'split')); + const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(dictionaryEntry, 'split')); actualResults1.push({name, dictionaryEntries}); actualResults2.push({name, noteDataList}); actualResults3.push({name, results: renderResults}); diff --git a/test/fixtures/translator-test.js b/test/fixtures/translator-test.js index 58247b70..d1b3de8b 100644 --- a/test/fixtures/translator-test.js +++ b/test/fixtures/translator-test.js @@ -23,7 +23,6 @@ import {dirname, join} from 'path'; import {expect, vi} from 'vitest'; import {parseJson} from '../../dev/json.js'; import {createDictionaryArchive} from '../../dev/util.js'; -import {AnkiNoteDataCreator} from '../../ext/js/data/sandbox/anki-note-data-creator.js'; import {DictionaryDatabase} from '../../ext/js/dictionary/dictionary-database.js'; import {DictionaryImporter} from '../../ext/js/dictionary/dictionary-importer.js'; import {Translator} from '../../ext/js/language/translator.js'; @@ -42,7 +41,7 @@ vi.stubGlobal('chrome', chrome); /** * @param {string} dictionaryDirectory * @param {string} dictionaryName - * @returns {Promise<{translator: Translator, ankiNoteDataCreator: AnkiNoteDataCreator}>} + * @returns {Promise<Translator>} */ async function createTranslatorContext(dictionaryDirectory, dictionaryName) { // Dictionary @@ -69,31 +68,23 @@ async function createTranslatorContext(dictionaryDirectory, dictionaryName) { const deinflectionReasons = parseJson(readFileSync(languageTransformDescriptorPath, {encoding: 'utf8'})); translator.prepare(deinflectionReasons); - // Assign properties - const ankiNoteDataCreator = new AnkiNoteDataCreator(); - return {translator, ankiNoteDataCreator}; + return translator; } /** * @param {string|undefined} htmlFilePath * @param {string} dictionaryDirectory * @param {string} dictionaryName - * @returns {Promise<import('vitest').TestAPI<{window: import('jsdom').DOMWindow, translator: Translator, ankiNoteDataCreator: AnkiNoteDataCreator}>>} + * @returns {Promise<import('vitest').TestAPI<{window: import('jsdom').DOMWindow, translator: Translator}>>} */ export async function createTranslatorTest(htmlFilePath, dictionaryDirectory, dictionaryName) { const test = createDomTest(htmlFilePath); - const {translator, ankiNoteDataCreator} = await createTranslatorContext(dictionaryDirectory, dictionaryName); - /** @type {import('vitest').TestAPI<{window: import('jsdom').DOMWindow, translator: Translator, ankiNoteDataCreator: AnkiNoteDataCreator}>} */ + const translator = await createTranslatorContext(dictionaryDirectory, dictionaryName); + /** @type {import('vitest').TestAPI<{window: import('jsdom').DOMWindow, translator: Translator}>} */ const result = test.extend({ window: async ({window}, use) => { await use(window); }, // eslint-disable-next-line no-empty-pattern - translator: async ({}, use) => { await use(translator); }, - ankiNoteDataCreator: async ({window}, use) => { - // The window property needs to be referenced for it to be initialized. - // It is needed for DOM access for structured content. - void window; - await use(ankiNoteDataCreator); - } + translator: async ({}, use) => { await use(translator); } }); return result; } diff --git a/test/profile-conditions-util.test.js b/test/profile-conditions-util.test.js index 7af5f223..fcd53939 100644 --- a/test/profile-conditions-util.test.js +++ b/test/profile-conditions-util.test.js @@ -17,7 +17,7 @@ */ import {describe, expect, test} from 'vitest'; -import {ProfileConditionsUtil} from '../ext/js/background/profile-conditions-util.js'; +import {createSchema, normalizeContext} from '../ext/js/background/profile-conditions-util.js'; /** */ function testNormalizeContext() { @@ -50,8 +50,7 @@ function testNormalizeContext() { ]; test.each(data)('normalize-context-test-%#', ({context, expected}) => { - const profileConditionsUtil = new ProfileConditionsUtil(); - const actual = profileConditionsUtil.normalizeContext(context); + const actual = normalizeContext(context); expect(actual).toStrictEqual(expected); }); }); @@ -1101,14 +1100,13 @@ function testSchemas() { /* eslint-enable no-multi-spaces */ test.each(data)('schemas-test-%#', ({conditionGroups, expectedSchema, inputs}) => { - const profileConditionsUtil = new ProfileConditionsUtil(); - const schema = profileConditionsUtil.createSchema(conditionGroups); + const schema = createSchema(conditionGroups); if (typeof expectedSchema !== 'undefined') { expect(schema.schema).toStrictEqual(expectedSchema); } if (Array.isArray(inputs)) { for (const {expected, context} of inputs) { - const normalizedContext = profileConditionsUtil.normalizeContext(context); + const normalizedContext = normalizeContext(context); const actual = schema.isValid(normalizedContext); expect(actual).toStrictEqual(expected); } diff --git a/test/utilities/anki.js b/test/utilities/anki.js index e30d578f..69f4ce8b 100644 --- a/test/utilities/anki.js +++ b/test/utilities/anki.js @@ -16,16 +16,16 @@ */ import {AnkiNoteBuilder} from '../../ext/js/data/anki-note-builder.js'; +import {createAnkiNoteData} from '../../ext/js/data/sandbox/anki-note-data-creator.js'; import {AnkiTemplateRenderer} from '../../ext/js/templates/sandbox/anki-template-renderer.js'; /** - * @param {import('../../ext/js/data/sandbox/anki-note-data-creator.js').AnkiNoteDataCreator} ankiNoteDataCreator * @param {import('dictionary').DictionaryEntry} dictionaryEntry * @param {import('settings').ResultOutputMode} mode * @returns {import('anki-templates').NoteData} * @throws {Error} */ -export function createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mode) { +export function createTestAnkiNoteData(dictionaryEntry, mode) { const marker = '{marker}'; /** @type {import('anki-templates-internal').CreateDetails} */ const data = { @@ -43,7 +43,7 @@ export function createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mod }, media: {} }; - return ankiNoteDataCreator.create(marker, data); + return createAnkiNoteData(marker, data); } /** |