Skip to content

Commit 30e22a1

Browse files
committed
initial
1 parent 275b356 commit 30e22a1

File tree

22 files changed

+394
-13
lines changed

22 files changed

+394
-13
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { allRecipes, allRecipeAuthors } from "../../../shared-data/slices"
2+
3+
/**
4+
* Test behaviour when a slice is created and passed `slices` option to createPage
5+
*/
6+
7+
describe("Slice passed via createPage", () => {
8+
it("Pages created with slices mapping have correct content", () => {
9+
allRecipes.forEach(recipe => {
10+
cy.visit(`recipe/${recipe.id}`).waitForRouteChange()
11+
12+
cy.getTestElement(`recipe-name`)
13+
.invoke(`text`)
14+
.should(`contain`, recipe.name)
15+
16+
cy.getTestElement(`recipe-description`)
17+
.invoke(`text`)
18+
.should(`contain`, recipe.description)
19+
20+
cy.getTestElement(`recipe-author-name`)
21+
.invoke(`text`)
22+
.should(`contain`, allRecipeAuthors.find(author => recipe.authorId === author.id).name)
23+
})
24+
})
25+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Test basic Slices API behaviour like context, props, ....
3+
*/
4+
5+
describe(`Slices`, () => {
6+
beforeEach(() => {
7+
cy.visit(`/`).waitForRouteChange()
8+
})
9+
10+
it(`Slice content show on screen`, () => {
11+
cy.getTestElement(`footer-static-text`)
12+
.invoke(`text`)
13+
.should(`contain`, `Built with`)
14+
})
15+
16+
it(`Slice recieves context passed via createSlice`, () => {
17+
cy.getTestElement(`footer-slice-context-value`)
18+
.invoke(`text`)
19+
.should(`contain`, `Gatsby`)
20+
})
21+
22+
it(`Slice can take in props`, () => {
23+
cy.getTestElement(`footer-props`)
24+
.invoke(`text`)
25+
.should(`contains`, `Gatsbyjs`)
26+
})
27+
28+
it(`Slice can consume a context wrapped in WrapRootElement`, () => {
29+
cy.getTestElement(`footer-context-derieved-value`)
30+
.invoke(`text`)
31+
.should(`contain`, `2`)
32+
})
33+
})

e2e-tests/development-runtime/gatsby-node.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require(`path`)
22
const { createFilePath } = require(`gatsby-source-filesystem`)
33
const headFunctionExportSharedData = require("./shared-data/head-function-export")
4+
const slicesData = require("./shared-data/slices")
45
const {
56
addRemoteFilePolyfillInterface,
67
polyfillImageServiceDevRoutes,
@@ -37,7 +38,8 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
3738
const items = [
3839
{
3940
name: "photoA.jpg",
40-
url: "https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
41+
url:
42+
"https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
4143
placeholderUrl:
4244
"https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=%width%&h=%height%",
4345
mimeType: "image/jpg",
@@ -47,15 +49,17 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
4749
},
4850
{
4951
name: "photoB.jpg",
50-
url: "https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&h=2000&q=10",
52+
url:
53+
"https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&h=2000&q=10",
5154
mimeType: "image/jpg",
5255
filename: "photo-1552053831.jpg",
5356
width: 1247,
5457
height: 2000,
5558
},
5659
{
5760
name: "photoC.jpg",
58-
url: "https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
61+
url:
62+
"https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
5963
placeholderUrl:
6064
"https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=%width%&h=%height%",
6165
mimeType: "image/jpg",
@@ -122,7 +126,7 @@ exports.onCreateNode = function onCreateNode({
122126
* @type {import('gatsby').createPages}
123127
*/
124128
exports.createPages = async function createPages({
125-
actions: { createPage, createRedirect },
129+
actions: { createPage, createRedirect, createSlice },
126130
graphql,
127131
}) {
128132
const { data } = await graphql(`
@@ -172,6 +176,41 @@ exports.createPages = async function createPages({
172176
})
173177
})
174178

179+
//-------------------------Slice API----------------------------
180+
createSlice({
181+
id: `footer`,
182+
component: path.resolve(`./src/components/footer.js`),
183+
context: {
184+
framework: slicesData.framework,
185+
},
186+
})
187+
188+
slicesData.allRecipeAuthors.forEach(({ id, name }) => {
189+
createSlice({
190+
id: `author-${id}`,
191+
component: path.resolve(`./src/components/recipe-author.js`),
192+
context: {
193+
name,
194+
id,
195+
},
196+
})
197+
})
198+
199+
slicesData.allRecipes.forEach(({ authorId, id, name, description }) => {
200+
createPage({
201+
path: `/recipe/${id}`,
202+
component: path.resolve(`./src/templates/recipe.js`),
203+
context: {
204+
description: description,
205+
name,
206+
},
207+
slices: {
208+
author: `author-${authorId}`,
209+
},
210+
})
211+
})
212+
//---------------------------------------------------------------
213+
175214
createPage({
176215
path: `/안녕`,
177216
component: path.resolve(`src/pages/page-2.js`),

e2e-tests/development-runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"author": "Dustin Schau <[email protected]>",
66
"dependencies": {
77
"babel-plugin-search-and-replace": "^1.1.0",
8-
"gatsby": "next",
8+
"gatsby": "^4.23.0-alpha-preview-slices.78",
99
"gatsby-image": "next",
1010
"gatsby-plugin-image": "next",
1111
"gatsby-plugin-less": "next",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const allRecipes = [
2+
{
3+
id: "r1",
4+
name: "Jollof Rice",
5+
description:
6+
"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).",
7+
authorId: "a-1",
8+
},
9+
{
10+
id: "r2",
11+
name: "Ewa Agoyin",
12+
description:
13+
"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).",
14+
authorId: "a-2",
15+
},
16+
]
17+
18+
const allRecipeAuthors = [
19+
{ id: "a-1", name: "Jude" },
20+
{ id: "a-2", name: "Ty" },
21+
]
22+
23+
const framework = "Gatsby"
24+
25+
module.exports = { allRecipes, allRecipeAuthors, framework }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { useContext } from "react"
2+
import { ContextForSlices } from "../context-for-slices"
3+
4+
// Use as a Slice
5+
function Footer({ framework, lang, sliceContext: { framework: frameworkViaContext }}) {
6+
const { posts } = useContext(ContextForSlices)
7+
8+
return (
9+
<footer
10+
style={{
11+
marginTop: `10px`,
12+
fontSize: `12px`,
13+
}}
14+
>
15+
<span data-testid="footer-slice-context-value">{frameworkViaContext}</span>
16+
<span data-testid="footer-static-text">Built with {` `}</span>
17+
<span data-testid="footer-props">{`${framework}${lang}`}</span>
18+
{` `}Posts Count: <span data-testid="footer-context-derieved-value">{`${posts.length}`}</span>
19+
</footer>
20+
)
21+
}
22+
23+
export default Footer

e2e-tests/development-runtime/src/components/layout.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
22
import PropTypes from "prop-types"
3-
import { StaticQuery, graphql } from "gatsby"
3+
import { StaticQuery, graphql, Slice } from "gatsby"
44

55
import Header from "./header"
66
import "./layout.css"
@@ -29,6 +29,12 @@ const Layout = ({ children }) => (
2929
>
3030
{children}
3131
</div>
32+
<Slice alias="footer" framework="Gatsby" lang="js"/>
33+
34+
{/** The slice below doesn't exist but it shouldn't break build */}
35+
<Slice alias="this-alias-does-not-exist" allowEmpty/>
36+
37+
{/** Insert this here and expect it to fail <Slice alias="this-alias-does-not-exist-too"/>*/}
3238
</>
3339
)}
3440
/>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from "react"
2+
3+
// Use as a Slice
4+
function RecipeAuthor({ sliceContext: { name } }) {
5+
return (
6+
<div>
7+
Written by{" "}
8+
<span data-testid="recipe-author-name" style={{ fontWeight: "bold" }}>
9+
{name}
10+
</span>
11+
</div>
12+
)
13+
}
14+
15+
export default RecipeAuthor
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from "react"
2+
3+
const ContextForSlices = React.createContext()
4+
5+
const ContextForSlicesProvider = ({ children }) => {
6+
const contextValue = {
7+
posts: [
8+
{
9+
title: "My first blog post",
10+
content: "This is my first blog post",
11+
},
12+
{
13+
title: "My second blog post",
14+
content: "This is my second blog post",
15+
},
16+
],
17+
}
18+
return (
19+
<ContextForSlices.Provider value={contextValue}>
20+
{children}
21+
</ContextForSlices.Provider>
22+
)
23+
}
24+
25+
export { ContextForSlices, ContextForSlicesProvider }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from "react"
2+
import { Slice } from "gatsby"
3+
import Layout from "../components/layout"
4+
5+
const Recipe = ({ pageContext: { description, name } }) => {
6+
return (
7+
<Layout>
8+
<h1 data-testid="recipe-name">{name}</h1>
9+
<p data-testid="recipe-description">{description}</p>
10+
<Slice alias="author" />
11+
</Layout>
12+
)
13+
}
14+
15+
export default Recipe

0 commit comments

Comments
 (0)