Skip to content

Commit

Permalink
chore(themes): Add some basic end to end tests for themes (gatsbyjs#1…
Browse files Browse the repository at this point in the history
…5637)

* wip

* Fix component shadowing test

* Clean up linting issues

* Remove development runtime stuff

* Lint

* Fix tests and add to CI config

* Add e2e_tests_themes job

* Fix test path
  • Loading branch information
sidharthachatterjee authored and GatsbyJS Bot committed Jul 12, 2019
1 parent 24e3fac commit 26550a3
Show file tree
Hide file tree
Showing 26 changed files with 297 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ jobs:
test_path: e2e-tests/production-runtime
test_command: yarn test && yarn test:offline

e2e_tests_themes:
<<: *e2e-executor
steps:
- e2e-test:
test_path: e2e-tests/themes/production-runtime
test_command: yarn test

starters_validate:
executor: node
steps:
Expand Down Expand Up @@ -245,6 +252,8 @@ workflows:
<<: *e2e-test-workflow
- e2e_tests_production_runtime:
<<: *e2e-test-workflow
- e2e_tests_themes:
<<: *e2e-test-workflow
- starters_validate:
<<: *ignore_master
- starters_publish:
Expand Down
3 changes: 3 additions & 0 deletions e2e-tests/themes/gatsby-theme-about/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# gatsby-theme-about

A Gatsby theme that adds an About page
15 changes: 15 additions & 0 deletions e2e-tests/themes/gatsby-theme-about/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
siteMetadata: {
title: `Blog Title Placeholder`,
author: `Name Placeholder`,
description: `Description placeholder`,
},
plugins: [
{
resolve: `gatsby-plugin-page-creator`,
options: {
path: `${__dirname}/src/pages`,
},
},
],
}
1 change: 1 addition & 0 deletions e2e-tests/themes/gatsby-theme-about/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
32 changes: 32 additions & 0 deletions e2e-tests/themes/gatsby-theme-about/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "gatsby-theme-about",
"version": "1.0.0",
"description": "A Gatsby theme that adds an About page",
"main": "index.js",
"keywords": [
"gatsby",
"gatsby-theme",
"gatsby-plugin",
"react",
"blog"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby.git",
"directory": "e2e-tests/themes/gatsby-theme-about"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/e2e-tests/themes/gatsby-theme-about#readme",
"dependencies": {
"gatsby": "^2.13.13",
"gatsby-plugin-page-creator": "^2.1.3"
},
"devDependencies": {
"prettier": "^1.18.2"
},
"peerDependencies": {
"gatsby": "^2.13.13",
"react": "^16.8.6",
"react-dom": "^16.8.6"
}
}
10 changes: 10 additions & 0 deletions e2e-tests/themes/gatsby-theme-about/src/components/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react"

import Time from "./time"

export default ({ date }) => (
<div>
<span data-testid="date">{date.toLocaleString(`en-US`)}</span>
<Time date={date} />
</div>
)
5 changes: 5 additions & 0 deletions e2e-tests/themes/gatsby-theme-about/src/components/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react"

export default ({ date }) => (
<span data-testid="time">{date.toTimeString()}</span>
)
16 changes: 16 additions & 0 deletions e2e-tests/themes/gatsby-theme-about/src/pages/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react"
import { graphql } from "gatsby"

export default ({ data }) => (
<h1 data-testid="title">{data.site.siteMetadata.title}</h1>
)

export const pageQuery = graphql`
query {
site {
siteMetadata {
title
}
}
}
`
16 changes: 16 additions & 0 deletions e2e-tests/themes/gatsby-theme-about/src/pages/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react"
import { graphql } from "gatsby"

export default ({ data }) => (
<p data-testid="description">{data.site.siteMetadata.description}</p>
)

export const pageQuery = graphql`
query {
site {
siteMetadata {
description
}
}
}
`
7 changes: 7 additions & 0 deletions e2e-tests/themes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"workspaces": [
"gatsby-theme-about",
"production-runtime"
]
}
12 changes: 12 additions & 0 deletions e2e-tests/themes/production-runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Project dependencies
.cache
node_modules
yarn-error.log

# Build directory
/public
.DS_Store

# Cypress output
cypress/videos/
cypress/screenshots/
3 changes: 3 additions & 0 deletions e2e-tests/themes/production-runtime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# production-runtime

A Gatsby project to test our production runtime for Themes
4 changes: 4 additions & 0 deletions e2e-tests/themes/production-runtime/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"baseUrl": "http://localhost:9000",
"chromeWebSecurity": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* global cy */

