Skip to content

Commit

Permalink
add tests to e2e/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Sep 17, 2022
1 parent 2c3c86b commit 8fe5aa8
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,56 @@
describe(`collection-routing`, () => {
beforeEach(() => {
cy.visit(`/collection-routing`).waitForRouteChange()
function assert404(slug) {
cy.visit(`/collection-routing/mutations/${slug}/`, {
failOnStatusCode: false,
}).waitForRouteChange()

// page doesn't exist yet
cy.get(`h1`).invoke(`text`).should(`eq`, `Gatsby.js development 404 page`)

cy.visit(`/collection-routing/mutations/child-${slug}/`, {
failOnStatusCode: false,
}).waitForRouteChange()

// page doesn't exist yet
cy.get(`h1`).invoke(`text`).should(`eq`, `Gatsby.js development 404 page`)
}

function assertPageExist(slug, content) {
cy.visit(`/collection-routing/mutations/${slug}/`).waitForRouteChange()
cy.contains(content)

cy.visit(`/collection-routing/mutations/child-${slug}/`).waitForRouteChange()
cy.contains(content)
}

function refresh(setup) {
cy.then(() => {
console.log(`fetch`, fetch)
return fetch(
`http://localhost:8000/__refresh/gatsby-source-fs-route-mutations`,
{
method: `POST`,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ setup }),
}
).then(() => {
console.log(`fetched`)
})
})

cy.wait(5000)
}

describe(`collection-routing`, () => {
it(`can create simplest collection route that also has a number as an identifier`, () => {
cy.visit(`/collection-routing/1/`).waitForRouteChange()
cy.findByTestId(`slug`).should(`have.text`, `/preview/1`)
cy.findByTestId(`pagecontext`).should(`have.text`, `1`)
})

it(`can navigate to a collection route and see its content rendered`, () => {
cy.visit(`/collection-routing`).waitForRouteChange()
// this test depends on the alphabetical sorting of markdown files
cy.findByTestId(`collection-routing-blog-0`)
.should(`have.attr`, `data-testslug`, `/2018-12-14-hello-world/`)
Expand All @@ -24,6 +65,7 @@ describe(`collection-routing`, () => {
})

it(`can navigate to a collection route that uses unions and see its content rendered`, () => {
cy.visit(`/collection-routing`).waitForRouteChange()
// this test depends on the alphabetical sorting of image files
cy.findByTestId(`collection-routing-image-0`)
.should(`have.attr`, `data-testimagename`, `citrus-fruits`)
Expand Down Expand Up @@ -61,4 +103,49 @@ describe(`collection-routing`, () => {
cy.findByTestId(`title`)
cy.should(`have.text`, `Named SPLAT Nested with Collection Route!`)
})

describe(`data updates`, () => {
before(() => {
refresh(`reset`)
})
after(() => {
refresh(`reset`)
})

it(`creates a page when new node is created`, () => {
assert404(`new-node`)
assert404(`updated-node`)

refresh(`create`)

assertPageExist(`new-node`, `This is node that was just created`)
assert404(`updated-node`)
})

it(`remove previous page and add a new one when slug changes`, () => {
assertPageExist(`new-node`, `This is node that was just created`)
assert404(`updated-node`)

refresh(`update`)

assertPageExist(
`updated-node`,
`This is node that had slug and content updated`
)
assert404(`new-node`)
})

it(`remove a page when node is deleted`, () => {
assertPageExist(
`updated-node`,
`This is node that had slug and content updated`
)
assert404(`new-node`)

refresh(`delete`)

assert404(`new-node`)
assert404(`updated-node`)
})
})
})
1 change: 1 addition & 0 deletions e2e-tests/development-runtime/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
`gatsby-source-fake-data`,
`gatsby-source-pinc-data`,
`gatsby-source-query-on-demand-data`,
`gatsby-source-fs-route-mutations`,
`gatsby-browser-tsx`,
`gatsby-node-typegen`,
`gatsby-transformer-sharp`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { GatsbyNode } from "gatsby"

let createdNodes = new Set<string>()

export const sourceNodes: GatsbyNode["sourceNodes"] = ({
actions,
webhookBody,
getNode,
createContentDigest,
reporter,
}) => {
const handledNodes = new Set(createdNodes)
function addNode(data: { id: string; slug: string; content: string }): void {
const node = {
...data,
parent: null,
children: [],
internal: {
type: `FilesystemRoutesMutation`,
contentDigest: createContentDigest(data),
},
}

createdNodes.add(node.id)
handledNodes.delete(node.id)

actions.createNode(node)

const childNode = {
...data,
id: `${node.id} << childNode`,
parent: node.id,
internal: {
type: `FilesystemRoutesMutationChild`,
contentDigest: node.internal.contentDigest,
},
}
actions.createNode(childNode)
const parent = getNode(node.id)

if (!parent) {
throw new Error(`Could not find parent node`)
}

actions.createParentChildLink({
parent: parent,
child: childNode,
})
}

if (webhookBody?.setup === `create`) {
reporter.verbose(`[gatsby-source-fs-route-mutation] create a new node`)
addNode({
id: `fs-route-mutation-test`,
slug: `new-node`,
content: `This is node that was just created`,
})
} else if (webhookBody?.setup === `update`) {
reporter.verbose(`[gatsby-source-fs-route-mutation] update a node`)
addNode({
id: `fs-route-mutation-test`,
slug: `updated-node`,
content: `This is node that had slug and content updated`,
})
} else if (webhookBody?.setup === `delete`) {
reporter.verbose(`[gatsby-source-fs-route-mutation] delete a node`)
} else {
reporter.verbose(`[gatsby-source-fs-route-mutation] initial setup`)
}

addNode({
id: `fs-route-mutation-stable`,
slug: `stable`,
content: `This is stable node`,
})

for (const nodeIdToDelete of handledNodes) {
const nodeToDelete = getNode(nodeIdToDelete)
if (nodeToDelete) {
createdNodes.delete(nodeIdToDelete)
actions.deleteNode(nodeToDelete)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react"
import { graphql } from "gatsby"

export default function FSRoutesMutationTemplate({ data }) {
return (
<>
<pre>{JSON.stringify(data, null, 2)}</pre>
</>
)
}

export const query = graphql`
query($id: String!) {
filesystemRoutesMutationChild(id: { eq: $id }) {
content
}
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react"
import { graphql } from "gatsby"

export default function FSRoutesMutationTemplate({ data }) {
return (
<>
<pre>{JSON.stringify(data, null, 2)}</pre>
</>
)
}

export const query = graphql`
query($id: String!) {
filesystemRoutesMutation(id: { eq: $id }) {
content
}
}
`

0 comments on commit 8fe5aa8

Please sign in to comment.