Skip to content

Commit 9411b7a

Browse files
authored
add e2e for next/head and next/amp in pages router (#845)
* add e2e for next/head * add test for next/amp * add html attr to test * comment
1 parent c212ed0 commit 9411b7a

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* When doing `next build` you would get the error below:
3+
* TypeScript error: Property 'amp-timeago' does not exist on type 'JSX.IntrinsicElements'.
4+
* https://stackoverflow.com/questions/50585952/property-amp-img-does-not-exist-on-type-jsx-intrinsicelements/50601125#50601125
5+
* The workaround in that SO post doesn't work in this (mono)repo so I ended up using @ts-expect-error and @ts-ignore
6+
*
7+
*/
8+
9+
export const config = { amp: true };
10+
11+
export async function getServerSideProps() {
12+
return {
13+
props: {
14+
time: new Date().toISOString(),
15+
},
16+
};
17+
}
18+
19+
function MyAmpPage({ time }: { time: string }) {
20+
const date = new Date(time);
21+
22+
return (
23+
<div>
24+
<p>Some time: {date.toJSON()}</p>
25+
{/* @ts-expect-error AMP Component not recognized by TypeScript */}
26+
<amp-timeago
27+
width="0"
28+
height="15"
29+
datetime={date.toJSON()}
30+
layout="responsive"
31+
data-testid="amp-timeago"
32+
>
33+
.{/* @ts-ignore */}
34+
</amp-timeago>
35+
</div>
36+
);
37+
}
38+
39+
export default MyAmpPage;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { InferGetServerSidePropsType } from "next";
2+
import Head from "next/head";
3+
4+
export async function getServerSideProps() {
5+
return {
6+
props: {
7+
time: new Date().toISOString(),
8+
envVar: process.env.SOME_PROD_VAR,
9+
},
10+
};
11+
}
12+
13+
export default function Page({
14+
time,
15+
envVar,
16+
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
17+
return (
18+
<div>
19+
<Head>
20+
<title>OpenNext head</title>
21+
<meta
22+
property="og:title"
23+
content={`OpenNext pages router head ${envVar}`}
24+
/>
25+
<meta property="time" content={time} />
26+
<meta
27+
name="description"
28+
content="OpenNext takes the Next.js build output and converts it into packages that can be deployed across a variety of environments. Natively OpenNext has support for AWS Lambda, Cloudflare, and classic Node.js Server."
29+
/>
30+
</Head>
31+
<p>This is a page!</p>
32+
</div>
33+
);
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
test.describe("next/amp", () => {
4+
test("should load and display the timeago component", async ({ page }) => {
5+
await page.goto("/amp");
6+
const timeago = await page.getByTestId("amp-timeago").textContent();
7+
// We can safely assume this will always show `just now` as its using `format()` from `timeago.js`.
8+
// It will show `just now` if the time is less than 10s ago.
9+
expect(timeago).toBe("just now");
10+
const htmlEl = page.locator("html");
11+
await expect(htmlEl).toHaveAttribute("amp");
12+
});
13+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
test.describe("next/head", () => {
4+
test("should have the correct title", async ({ page }) => {
5+
await page.goto("/head");
6+
const title = await page.title();
7+
expect(title).toBe("OpenNext head");
8+
});
9+
test("should have the correct meta tags", async ({ page }) => {
10+
await page.goto("/head");
11+
const ogTitle = await page
12+
.locator('meta[property="og:title"]')
13+
.getAttribute("content");
14+
const ogDesc = await page
15+
.locator('meta[name="description"]')
16+
.getAttribute("content");
17+
const time = await page
18+
.locator('meta[property="time"]')
19+
.getAttribute("content");
20+
expect(ogTitle).toBe("OpenNext pages router head bar");
21+
expect(ogDesc).toBe(
22+
"OpenNext takes the Next.js build output and converts it into packages that can be deployed across a variety of environments. Natively OpenNext has support for AWS Lambda, Cloudflare, and classic Node.js Server.",
23+
);
24+
25+
expect(new Date(time!).getTime()).toBeLessThan(Date.now());
26+
});
27+
});

0 commit comments

Comments
 (0)