-
Notifications
You must be signed in to change notification settings - Fork 160
add e2e for next/head and next/amp in pages router #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* When doing `next build` you would get the error below: | ||
* TypeScript error: Property 'amp-timeago' does not exist on type 'JSX.IntrinsicElements'. | ||
* https://stackoverflow.com/questions/50585952/property-amp-img-does-not-exist-on-type-jsx-intrinsicelements/50601125#50601125 | ||
* The workaround in that SO post doesn't work in this (mono)repo so I ended up using @ts-expect-error and @ts-ignore | ||
* | ||
*/ | ||
|
||
export const config = { amp: true }; | ||
|
||
export async function getServerSideProps() { | ||
return { | ||
props: { | ||
time: new Date().toISOString(), | ||
}, | ||
}; | ||
} | ||
|
||
function MyAmpPage({ time }: { time: string }) { | ||
const date = new Date(time); | ||
|
||
return ( | ||
<div> | ||
<p>Some time: {date.toJSON()}</p> | ||
{/* @ts-expect-error AMP Component not recognized by TypeScript */} | ||
<amp-timeago | ||
width="0" | ||
height="15" | ||
datetime={date.toJSON()} | ||
layout="responsive" | ||
data-testid="amp-timeago" | ||
> | ||
.{/* @ts-ignore */} | ||
</amp-timeago> | ||
</div> | ||
); | ||
} | ||
|
||
export default MyAmpPage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { InferGetServerSidePropsType } from "next"; | ||
import Head from "next/head"; | ||
|
||
export async function getServerSideProps() { | ||
return { | ||
props: { | ||
time: new Date().toISOString(), | ||
envVar: process.env.SOME_PROD_VAR, | ||
}, | ||
}; | ||
} | ||
|
||
export default function Page({ | ||
time, | ||
envVar, | ||
}: InferGetServerSidePropsType<typeof getServerSideProps>) { | ||
return ( | ||
<div> | ||
<Head> | ||
<title>OpenNext head</title> | ||
<meta | ||
property="og:title" | ||
content={`OpenNext pages router head ${envVar}`} | ||
/> | ||
<meta property="time" content={time} /> | ||
<meta | ||
name="description" | ||
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." | ||
/> | ||
</Head> | ||
<p>This is a page!</p> | ||
</div> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
test.describe("next/amp", () => { | ||
test("should load and display the timeago component", async ({ page }) => { | ||
await page.goto("/amp"); | ||
const timeago = await page.getByTestId("amp-timeago").textContent(); | ||
expect(timeago).toBe("just now"); | ||
const htmlEl = page.locator("html"); | ||
await expect(htmlEl).toHaveAttribute("amp"); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
test.describe("next/head", () => { | ||
test("should have the correct title", async ({ page }) => { | ||
await page.goto("/head"); | ||
const title = await page.title(); | ||
expect(title).toBe("OpenNext head"); | ||
}); | ||
test("should have the correct meta tags", async ({ page }) => { | ||
await page.goto("/head"); | ||
const ogTitle = await page | ||
.locator('meta[property="og:title"]') | ||
.getAttribute("content"); | ||
const ogDesc = await page | ||
.locator('meta[name="description"]') | ||
.getAttribute("content"); | ||
const time = await page | ||
.locator('meta[property="time"]') | ||
.getAttribute("content"); | ||
expect(ogTitle).toBe("OpenNext pages router head bar"); | ||
expect(ogDesc).toBe( | ||
"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.", | ||
); | ||
|
||
expect(new Date(time!).getTime()).toBeLessThan(Date.now()); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How precise is it ?
Could it display a
1s ago
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory it should be always showing
just now
here as the value is calculated at render time, and that wont take many ms. Its usingformat()
fromtimeago.js
under the hood, and it will showjust now
until it reaches 10s, then it will start showing10 seconds ago
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a comment about it in the test.