Skip to content

Commit

Permalink
test: more
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Dec 5, 2024
1 parent ad894fe commit 363231f
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 9 deletions.
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig, devices } from "@playwright/test";

// https://playwright.dev/docs/test-configuration
export default defineConfig({
testDir: "./tests",
testDir: "./tests",testMatch:"**/*.spec.*",
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions tests-app/app/PrismicPreview/default/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PrismicPreview } from "@prismicio/next";

export default function Page() {
return (
<div>
<PrismicPreview repositoryName="foobar" />
<span data-testid="timestamp">timestamp: {Date.now()}</span>
</div>
);
}
13 changes: 13 additions & 0 deletions tests-app/app/PrismicPreview/with-custom-exit-url/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PrismicPreview } from "@prismicio/next";

export default function Page() {
return (
<div>
<PrismicPreview
repositoryName="foobar"
exitPreviewURL="/api/custom-exit-preview"
/>
<span data-testid="timestamp">timestamp: {Date.now()}</span>
</div>
);
}
6 changes: 6 additions & 0 deletions tests-app/app/api/custom-exit-preview/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { exitPreview } from "@prismicio/next";

/** This endpoint exits a preview session. */
export function GET() {
return exitPreview();
}
6 changes: 6 additions & 0 deletions tests-app/app/api/exit-preview/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { exitPreview } from "@prismicio/next";

/** This endpoint exits a preview session. */
export function GET() {
return exitPreview();
}
5 changes: 0 additions & 5 deletions tests-app/app/app/PrismicPreview/default/page.tsx

This file was deleted.

45 changes: 42 additions & 3 deletions tests/PrismicPreview.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import { test, expect } from "@playwright/test";
import { test, expect } from "./test";

test("renders an image field", async ({ page }) => {
await page.goto("/app/PrismicPreview/default");
test("adds the Prismic toolbar script", async ({ appRouterPage }) => {
await appRouterPage.goto("/PrismicPreview/default");
await expect(appRouterPage.toolbarScript).toHaveCount(1);
const repo = await appRouterPage.getToolbarScriptParam("repo");
expect(repo).toBe("foobar");
});

test("refreshes the page on prismicPreviewUpdate events", async ({
appRouterPage,
}) => {
await appRouterPage.goto("/PrismicPreview/default");
await appRouterPage.dispatchPreviewUpdateEvent();
await appRouterPage.waitForTimestampChange();
});

test("calls the default exit preview endpoint on prismicPreviewEnd events", async ({
appRouterPage,
}) => {
await appRouterPage.goto("/PrismicPreview/default");
const request = appRouterPage.waitForRequest("/api/exit-preview");
await appRouterPage.dispatchPreviewEndEvent();
await request;
await appRouterPage.waitForTimestampChange();
});

test("calls the custom exit preview endpoint on prismicPreviewEnd events", async ({
appRouterPage,
}) => {
await appRouterPage.goto("/PrismicPreview/with-custom-exit-url");
const request = appRouterPage.waitForRequest("/api/custom-exit-preview");
await appRouterPage.dispatchPreviewEndEvent();
await request;
await appRouterPage.waitForTimestampChange();
});

test("calls the preview endpoint when accessing from a preview share link", async ({
appRouterPage,
}) => {
await appRouterPage.goto("/PrismicPreview/default");
await appRouterPage.dispatchPreviewUpdateEvent();
await appRouterPage.waitForTimestampChange();
});
77 changes: 77 additions & 0 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Locator, Page, test as base } from "@playwright/test";

export { expect } from "@playwright/test";

type Fixtures = {
appRouterPage: AppRouterPage;
pagesRouterPage: PagesRouterPage;
};

export const test = base.extend<Fixtures>({
appRouterPage: async ({ page }, use) => {
await use(new AppRouterPage(page));
},
pagesRouterPage: async ({ page }, use) => {
await use(new PagesRouterPage(page));
},
});

class DefaultPage {
page: Page;
baseURL: string;
toolbarScript: Locator;

constructor(page: Page, baseURL: string) {
this.page = page;
this.baseURL = baseURL;

// Locators
this.toolbarScript = page.locator('script[src*="prismic.io/prismic.js"]');
}

async goto(path: string) {
await this.page.goto(new URL(path, this.baseURL).toString());
}

async waitForRequest(path: string) {
return await this.page.waitForRequest(
new URL(path, this.baseURL).toString(),
);
}

async waitForTimestampChange() {
await this.page.waitForFunction(
(initialContent) =>
document.querySelector('[data-testid="timestamp"]')?.textContent !==
initialContent,
await this.page.getByTestId("timestamp").textContent(),
);
}

async dispatchPreviewUpdateEvent(ref = "ref") {
return await this.page
.locator("body")
.dispatchEvent("prismicPreviewUpdate", { detail: { ref } });
}

async dispatchPreviewEndEvent() {
return await this.page.locator("body").dispatchEvent("prismicPreviewEnd");
}

async getToolbarScriptParam(name: string) {
const src = await this.toolbarScript.getAttribute("src");
return new URL(src ?? "").searchParams.get(name);
}
}

class AppRouterPage extends DefaultPage {
constructor(page: Page) {
super(page, "http://localhost:4321");
}
}

class PagesRouterPage extends DefaultPage {
constructor(page: Page) {
super(page, "http://localhost:4322");
}
}

0 comments on commit 363231f

Please sign in to comment.