Skip to content

Commit

Permalink
Modified playwright endpoint stubing to enable multiple different res…
Browse files Browse the repository at this point in the history
…ponses
  • Loading branch information
marekdedic committed Oct 21, 2023
1 parent ee042c6 commit bf20b44
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 100 deletions.
28 changes: 16 additions & 12 deletions __tests__/frontend-playwright/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] } },
},
];
});
});

Expand Down
28 changes: 16 additions & 12 deletions __tests__/frontend-playwright/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] } },
},
];
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
],
},
},
];
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
],
},
},
};
];
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
],
},
},
];
});
});

Expand Down
28 changes: 16 additions & 12 deletions __tests__/frontend-playwright/move-api-error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
];
});
});

Expand Down
28 changes: 16 additions & 12 deletions __tests__/frontend-playwright/move-folders-equal-error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
];
});
});

Expand Down
27 changes: 11 additions & 16 deletions __tests__/test-utils-playwright/stub-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Page } from "@playwright/test";

declare global {
interface Window {
_endpointStubs: Record<string, EndpointStub>;
_endpointStubs: Record<string, Array<EndpointStub>>;
}
}

Expand Down Expand Up @@ -40,30 +40,25 @@ export async function setup(page: Page): Promise<void> {
);

await page.evaluate(() => {
window._endpointStubs = {} as Record<string, EndpointStub>;
window._endpointStubs = {} as Record<string, Array<EndpointStub>>;

function endpointFn(
successHandler: SuccessHandlerType,
failureHandler: FailureHandlerType,
): Record<string, () => void> {
const stubbedEndpoints: Record<string, () => void> = {};
for (const key in window._endpointStubs) {
const stub = window._endpointStubs[key];
if (stub.status === "success") {
stubbedEndpoints[key] = (
...args: Array<google.script.Parameter>
): void => {
_logEndpointCall(key, args);
stubbedEndpoints[key] = (
...args: Array<google.script.Parameter>
): void => {
_logEndpointCall(key, args);
const stub = window._endpointStubs[key].shift()!;
if (stub.status === "success") {
successHandler(stub.value);
};
} else {
stubbedEndpoints[key] = (
...args: Array<google.script.Parameter>
): void => {
_logEndpointCall(key, args);
} else {
failureHandler(stub.value);
};
}
}
};
}
return stubbedEndpoints;
}
Expand Down

0 comments on commit bf20b44

Please sign in to comment.