Skip to content

Commit

Permalink
Add cypress and test adding an item
Browse files Browse the repository at this point in the history
- Also add eslint plugin for cypress and @testin-library/cypress bindings
This test is skipped since it mutates the shared app state—running it adds the same item over and over.
We'll need to add delete functionality, create a test user, and/or stub the firebase network request to have a solid test.
Otherwise, we'll constantly need to login to the firebase console to delete the test data.
  • Loading branch information
Steve Gardner committed Dec 22, 2019
1 parent b6e6866 commit cef838e
Show file tree
Hide file tree
Showing 9 changed files with 593 additions and 24 deletions.
4 changes: 4 additions & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"baseUrl": "http://localhost:3000",
"$schema": "https://on.cypress.io/cypress.schema.json"
}
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
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"
}
15 changes: 15 additions & 0 deletions cypress/integration/home.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
describe('Home page', function() {
// TODO: Get app into empty test state with test user or API call to delete avocados. Or stub the request.
it.skip('adds an item to the list', function() {
const itemName = 'avocados';

cy.visit('/');

cy.findByLabelText('Add Item:')
.type(itemName)
.findByText('Add Item')
.click()
.findByLabelText('shopping list')
.contains(itemName);
});
});
17 changes: 17 additions & 0 deletions 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
}
27 changes: 27 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import '@testing-library/cypress/add-commands';

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"format": "prettier --write 'src/**/*.{js,jsx,ts,tsx,json,css,scss,md}'"
"format": "prettier --write 'src/**/*.{js,jsx,ts,tsx,json,css,scss,md}'",
"cy:open": "cypress open"
},
"eslintConfig": {
"extends": "react-app"
"extends": [
"react-app",
"plugin:cypress/recommended"
]
},
"husky": {
"hooks": {
Expand All @@ -44,5 +48,10 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@testing-library/cypress": "^5.0.2",
"cypress": "^3.8.0",
"eslint-plugin-cypress": "^2.8.1"
}
}
2 changes: 1 addition & 1 deletion src/pages/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const List = () => {
<Loading />
) : (
<main>
<ul>
<ul aria-label="shopping list">
{data.map(item => (
<li key={item.id}>{item.name}</li>
))}
Expand Down
Loading

0 comments on commit cef838e

Please sign in to comment.