describe(`Components`, () => {
it(`can be added by themes`, () => {
cy.visit(`/shadowed`).waitForRouteChange()
cy.getTestElement(`date`).contains(`6/28/1993`)
})
it(`added by themes can be shadowed`, () => {
cy.visit(`/shadowed`).waitForRouteChange()
cy.getTestElement(`time`).contains(`2:39:07 PM`)
})
})
14 changes: 14 additions & 0 deletions e2e-tests/themes/production-runtime/cypress/integration/pages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* global cy */

describe(`Pages`, () => {
it(`can be added by themes`, () => {
cy.visit(`/about`).waitForRouteChange()
cy.getTestElement(`title`).contains(`Blog Title Placeholder`)
})
it(`added by themes can be shadowed`, () => {
cy.visit(`/contact`).waitForRouteChange()
cy.getTestElement(`title`).contains(
`A title since the theme didn't have one`
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* global cy */

describe(`Site Metadata`, () => {
it(`can be added by themes`, () => {
cy.visit(`/`).waitForRouteChange()
cy.getTestElement(`title`).contains(`Blog Title Placeholder`)
})
it(`added by themes can be overridden`, () => {
cy.visit(`/`).waitForRouteChange()
cy.getTestElement(`author`).contains(`Sidhartha Chatterjee`)
})
})
17 changes: 17 additions & 0 deletions e2e-tests/themes/production-runtime/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "gatsby-cypress"
24 changes: 24 additions & 0 deletions e2e-tests/themes/production-runtime/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if (typeof window !== `undefined`) {
window.___PageComponentLifecycleCallsLog = []
}

const addLogEntry = (action, location) => {
const idElement = document.querySelector(`[data-testid="dom-marker"]`)
window.___PageComponentLifecycleCallsLog.push({
action,
pathname: location.pathname,
domContent: idElement ? idElement.innerText : null,
})
}

exports.onPreRouteUpdate = ({ location }) => {
addLogEntry(`onPreRouteUpdate`, location)
}

exports.onRouteUpdate = ({ location }) => {
addLogEntry(`onRouteUpdate`, location)
}

exports.onPrefetchPathname = ({ pathname }) => {
addLogEntry(`onPrefetchPathname`, pathname)
}
6 changes: 6 additions & 0 deletions e2e-tests/themes/production-runtime/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
siteMetadata: {
author: `Sidhartha Chatterjee`,
},
plugins: [`gatsby-theme-about`],
}
35 changes: 35 additions & 0 deletions e2e-tests/themes/production-runtime/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "production-runtime",
"description": "Gatsby default starter",
"version": "1.0.0",
"author": "Sidhartha Chatterjee <sid@gatsbyjs.com>",
"dependencies": {
"gatsby": "^2.13.14",
"react": "^16.8.0",
"react-dom": "^16.8.0",
"gatsby-theme-about": "*"
},
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"src/**/*.js\"",
"serve": "gatsby serve",
"start": "npm run develop",
"test": "cross-env CYPRESS_SUPPORT=y npm run build && npm run start-server-and-test",
"start-server-and-test": "start-server-and-test serve http://localhost:9000 cy:run",
"cy:open": "cypress open",
"cy:run": "cypress run --browser chrome"
},
"devDependencies": {
"cross-env": "^5.2.0",
"cypress": "^3.1.3",
"gatsby-cypress": "^0.1.7",
"prettier": "^1.15.2",
"start-server-and-test": "^1.7.11"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react"

export default ({ date }) => (
<span data-testid="time">{date.toLocaleTimeString(`en-US`)}</span>
)
5 changes: 5 additions & 0 deletions e2e-tests/themes/production-runtime/src/pages/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react"

export default () => (
<h1 data-testid="title">A title since the theme didn't have one</h1>
)
20 changes: 20 additions & 0 deletions e2e-tests/themes/production-runtime/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react"
import { graphql } from "gatsby"

export default ({ data }) => (
<>
<p data-testid="title">{data.site.siteMetadata.title}</p>
<p data-testid="author">{data.site.siteMetadata.author}</p>
</>
)

export const pageQuery = graphql`
query {
site {
siteMetadata {
title
author
}
}
}
`
8 changes: 8 additions & 0 deletions e2e-tests/themes/production-runtime/src/pages/shadowed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react"

import FormattedDate from "gatsby-theme-about/src/components/date"

export default () => {
const date = new Date(`June 28, 1993 14:39:07`)
return <FormattedDate date={date} />
}

0 comments on commit 26550a3

Please sign in to comment.