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.
feat: add fetch polyfill if needed (#348)
- Loading branch information
Showing
9 changed files
with
135 additions
and
3 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
2 changes: 1 addition & 1 deletion
2
...ess/component/basic/network/users-spec.js → ...s/component/basic/network/1-users-spec.js
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
File renamed without changes.
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,67 @@ | ||
/// <reference types="cypress" /> | ||
/// <reference types="../../lib" /> | ||
import { Users } from './2-users-fetch.jsx' | ||
import React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
|
||
describe('Users with Fetch', () => { | ||
it('fetches 3 users from remote API', () => { | ||
mount(<Users />) | ||
// fetching users can take a while | ||
cy.get('li', { timeout: 20000 }).should('have.length', 3) | ||
}) | ||
|
||
// https://github.com/bahmutov/cypress-react-unit-test/issues/347 | ||
context('mocking', () => { | ||
beforeEach(() => { | ||
cy.server() | ||
// mount the component after defining routes in tests | ||
// preventing race conditions where you wait on untouched routes | ||
}) | ||
|
||
it('can inspect real data from the server', () => { | ||
// spy on the request | ||
cy.route('/users?_limit=3').as('users') | ||
mount(<Users />) | ||
cy.wait('@users') | ||
.its('response.body') | ||
.should('have.length', 3) | ||
.its('0') | ||
.should('include.keys', ['id', 'name', 'username', 'email']) | ||
}) | ||
|
||
it('can stub and display mock network response', () => { | ||
const users = [{ id: 1, name: 'foo' }] | ||
// stub the request | ||
cy.route('GET', '/users?_limit=3', users).as('users') | ||
mount(<Users />) | ||
cy.get('li') | ||
.should('have.length', 1) | ||
.first() | ||
.contains('foo') | ||
}) | ||
|
||
it('can inspect mocked network reaponse', () => { | ||
const users = [{ id: 1, name: 'foo' }] | ||
cy.route('GET', '/users?_limit=3', users).as('users') | ||
mount(<Users />) | ||
cy.wait('@users') | ||
.its('response.body') | ||
.should('deep.equal', users) | ||
}) | ||
|
||
it('can delay and wait on Ajax call', () => { | ||
const users = [{ id: 1, name: 'foo' }] | ||
cy.route({ | ||
method: 'GET', | ||
url: '/users?_limit=3', | ||
response: users, | ||
delay: 1000, | ||
}).as('users') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react' | ||
|
||
export class Users extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
users: [], | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
fetch('https://jsonplaceholder.cypress.io/users?_limit=3') | ||
.then(response => { | ||
return response.json() | ||
}) | ||
.then(list => { | ||
this.setState({ | ||
users: list, | ||
}) | ||
}) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
{this.state.users.map(user => ( | ||
<li key={user.id}> | ||
<strong>{user.id}</strong> - {user.name} | ||
</li> | ||
))} | ||
</div> | ||
) | ||
} | ||
} |
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,4 @@ | ||
# Mocking network | ||
|
||
- [1-users-spec.js](1-users-spec.js) tests [1-users.jsx](1-users.jsx) that uses Axios to GET a list of users. Axios uses XMLHttpRequest to receive the data | ||
- [2-users-fetch-spec.js](2-users-fetch-spec.js) tests [2-users-fetch.jsx](2-users-fetch.jsx) that uses `fetch` directly, assuming `"experimentalFetchPolyfill": true` in `cypress.json`, read [Experimental Fetch Polyfill](https://www.cypress.io/blog/2020/06/29/experimental-fetch-polyfill/) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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