aboutsummaryrefslogtreecommitdiff
path: root/test/playwright/integration.spec.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-02-24 23:32:39 -0500
committerGitHub <noreply@github.com>2024-02-25 04:32:39 +0000
commita21948daf6210f67955ae4f98a81e21b8cf9f1f2 (patch)
tree9d4d39c25e23c3c25fbd0f3a557a79c05c825e09 /test/playwright/integration.spec.js
parent0e005f2ca6a3d9e572ed18b51c720d8bea907118 (diff)
Improve type safety for playwright stuff (#727)
Diffstat (limited to 'test/playwright/integration.spec.js')
-rw-r--r--test/playwright/integration.spec.js34
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());
});