This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break: use Cypress commands and aliases, version bumps and module loa…
…ding tweaks (#37) BREAKING CHANGE: how components are mounted and accessed * Fixed users spec - had race condition * Drafted cypress commands * Lib functions as commands. Use node_modules for loading React * Switch over to using cy.mount. Docs updated * Version bumping incl. babel@7 * Added error boundaries - good example component * Line about err boundary component
- Loading branch information
Showing
16 changed files
with
1,749 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"presets" : ["es2015", "react"] | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-react" | ||
], | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties" | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ErrorBoundary } from '../../src/error-boundary.jsx' | ||
import React from 'react' | ||
|
||
/* eslint-env mocha */ | ||
describe('Error Boundary', () => { | ||
const errorMessage = 'I crashed!' | ||
const ChildWithoutError = () => <h1>Normal Child</h1> | ||
const ChildWithError = () => { | ||
throw new Error(errorMessage) | ||
return null | ||
} | ||
|
||
it('renders normal children', () => { | ||
cy.mount( | ||
<ErrorBoundary> | ||
<ChildWithoutError /> | ||
</ErrorBoundary> | ||
) | ||
cy.get('h1') | ||
.should('have.text', 'Normal Child') | ||
cy.get('@Component') | ||
.its('state.error') | ||
.should('not.exist') | ||
}) | ||
|
||
it('on error, display fallback UI', () => { | ||
cy.mount( | ||
<ErrorBoundary> | ||
<ChildWithError /> | ||
</ErrorBoundary> | ||
) | ||
cy.get('header h1') | ||
.should('contain', 'Something went wrong.') | ||
cy.get('header h3') | ||
.should('contain', 'failed to load') | ||
cy.get('@Component') | ||
.its('state.error.message') | ||
.should('equal', errorMessage) | ||
cy.get('@Component') | ||
.its('state.error.stack') | ||
.should('contain', 'ChildWithError') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { HelloWorld } from '../../src/hello-world.jsx' | ||
import React from 'react' | ||
import { mount } from '../../lib' | ||
|
||
/* eslint-env mocha */ | ||
describe('HelloWorld component', () => { | ||
it('works', () => { | ||
mount(<HelloWorld />) | ||
cy.mount(<HelloWorld />) | ||
cy.contains('Hello World!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
import { HelloX, HelloState } from '../../src/hello-x.jsx' | ||
import React from 'react' | ||
import { mount } from '../../lib' | ||
|
||
/* eslint-env mocha */ | ||
describe('HelloX component', () => { | ||
it('works', () => { | ||
mount(<HelloX name="SuperMan" />) | ||
cy.mount(<HelloX name="SuperMan" />) | ||
cy.contains('Hello SuperMan!') | ||
}) | ||
}) | ||
|
||
describe('HelloState component', () => { | ||
it('changes state', () => { | ||
mount(<HelloState />) | ||
cy.mount(<HelloState />) | ||
cy.contains('Hello Spider-man!') | ||
Cypress.component().invoke('setState', {name: 'React'}) | ||
Cypress.component().its('state').should('deep.equal', { | ||
name: 'React' | ||
}) | ||
const stateToSet = { name: 'React' } | ||
cy.get('@Component') | ||
.invoke('setState', stateToSet) | ||
cy.get('@Component') | ||
.its('state') | ||
.should('deep.equal', stateToSet) | ||
cy.contains('Hello React!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,56 @@ | ||
import { Users } from '../../src/users.jsx' | ||
import React from 'react' | ||
import { mount } from '../../lib' | ||
|
||
/* eslint-env mocha */ | ||
describe('Users', () => { | ||
beforeEach(() => { | ||
mount(<Users />) | ||
context('Users', () => { | ||
describe('Component', () => { | ||
it('fetches 3 users from remote API', () => { | ||
cy.mount(<Users />) | ||
cy.get('li').should('have.length', 3) | ||
}) | ||
}) | ||
|
||
it('fetches 3 users from remote API', () => { | ||
cy.get('li').should('have.length', 3) | ||
}) | ||
describe('Network State', () => { | ||
beforeEach(() => { | ||
cy.server() | ||
// cy.mount the component after defining routes in tests | ||
// preventing race conditions where you wait on untouched routes | ||
}) | ||
|
||
it('can inspect real data in XHR', () => { | ||
cy.server() | ||
cy.route('/users?_limit=3').as('users') | ||
cy.wait('@users').its('response.body').should('have.length', 3) | ||
}) | ||
|
||
it('can display mock XHR response', () => { | ||
cy.server() | ||
const users = [{id: 1, name: 'foo'}] | ||
cy.route('GET', '/users?_limit=3', users).as('users') | ||
cy.get('li').should('have.length', 1) | ||
.first().contains('foo') | ||
}) | ||
it('can inspect real data in XHR', () => { | ||
cy.route('/users?_limit=3').as('users') | ||
cy.mount(<Users />) | ||
cy.wait('@users').its('response.body').should('have.length', 3) | ||
}) | ||
|
||
it('can inspect mocked XHR', () => { | ||
cy.server() | ||
const users = [{id: 1, name: 'foo'}] | ||
cy.route('GET', '/users?_limit=3', users).as('users') | ||
cy.wait('@users').its('response.body').should('deep.equal', users) | ||
}) | ||
it('can display mock XHR response', () => { | ||
const users = [{id: 1, name: 'foo'}] | ||
cy.route('GET', '/users?_limit=3', users).as('users') | ||
cy.mount(<Users />) | ||
cy.get('li').should('have.length', 1) | ||
.first().contains('foo') | ||
}) | ||
|
||
it('can inspect mocked XHR', () => { | ||
const users = [{id: 1, name: 'foo'}] | ||
cy.route('GET', '/users?_limit=3', users).as('users') | ||
cy.mount(<Users />) | ||
cy.wait('@users').its('response.body').should('deep.equal', users) | ||
}) | ||
|
||
it('can delay and wait on XHR', () => { | ||
cy.server() | ||
const users = [{id: 1, name: 'foo'}] | ||
cy.route({ | ||
method: 'GET', | ||
url: '/users?_limit=3', | ||
response: users, | ||
delay: 1000 | ||
}).as('users') | ||
cy.get('li').should('have.length', 0) | ||
cy.wait('@users') | ||
cy.get('li').should('have.length', 1) | ||
it('can delay and wait on XHR', () => { | ||
const users = [{id: 1, name: 'foo'}] | ||
cy.route({ | ||
method: 'GET', | ||
url: '/users?_limit=3', | ||
response: users, | ||
delay: 1000 | ||
}).as('users') | ||
cy.mount(<Users />) | ||
cy.get('li').should('have.length', 0) | ||
cy.wait('@users') | ||
cy.get('li').should('have.length', 1) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.