From bf20b44565239022bd9c574dfa04c5c812bc29ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20D=C4=9Bdi=C4=8D?= Date: Sun, 22 Oct 2023 01:35:47 +0200 Subject: [PATCH] Modified playwright endpoint stubing to enable multiple different responses --- __tests__/frontend-playwright/basic.spec.ts | 28 ++++++++------ .../frontend-playwright/configuration.spec.ts | 28 ++++++++------ .../destination-selection-api-error.spec.ts | 38 +++++++++++++------ ...tination-selection-unhandled-error.spec.ts | 38 +++++++++++++------ ...estination-selection-unknown-error.spec.ts | 38 +++++++++++++------ .../move-api-error.spec.ts | 28 ++++++++------ .../move-folders-equal-error.spec.ts | 28 ++++++++------ .../test-utils-playwright/stub-endpoints.ts | 27 ++++++------- 8 files changed, 153 insertions(+), 100 deletions(-) diff --git a/__tests__/frontend-playwright/basic.spec.ts b/__tests__/frontend-playwright/basic.spec.ts index e1dc1fe1..bc2cc7c0 100644 --- a/__tests__/frontend-playwright/basic.spec.ts +++ b/__tests__/frontend-playwright/basic.spec.ts @@ -7,18 +7,22 @@ test.beforeEach(async ({ page }) => { await setup(page); await page.evaluate(() => { - window._endpointStubs.listFolders = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.listSharedDrives = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.move = { - status: "success", - value: { status: "success", response: { errors: [] } }, - }; + window._endpointStubs.listSharedDrives = [ + { + status: "success", + value: { status: "success", response: [] }, + }, + { + status: "success", + value: { status: "success", response: [] }, + }, + ]; + window._endpointStubs.move = [ + { + status: "success", + value: { status: "success", response: { errors: [] } }, + }, + ]; }); }); diff --git a/__tests__/frontend-playwright/configuration.spec.ts b/__tests__/frontend-playwright/configuration.spec.ts index e5bb1c8c..fe909a0e 100644 --- a/__tests__/frontend-playwright/configuration.spec.ts +++ b/__tests__/frontend-playwright/configuration.spec.ts @@ -7,18 +7,22 @@ test.beforeEach(async ({ page }) => { await setup(page); await page.evaluate(() => { - window._endpointStubs.listFolders = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.listSharedDrives = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.move = { - status: "success", - value: { status: "success", response: { errors: [] } }, - }; + window._endpointStubs.listSharedDrives = [ + { + status: "success", + value: { status: "success", response: [] }, + }, + { + status: "success", + value: { status: "success", response: [] }, + }, + ]; + window._endpointStubs.move = [ + { + status: "success", + value: { status: "success", response: { errors: [] } }, + }, + ]; }); }); diff --git a/__tests__/frontend-playwright/destination-selection-api-error.spec.ts b/__tests__/frontend-playwright/destination-selection-api-error.spec.ts index 07317530..edfeed8a 100644 --- a/__tests__/frontend-playwright/destination-selection-api-error.spec.ts +++ b/__tests__/frontend-playwright/destination-selection-api-error.spec.ts @@ -7,20 +7,34 @@ test.beforeEach(async ({ page }) => { await setup(page); await page.evaluate(() => { - window._endpointStubs.listFolders = { - status: "success", - value: { status: "error", type: "DriveAPIError" }, - }; - window._endpointStubs.listSharedDrives = { - status: "success", - value: { + window._endpointStubs.listFolders = [ + { status: "success", - response: [ - { id: "ID_DRIVE_1", name: "DRIVE 1" }, - { id: "ID_DRIVE_2", name: "DRIVE 2" }, - ], + value: { status: "error", type: "DriveAPIError" }, }, - }; + ]; + window._endpointStubs.listSharedDrives = [ + { + status: "success", + value: { + status: "success", + response: [ + { id: "ID_DRIVE_1", name: "DRIVE 1" }, + { id: "ID_DRIVE_2", name: "DRIVE 2" }, + ], + }, + }, + { + status: "success", + value: { + status: "success", + response: [ + { id: "ID_DRIVE_1", name: "DRIVE 1" }, + { id: "ID_DRIVE_2", name: "DRIVE 2" }, + ], + }, + }, + ]; }); }); diff --git a/__tests__/frontend-playwright/destination-selection-unhandled-error.spec.ts b/__tests__/frontend-playwright/destination-selection-unhandled-error.spec.ts index b58fc68f..9a34ad1f 100644 --- a/__tests__/frontend-playwright/destination-selection-unhandled-error.spec.ts +++ b/__tests__/frontend-playwright/destination-selection-unhandled-error.spec.ts @@ -7,20 +7,34 @@ test.beforeEach(async ({ page }) => { await setup(page); await page.evaluate(() => { - window._endpointStubs.listFolders = { - status: "failure", - value: new Error("ERROR MESSAGE"), - }; - window._endpointStubs.listSharedDrives = { - status: "success", - value: { + window._endpointStubs.listFolders = [ + { + status: "failure", + value: new Error("ERROR MESSAGE"), + }, + ]; + window._endpointStubs.listSharedDrives = [ + { + status: "success", + value: { + status: "success", + response: [ + { id: "ID_DRIVE_1", name: "DRIVE 1" }, + { id: "ID_DRIVE_2", name: "DRIVE 2" }, + ], + }, + }, + { status: "success", - response: [ - { id: "ID_DRIVE_1", name: "DRIVE 1" }, - { id: "ID_DRIVE_2", name: "DRIVE 2" }, - ], + value: { + status: "success", + response: [ + { id: "ID_DRIVE_1", name: "DRIVE 1" }, + { id: "ID_DRIVE_2", name: "DRIVE 2" }, + ], + }, }, - }; + ]; }); }); diff --git a/__tests__/frontend-playwright/destination-selection-unknown-error.spec.ts b/__tests__/frontend-playwright/destination-selection-unknown-error.spec.ts index f37cfc1a..2b3f05ff 100644 --- a/__tests__/frontend-playwright/destination-selection-unknown-error.spec.ts +++ b/__tests__/frontend-playwright/destination-selection-unknown-error.spec.ts @@ -7,20 +7,34 @@ test.beforeEach(async ({ page }) => { await setup(page); await page.evaluate(() => { - window._endpointStubs.listFolders = { - status: "success", - value: { status: "error", type: "unknown" }, - }; - window._endpointStubs.listSharedDrives = { - status: "success", - value: { + window._endpointStubs.listFolders = [ + { status: "success", - response: [ - { id: "ID_DRIVE_1", name: "DRIVE 1" }, - { id: "ID_DRIVE_2", name: "DRIVE 2" }, - ], + value: { status: "error", type: "unknown" }, }, - }; + ]; + window._endpointStubs.listSharedDrives = [ + { + status: "success", + value: { + status: "success", + response: [ + { id: "ID_DRIVE_1", name: "DRIVE 1" }, + { id: "ID_DRIVE_2", name: "DRIVE 2" }, + ], + }, + }, + { + status: "success", + value: { + status: "success", + response: [ + { id: "ID_DRIVE_1", name: "DRIVE 1" }, + { id: "ID_DRIVE_2", name: "DRIVE 2" }, + ], + }, + }, + ]; }); }); diff --git a/__tests__/frontend-playwright/move-api-error.spec.ts b/__tests__/frontend-playwright/move-api-error.spec.ts index 561512cb..d2fa9109 100644 --- a/__tests__/frontend-playwright/move-api-error.spec.ts +++ b/__tests__/frontend-playwright/move-api-error.spec.ts @@ -7,18 +7,22 @@ test.beforeEach(async ({ page }) => { await setup(page); await page.evaluate(() => { - window._endpointStubs.listFolders = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.listSharedDrives = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.move = { - status: "success", - value: { status: "error", type: "DriveAPIError" }, - }; + window._endpointStubs.listSharedDrives = [ + { + status: "success", + value: { status: "success", response: [] }, + }, + { + status: "success", + value: { status: "success", response: [] }, + }, + ]; + window._endpointStubs.move = [ + { + status: "success", + value: { status: "error", type: "DriveAPIError" }, + }, + ]; }); }); diff --git a/__tests__/frontend-playwright/move-folders-equal-error.spec.ts b/__tests__/frontend-playwright/move-folders-equal-error.spec.ts index aed327f6..14175aa3 100644 --- a/__tests__/frontend-playwright/move-folders-equal-error.spec.ts +++ b/__tests__/frontend-playwright/move-folders-equal-error.spec.ts @@ -7,18 +7,22 @@ test.beforeEach(async ({ page }) => { await setup(page); await page.evaluate(() => { - window._endpointStubs.listFolders = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.listSharedDrives = { - status: "success", - value: { status: "success", response: [] }, - }; - window._endpointStubs.move = { - status: "success", - value: { status: "error", type: "sourceEqualsDestination" }, - }; + window._endpointStubs.listSharedDrives = [ + { + status: "success", + value: { status: "success", response: [] }, + }, + { + status: "success", + value: { status: "success", response: [] }, + }, + ]; + window._endpointStubs.move = [ + { + status: "success", + value: { status: "error", type: "sourceEqualsDestination" }, + }, + ]; }); }); diff --git a/__tests__/test-utils-playwright/stub-endpoints.ts b/__tests__/test-utils-playwright/stub-endpoints.ts index b443c95e..b6f77a6a 100644 --- a/__tests__/test-utils-playwright/stub-endpoints.ts +++ b/__tests__/test-utils-playwright/stub-endpoints.ts @@ -4,7 +4,7 @@ import type { Page } from "@playwright/test"; declare global { interface Window { - _endpointStubs: Record; + _endpointStubs: Record>; } } @@ -40,7 +40,7 @@ export async function setup(page: Page): Promise { ); await page.evaluate(() => { - window._endpointStubs = {} as Record; + window._endpointStubs = {} as Record>; function endpointFn( successHandler: SuccessHandlerType, @@ -48,22 +48,17 @@ export async function setup(page: Page): Promise { ): Record void> { const stubbedEndpoints: Record void> = {}; for (const key in window._endpointStubs) { - const stub = window._endpointStubs[key]; - if (stub.status === "success") { - stubbedEndpoints[key] = ( - ...args: Array - ): void => { - _logEndpointCall(key, args); + stubbedEndpoints[key] = ( + ...args: Array + ): void => { + _logEndpointCall(key, args); + const stub = window._endpointStubs[key].shift()!; + if (stub.status === "success") { successHandler(stub.value); - }; - } else { - stubbedEndpoints[key] = ( - ...args: Array - ): void => { - _logEndpointCall(key, args); + } else { failureHandler(stub.value); - }; - } + } + }; } return stubbedEndpoints; }