Skip to content

Commit

Permalink
test: add ssg specific headers
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed May 23, 2024
1 parent 9584173 commit c7c750b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
20 changes: 20 additions & 0 deletions e2e-tests/adapters/cypress/e2e/headers.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ describe("Headers", () => {

beforeEach(() => {
cy.intercept(PATH_PREFIX + "/", WorkaroundCachedResponse).as("index")
cy.intercept(
PATH_PREFIX + "/routes/ssg/static",
WorkaroundCachedResponse
).as("ssg")
cy.intercept(
PATH_PREFIX + "/routes/ssr/static",
WorkaroundCachedResponse
Expand Down Expand Up @@ -128,6 +132,22 @@ describe("Headers", () => {
checkHeaders("@js")
})

it("should contain correct headers for ssg page", () => {
cy.visit("routes/ssg/static").waitForRouteChange()

checkHeaders("@ssg", {
...defaultHeaders,
"x-custom-header": "my custom header value",
"x-ssg-header": "my custom header value",
"cache-control": "public,max-age=0,must-revalidate",
})

checkHeaders("@app-data")
checkHeaders("@page-data")
checkHeaders("@slice-data")
checkHeaders("@static-query-result")
})

it("should contain correct headers for ssr page", () => {
cy.visit("routes/ssr/static").waitForRouteChange()

Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/adapters/cypress/e2e/remote-file.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const PATH_PREFIX = Cypress.env(`PATH_PREFIX`) || ``
const configs = [
{
title: `remote-file (SSG, Page Query)`,
pagePath: `/routes/remote-file/`,
pagePath: `/routes/ssg/remote-file/`,
placeholders: true,
},
{
title: `remote-file (SSG, Page Context)`,
pagePath: `/routes/remote-file-data-from-context/`,
pagePath: `/routes/ssg/remote-file-data-from-context/`,
placeholders: true,
},
{
Expand Down
46 changes: 35 additions & 11 deletions e2e-tests/adapters/cypress/e2e/trailing-slash.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertPageVisits } from "../utils/assert-page-visits"
import { applyTrailingSlashOption } from "../../utils"

Cypress.on("uncaught:exception", (err) => {
Cypress.on("uncaught:exception", err => {
if (err.message.includes("Minified React error")) {
return false
}
Expand All @@ -12,38 +12,62 @@ const TRAILING_SLASH = Cypress.env(`TRAILING_SLASH`) || `never`
describe("trailingSlash", () => {
describe(TRAILING_SLASH, () => {
it("should work when using Gatsby Link (without slash)", () => {
cy.visit('/').waitForRouteChange()
cy.visit("/").waitForRouteChange()

cy.get(`[data-testid="static-without-slash"]`).click().waitForRouteChange().assertRoute(applyTrailingSlashOption(`/routes/static`, TRAILING_SLASH))
cy.get(`[data-testid="static-without-slash"]`)
.click()
.waitForRouteChange()
.assertRoute(
applyTrailingSlashOption(`/routes/ssg/static`, TRAILING_SLASH)
)
})
it("should work when using Gatsby Link (with slash)", () => {
cy.visit('/').waitForRouteChange()
cy.visit("/").waitForRouteChange()

cy.get(`[data-testid="static-with-slash"]`).click().waitForRouteChange().assertRoute(applyTrailingSlashOption(`/routes/static`, TRAILING_SLASH))
cy.get(`[data-testid="static-with-slash"]`)
.click()
.waitForRouteChange()
.assertRoute(
applyTrailingSlashOption(`/routes/ssg/static`, TRAILING_SLASH)
)
})
it("should work on direct visit (with other setting)", () => {
const destination = applyTrailingSlashOption("/routes/static", TRAILING_SLASH)
const inverse = TRAILING_SLASH === `always` ? "/routes/static" : "/routes/static/"
const destination = applyTrailingSlashOption(
"/routes/ssg/static",
TRAILING_SLASH
)
const inverse =
TRAILING_SLASH === `always`
? "/routes/ssg/static"
: "/routes/ssg/static/"

assertPageVisits([
{
path: destination,
status: 200,
},
{ path: inverse, status: 301, destinationPath: destination }
{ path: inverse, status: 301, destinationPath: destination },
])

cy.visit(inverse).waitForRouteChange().assertRoute(applyTrailingSlashOption(`/routes/static`, TRAILING_SLASH))
cy.visit(inverse)
.waitForRouteChange()
.assertRoute(
applyTrailingSlashOption(`/routes/ssg/static`, TRAILING_SLASH)
)
})
it("should work on direct visit (with current setting)", () => {
assertPageVisits([
{
path: applyTrailingSlashOption("/routes/static", TRAILING_SLASH),
path: applyTrailingSlashOption("/routes/ssg/static", TRAILING_SLASH),
status: 200,
},
])

cy.visit(applyTrailingSlashOption("/routes/static", TRAILING_SLASH)).waitForRouteChange().assertRoute(applyTrailingSlashOption(`/routes/static`, TRAILING_SLASH))
cy.visit(applyTrailingSlashOption("/routes/ssg/static", TRAILING_SLASH))
.waitForRouteChange()
.assertRoute(
applyTrailingSlashOption(`/routes/ssg/static`, TRAILING_SLASH)
)
})
})
})
9 changes: 9 additions & 0 deletions e2e-tests/adapters/gatsby-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ const config: GatsbyConfig = {
},
],
},
{
source: `routes/ssg/*`,
headers: [
{
key: "x-ssg-header",
value: "my custom header value",
},
],
},
],
...configOverrides,
}
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/adapters/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const createPages: GatsbyNode["createPages"] = async ({

createPage({
path: applyTrailingSlashOption(
`/routes/remote-file-data-from-context/`,
`/routes/ssg/remote-file-data-from-context/`,
TRAILING_SLASH
),
component: path.resolve(`./src/templates/remote-file-from-context.jsx`),
Expand Down
8 changes: 4 additions & 4 deletions e2e-tests/adapters/src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import "./index.css"
const routes = [
{
text: "Static",
url: "/routes/static",
url: "/routes/ssg/static",
id: "static-without-slash",
},
{
text: "Static (With Slash)",
url: "/routes/static/",
url: "/routes/ssg/static/",
id: "static-with-slash",
},
{
Expand Down Expand Up @@ -41,11 +41,11 @@ const routes = [
},
{
text: "RemoteFile (ImageCDN and FileCDN) (SSG, Page Query)",
url: "/routes/remote-file",
url: "/routes/ssg/remote-file",
},
{
text: "RemoteFile (ImageCDN and FileCDN) (SSG, Page Context)",
url: "/routes/remote-file-data-from-context",
url: "/routes/ssg/remote-file-data-from-context",
},
{
text: "RemoteFile (ImageCDN and FileCDN) (SSR, Page Query)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { graphql } from "gatsby"
import React from "react"

import { GatsbyImage } from "gatsby-plugin-image"
import Layout from "../../components/layout"
import Layout from "../../../components/layout"

const RemoteFile = ({ data }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react"
import Layout from "../../components/layout"
import Layout from "../../../components/layout"

const StaticPage = () => {
return (
Expand All @@ -11,4 +11,4 @@ const StaticPage = () => {

export default StaticPage

export const Head = () => <title>Static</title>
export const Head = () => <title>Static</title>

0 comments on commit c7c750b

Please sign in to comment.