Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinjude committed Oct 4, 2022
1 parent 84c525c commit 82d04e3
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { allRecipes } from "../../../shared-data/slices"

/**
* Test behaviour when a slice created and passed as a `slices` option via createPage
*/

describe("Slice passed via createPage", () => {
it("Created pages should have correct values", () => {
allRecipes.forEach(recipe => {
cy.visit(`recipe/${recipe.id}`).waitForRouteChange()

cy.getTestElement(`recipe-name`)
.invoke(`text`)
.should(`contain`, recipe.name)

cy.getTestElement(`recipe-description`)
.invoke(`text`)
.should(`contain`, recipe.description)

cy.getTestElement(`recipe-author-name`)
.invoke(`text`)
.should(`contain`, recipe.author.name)
})
})
})
33 changes: 33 additions & 0 deletions e2e-tests/development-runtime/cypress/integration/slices/slices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Test basic Slices API behaviour like context, props, ....
*/

describe(`Slices`, () => {
beforeEach(() => {
cy.visit(`/`).waitForRouteChange()
})

it(`Slice content show on screen`, () => {
cy.getTestElement(`footer-static-text`)
.invoke(`text`)
.should(`contain`, `Built with`)
})

it(`Slice recieves context passed via createSlice`, () => {
cy.getTestElement(`footer-slice-context-value`)
.invoke(`text`)
.should(`contain`, `Gatsby`)
})

it(`Slice can take in props`, () => {
cy.getTestElement(`footer-props`)
.invoke(`text`)
.should(`contains`, `Gatsbyjs`)
})

it(`Slice can consume a context wrapped in WrapRootElement`, () => {
cy.getTestElement(`footer-context-derieved-value`)
.invoke(`text`)
.should(`contain`, `2`)
})
})
40 changes: 37 additions & 3 deletions e2e-tests/development-runtime/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const headFunctionExportSharedData = require("./shared-data/head-function-export")
const slicesData = require("./shared-data/slices")
const {
addRemoteFilePolyfillInterface,
polyfillImageServiceDevRoutes,
Expand Down Expand Up @@ -37,7 +38,8 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
const items = [
{
name: "photoA.jpg",
url: "https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
url:
"https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
placeholderUrl:
"https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=%width%&h=%height%",
mimeType: "image/jpg",
Expand All @@ -47,15 +49,17 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
},
{
name: "photoB.jpg",
url: "https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&h=2000&q=10",
url:
"https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&h=2000&q=10",
mimeType: "image/jpg",
filename: "photo-1552053831.jpg",
width: 1247,
height: 2000,
},
{
name: "photoC.jpg",
url: "https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
url:
"https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
placeholderUrl:
"https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=%width%&h=%height%",
mimeType: "image/jpg",
Expand Down Expand Up @@ -172,10 +176,40 @@ exports.createPages = async function createPages({
})
})

//-------------------------Slice API----------------------------
createSlice({
id: `footer`,
component: path.resolve(`./src/components/footer.js`),
context: {
framework: slicesData.framework,
},
})

slicesData.allRecipeAuthors.forEach(({ id, name }) => {
createSlice({
id: `author-${id}`,
component: path.resolve(`./src/components/recipe-author.js`),
context: {
name,
id,
},
})
})

slicesData.allRecipes.forEach(({ authorId, id, name, description }) => {
createPage({
path: `/recipe/${id}`,
component: path.resolve(`./src/templates/recipe.js`),
context: {
description: description,
name,
},
slices: {
author: `author-${authorId}`,
},
})
})
//---------------------------------------------------------------

createPage({
path: `/안녕`,
Expand Down
25 changes: 25 additions & 0 deletions e2e-tests/development-runtime/shared-data/slices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const allRecipes = [
{
id: "r1",
name: "Jollof Rice",
description:
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).",
authorId: "a-1",
},
{
id: "r2",
name: "Ewa Agoyin",
description:
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).",
authorId: "a-2",
},
]

const allRecipeAuthors = [
{ id: "a-1", name: "Jude" },
{ id: "a-2", name: "Ty" },
]

const framework = "Gatsby"

module.exports = { allRecipes, allRecipeAuthors, framework }
13 changes: 7 additions & 6 deletions e2e-tests/development-runtime/src/components/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import React, { useContext } from "react"
import { ContextForSlices } from "../context-for-slices"

// Use as a Slice
function Footer({ builtWith, builtWithLink }) {
const { posts } = {posts: {}}
function Footer({ framework, lang, sliceContext: { framework: frameworkViaContext }}) {
const { posts } = useContext(ContextForSlices)

return (
<footer
style={{
marginTop: `10px`,
fontSize: `12px`,
}}
>
© {new Date().getFullYear()} &middot; Built with
{` `}
<a href={builtWithLink}>{builtWith}</a>
{` Posts Count: ${posts.length}`}
<span data-testid="footer-slice-context-value">{frameworkViaContext}</span>
<span data-testid="footer-static-text">Built with {` `}</span>
<span data-testid="footer-props">{`${framework}${lang}`}</span>
{` `}Posts Count: <span data-testid="footer-context-derieved-value">{`${posts.length}`}</span>
</footer>
)
}
Expand Down
5 changes: 2 additions & 3 deletions e2e-tests/development-runtime/src/components/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ const Layout = ({ children }) => (
>
{children}
</div>
<Slice alias="footer" builtWith="Gatsby" builtWithLink="https://www.gatsbyjs.com"/>
<Slice alias="footer" framework="Gatsby" lang="js"/>

{/** The slice below doesn't exist but it shouldn't break build */}
<Slice alias="this-alias-does-not-exist" allowEmpty/>

{/** Insert this here and expect it to fail */}
<Slice alias="this-alias-does-not-exist-too"/>
{/** Insert this here and expect it to fail <Slice alias="this-alias-does-not-exist-too"/>*/}
</>
)}
/>
Expand Down
15 changes: 15 additions & 0 deletions e2e-tests/development-runtime/src/components/recipe-author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"

// Use as a Slice
function RecipeAuthor({ sliceContext: { name } }) {
return (
<div>
Written by{" "}
<span data-testid="recipe-author-name" style={{ fontWeight: "bold" }}>
{name}
</span>
</div>
)
}

export default RecipeAuthor
15 changes: 15 additions & 0 deletions e2e-tests/development-runtime/src/templates/recipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { Slice } from "gatsby"
import Layout from "../components/layout"

const Recipe = ({ pageContext: { description, name } }) => {
return (
<Layout>
<h1 data-testid="recipe-name">{name}</h1>
<p data-testid="recipe-description">{description}</p>
<Slice alias="author" />
</Layout>
)
}

export default Recipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe(`Slices`, () => {
beforeEach(() => {
cy.visit(`/`).waitForRouteChange()
})

it(`Slice content show on screen`, () => {
cy.getTestElement(`footer-static-text`)
.invoke(`text`)
.should(`contain`, `Built with`)
})

it(`Slice can take in props`, () => {
cy.getTestElement(`footer-props`)
.invoke(`text`)
.should(`contains`, `Gatsbyjs`)
})

it(`Slice can consume a context wrapped in WrapRootElement`, () => {
cy.getTestElement(`footer-context-derieved-value`)
.invoke(`text`)
.should(`contain`, `2`)
})
})

3 changes: 3 additions & 0 deletions e2e-tests/production-runtime/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export const createPages: GatsbyNode["createPages"] = ({
createSlice({
id: `footer`,
component: path.resolve(`./src/components/footer.js`),
context: {
framework: "Gatsby",
},
})

createPage({
Expand Down
13 changes: 7 additions & 6 deletions e2e-tests/production-runtime/src/components/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import React, { useContext } from "react"
import { ContextForSlices } from "../context-for-slices"

// Use as a Slice
function Footer({ builtWith, builtWithLink }) {
const { posts } = {posts: {}}
function Footer({ framework, lang, sliceContext: { framework: frameworkViaContext }}) {
const { posts } = useContext(ContextForSlices)

return (
<footer
style={{
marginTop: `10px`,
fontSize: `12px`,
}}
>
© {new Date().getFullYear()} &middot; Built with
{` `}
<a href={builtWithLink}>{builtWith}</a>
{` Posts Count: ${posts.length}`}
<span data-testid="footer-slice-context-value">{frameworkViaContext}</span>
<span data-testid="footer-static-text">Built with {` `}</span>
<span data-testid="footer-props">{`${framework}${lang}`}</span>
{` `}Posts Count: <span data-testid="footer-context-derieved-value">{`${posts.length}`}</span>
</footer>
)
}
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/production-runtime/src/components/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const Layout = ({ children }) => (
>
{children}
</div>
<Slice alias="footer" builtWith="Gatsby" builtWithLink="https://www.gatsbyjs.com"/>

<Slice alias="footer" framework="Gatsby" lang="js"/>
{/** The slice below doesn't exist but it shouldn't break build */}
<Slice alias="this-alias-does-not-exist" allowEmpty/>
</React.Fragment>
Expand Down

0 comments on commit 82d04e3

Please sign in to comment.