Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: e2e-tests

on:
push:
branches: [master]
pull_request:

env:
NODE_VERSION: 16.15.1

jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: yarn

- name: install dependencies
run: yarn install --frozen-lockfile

- name: run app in Docker
run: |
docker run \
--interactive --tty --rm --detach \
--name ultimate-react-hook-form-form \
--publish 3000:3000 \
--publish 4000:4000 \
--volume "$PWD":/app \
--workdir /app \
node:${{ env.NODE_VERSION }}-alpine3.15 \
yarn dev

- name: run Cypress tests
uses: cypress-io/github-action@v4
with:
install: false
wait-on: 'http://localhost:3000'
browser: chrome

- name: upload video captures
uses: actions/upload-artifact@v3
if: always()
with:
name: cypress-videos
path: cypress/videos
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# cypress
cypress/downloads
cypress/e2e/examples
cypress/videos
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.15.1
11 changes: 11 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { defineConfig } = require("cypress");

module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
excludeSpecPattern: 'cypress/e2e/examples/**',
baseUrl: 'http://localhost:3000/',
},
});
93 changes: 93 additions & 0 deletions cypress/e2e/default.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
describe('ultimate form', () => {
it('submits successfully with phone number', () => {
cy.visit('/')

// STEP 1
cy.get('h2').should('contain', 'Step 1')

cy.get('#firstName-helper-text').should('not.exist')
cy.get('#firstName').focus().blur()
cy.get('#firstName-helper-text').should('exist')

cy.get('#lastName-helper-text').should('not.exist')
cy.get('#lastName').focus().blur()
cy.get('#lastName-helper-text').should('exist')

// Submit shouldn't work.
cy.get('button').click()
cy.get('h2').should('contain', 'Step 1')

cy.get('#firstName').type('Maria').blur()
cy.get('#firstName-helper-text').should('not.exist')

// Submit shouldn't work.
cy.get('button').click()
cy.get('h2').should('contain', 'Step 1')

cy.get('#lastName').type('Sanchez').blur()
cy.get('#lastName-helper-text').should('not.exist')
cy.get('button').click()

// STEP 2
cy.url().should('contain', '/step2')
cy.get('h2').should('contain', 'Step 2')

cy.get('#email-helper-text').should('not.exist')
cy.get('#email').focus().blur()
cy.get('#email-helper-text').should('exist')

cy.get('#email').type('maria@example.com').blur()
cy.get('#email-helper-text').should('not.exist')

cy.get('#phoneNumber').should('not.exist')
cy.get('input[name=hasPhone]').check()
cy.get('#phoneNumber').should('exist')
cy.get('#phoneNumber').type('555-555-5555')
cy.get('button').click()

// STEP 3
cy.url().should('contain', '/step3')
cy.get('h2').should('contain', 'Step 3')
// Skip upload for now.
cy.get('button').click()

// RESULTS
cy.url().should('contain', '/result')
cy.get('h2').should('contain', 'Form Values')
cy.get('table').within(() => {
cy.contains('td', 'Maria').should('exist')
cy.contains('td', 'Sanchez').should('exist')
cy.contains('td', 'maria@example.com').should('exist')
cy.contains('td', '555-555-5555').should('exist')
})
cy.get('button').click()

cy.get('[role=dialog]').within(() => {
cy.get('h2').should('contain', 'Great job!')
cy.contains('button', 'OK').click()
})
})

it('submits successfully without phone number', () => {
// STEP 1
cy.visit('/')
cy.get('#firstName').type('Maria')
cy.get('#lastName').type('Sanchez')
cy.get('button').click()

// STEP 2
cy.get('#email').type('maria@example.com')
cy.get('input[name=hasPhone]').uncheck()
cy.get('button').click()

// STEP 3 (skip)
cy.get('button').click()

// RESULTS
cy.get('table').within(() => {
cy.contains('td', 'Maria').should('exist')
cy.contains('td', 'Sanchez').should('exist')
cy.contains('td', 'maria@example.com').should('exist')
})
})
})
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"
}
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// 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/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.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')
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"concurrently": "^5.2.0",
"cors": "^2.8.5",
"cypress": "^10.3.0",
"express": "^4.17.1",
"express-fileupload": "^1.1.7-alpha.3",
"morgan": "^1.10.0"
Expand All @@ -33,6 +34,8 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"e2e:run": "cypress run",
"e2e:open": "cypress open",
"eject": "react-scripts eject",
"start:server": "node ./server.js",
"dev": "concurrently --kill-others \"npm run start:server\" \"npm run start\""
Expand Down
4 changes: 2 additions & 2 deletions src/Step1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useHistory } from "react-router-dom";
import { useData } from "./DataContext";
import Typography from "@material-ui/core/Typography";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { yupResolver } from "@hookform/resolvers";
import { PrimaryButton } from "./components/PrimaryButton";
import { MainContainer } from "./components/MainContainer";
import { Form } from "./components/Form";
Expand Down Expand Up @@ -42,7 +42,7 @@ export const Step1 = () => {
</Typography>
<Form onSubmit={handleSubmit(onSubmit)}>
<Input
{...register('parentName')}
ref={register}
id="firstName"
type="text"
label="First Name"
Expand Down
Loading