Skip to content

Commit cf19bb6

Browse files
author
Kent C. Dodds
authored
add cypress
* update all the things * add server * pretty darn close to something useful * add install:server * i dunno * simplify * stuff * cleanup * cypress is ready * update deps and things * fixup everything that is left hopefully
1 parent 7426afc commit cf19bb6

29 files changed

+6352
-5162
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module.exports = {
88
'kentcdodds/jest',
99
'kentcdodds/react',
1010
],
11+
plugins: ['eslint-plugin-cypress'],
12+
env: {'cypress/globals': true},
1113
overrides: [
1214
{
1315
files: ['**/__tests__/**'],

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ cache:
77
notifications:
88
email: false
99
node_js: '8'
10-
install: npm install
11-
script: npm run validate
10+
install: echo "install happens as part of setup"
11+
script: npm run setup
1212
after_script: npx codecov@3
1313
branches:
1414
only: master

cypress.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"baseUrl": "http://localhost:8080",
33
"integrationFolder": "cypress/e2e",
4-
"viewportHeight": 700,
4+
"viewportHeight": 900,
55
"viewportWidth": 400
66
}

cypress/e2e/calculator.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/* globals cy */
2-
describe('calculator', () => {
3-
it('can visit the app', () => {
1+
describe('anonymous calculator', () => {
2+
it('can make calculations', () => {
43
cy.visit('/')
54
.getByText(/^1$/)
65
.click()
@@ -10,5 +9,21 @@ describe('calculator', () => {
109
.click()
1110
.getByText(/^=$/)
1211
.click()
12+
.getByTestId('total')
13+
.should('have.text', '3')
14+
})
15+
})
16+
17+
describe('authenticated calculator', () => {
18+
it('displays the username', () => {
19+
cy.loginAsNewUser().then(user => {
20+
cy.visit('/')
21+
.getByTestId('username-display')
22+
.should('have.text', user.username)
23+
.getByText(/logout/i)
24+
.click()
25+
.get('[data=username-display]')
26+
.should('not.exist')
27+
})
1328
})
1429
})

cypress/e2e/login.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
describe('login', () => {
2+
it('should login an existing user', () => {
3+
cy.createUser().then(user => {
4+
cy.visit('/')
5+
.getByText(/login/i)
6+
.click()
7+
.getByLabelText(/username/i)
8+
.type(user.username)
9+
.getByLabelText(/password/i)
10+
.type(user.password)
11+
.getByText(/submit/i)
12+
.click()
13+
})
14+
})
15+
})

cypress/e2e/register.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {userBuilder} from '../support/generate'
2+
3+
describe('registration', () => {
4+
it('should register a new user', () => {
5+
const user = userBuilder()
6+
cy.visit('/')
7+
.getByText(/register/i)
8+
.click()
9+
.getByLabelText(/username/i)
10+
.type(user.username)
11+
.getByLabelText(/password/i)
12+
.type(user.password)
13+
.getByText(/submit/i)
14+
.click()
15+
})
16+
17+
it(`should show an error message if there's an error registering`, () => {
18+
cy.server()
19+
cy.route({
20+
method: 'POST',
21+
url: 'http://localhost:3000/register',
22+
status: 500,
23+
response: {},
24+
})
25+
cy.visit('/register')
26+
.getByText(/submit/i)
27+
.click()
28+
.getByText(/error.*try again/i)
29+
})
30+
})

cypress/support/commands.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
// ***********************************************
2-
// This example commands.js shows you how to
3-
// create various custom commands and overwrite
4-
// existing commands.
5-
//
6-
// For more comprehensive examples of custom
7-
// commands please read more here:
8-
// https://on.cypress.io/custom-commands
9-
// ***********************************************
10-
//
11-
//
12-
// -- This is a parent command --
13-
// Cypress.Commands.add("login", (email, password) => { ... })
14-
//
15-
//
16-
// -- This is a child command --
17-
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
18-
//
19-
//
20-
// -- This is a dual command --
21-
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
22-
//
23-
//
24-
// -- This is will overwrite an existing command --
25-
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
1+
import {userBuilder} from './generate'
2+
3+
Cypress.Commands.add('createUser', overrides => {
4+
const user = userBuilder(overrides)
5+
return cy
6+
.request({
7+
url: 'http://localhost:3000/register',
8+
method: 'POST',
9+
body: user,
10+
})
11+
.then(({body}) => body.user)
12+
})
13+
14+
Cypress.Commands.add('login', user => {
15+
return cy
16+
.request({
17+
url: 'http://localhost:3000/login',
18+
method: 'POST',
19+
body: user,
20+
})
21+
.then(({body}) => {
22+
window.localStorage.setItem('token', body.user.token)
23+
return body.user
24+
})
25+
})
26+
27+
Cypress.Commands.add('loginAsNewUser', () => {
28+
cy.createUser().then(user => {
29+
cy.login(user)
30+
})
31+
})

cypress/support/generate.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {build, fake} from 'test-data-bot'
2+
3+
const userBuilder = build('User').fields({
4+
username: fake(f => f.internet.userName()),
5+
password: fake(f => f.internet.password()),
6+
})
7+
8+
export {userBuilder}

cypress/support/index.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
// ***********************************************************
2-
// This example support/index.js is processed and
3-
// loaded automatically before your test files.
4-
//
5-
// This is a great place to put global configuration and
6-
// behavior that modifies Cypress.
7-
//
8-
// You can change the location of this file or turn off
9-
// automatically serving support files with the
10-
// 'supportFile' configuration option.
11-
//
12-
// You can read more here:
13-
// https://on.cypress.io/configuration
14-
// ***********************************************************
15-
16-
// Import commands.js using ES2015 syntax:
171
import './commands'
182
import 'cypress-testing-library/add-commands'
19-
20-
// Alternatively you can use CommonJS syntax:
21-
// require('./commands')

jest.config.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
module.exports = {
22
...require('./test/jest-common'),
3-
collectCoverageFrom: ['**/src/**/*.js'],
3+
collectCoverageFrom: [
4+
'**/src/**/*.js',
5+
'!**/__tests__/**',
6+
'!**/__server_tests__/**',
7+
'!**/node_modules/**',
8+
],
49
coverageThreshold: {
510
global: {
6-
statements: 13,
11+
statements: 10,
712
branches: 5,
8-
functions: 17,
9-
lines: 13,
13+
functions: 15,
14+
lines: 8,
1015
},
1116
'./src/shared/utils.js': {
1217
statements: 100,
@@ -15,14 +20,10 @@ module.exports = {
1520
lines: 100,
1621
},
1722
},
18-
coveragePathIgnorePatterns: [
19-
'/node_modules/',
20-
'/__tests__/',
21-
'__server_tests__/',
22-
],
2323
projects: [
2424
'./test/jest.lint.js',
2525
'./test/jest.client.js',
2626
'./test/jest.server.js',
27+
'./server',
2728
],
2829
}

0 commit comments

Comments
 (0)