diff options
Diffstat (limited to 'test/playwright/integration.spec.js')
-rw-r--r-- | test/playwright/integration.spec.js | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/test/playwright/integration.spec.js b/test/playwright/integration.spec.js index 4957b676..8e641397 100644 --- a/test/playwright/integration.spec.js +++ b/test/playwright/integration.spec.js @@ -17,12 +17,12 @@ import path from 'path'; import {createDictionaryArchive} from '../../dev/util.js'; +import {deferPromise} from '../../ext/js/core/utilities.js'; import { expect, - expectedAddNoteBody, + getExpectedAddNoteBody, + getMockModelFields, mockAnkiRouteHandler, - mockModelFieldNames, - mockModelFieldsToAnkiValues, root, test, writeToClipboardFromPage @@ -45,16 +45,21 @@ test('search clipboard', async ({page, extensionId}) => { test('anki add', async ({context, page, extensionId}) => { // Mock anki routes - /** @type {?(value: unknown) => void} */ - let resolve = null; - const addNotePromise = new Promise((res) => { - resolve = res; - }); + /** @type {import('core').DeferredPromiseDetails<Record<string, unknown>>} */ + const addNotePromiseDetails = deferPromise(); await context.route(/127.0.0.1:8765\/*/, (route) => { void mockAnkiRouteHandler(route); const req = route.request(); - if (req.url().includes('127.0.0.1:8765') && req.postDataJSON().action === 'addNote') { - /** @type {(value: unknown) => void} */ (resolve)(req.postDataJSON()); + if (req.url().includes('127.0.0.1:8765')) { + /** @type {unknown} */ + const requestJson = req.postDataJSON(); + if ( + typeof requestJson === 'object' && + requestJson !== null && + /** @type {Record<string, unknown>} */ (requestJson).action === 'addNote' + ) { + addNotePromiseDetails.resolve(/** @type {Record<string, unknown>} */ (requestJson)); + } } }); @@ -79,8 +84,9 @@ test('anki add', async ({context, page, extensionId}) => { await page.locator('[data-modal-action="show,anki-cards"]').click(); await page.locator('select.anki-card-deck').selectOption('Mock Deck'); await page.locator('select.anki-card-model').selectOption('Mock Model'); - for (const modelField of mockModelFieldNames) { - await page.locator(`[data-setting="anki.terms.fields.${modelField}"]`).fill(/** @type {string} */ (mockModelFieldsToAnkiValues[modelField])); + const mockFields = getMockModelFields(); + for (const [modelField, value] of mockFields) { + await page.locator(`[data-setting="anki.terms.fields.${modelField}"]`).fill(value); } await page.locator('#anki-cards-modal > div > div.modal-footer > button:nth-child(2)').click(); await writeToClipboardFromPage(page, '読むの例文'); @@ -94,6 +100,6 @@ test('anki add', async ({context, page, extensionId}) => { }).toPass({timeout: 5000}); await page.locator('#search-textbox').press('Enter'); await page.locator('[data-mode="term-kanji"]').click(); - const addNoteReqBody = await addNotePromise; - expect(addNoteReqBody).toMatchObject(expectedAddNoteBody); + const addNoteReqBody = await addNotePromiseDetails.promise; + expect(addNoteReqBody).toMatchObject(getExpectedAddNoteBody()); }); |