Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 2d1f758

Browse files
authored
fix: mixing api and component tests (#349)
1 parent ff4bdcc commit 2d1f758

File tree

8 files changed

+110
-2
lines changed

8 files changed

+110
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ plus a few smaller sanity specs in [cypress/component/basic](cypress/component/b
151151
<!-- prettier-ignore-start -->
152152
Spec | Description
153153
--- | ---
154+
[api-test](cypress/component/advanced/api-test) | Mix [REST api tests](https://glebbahmutov.com/blog/api-testing-with-sever-logs/) that use [cy-api](https://github.com/bahmutov/cy-api) with component tests
154155
[app-action-example](cypress/component/advanced/app-action-example) | App actions against components
155156
[context](cypress/component/advanced/context) | Confirms components that use React context feature work
156157
[custom-command](cypress/component/advanced/custom-command) | Wraps `mount` in a custom command for convenience
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# cy-api test example
2+
3+
Using component tests with [cy-api](https://github.com/bahmutov/cy-api) tests. Read [Black box API testing with server logs](https://glebbahmutov.com/blog/api-testing-with-sever-logs/)
4+
5+
![API tests](images/api-tests.png)
6+
7+
Note: currently the API and component tests do not clear the HTML created by each other
326 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// <reference types="cypress" />
2+
/// <reference types="../../lib" />
3+
/// <reference types="@bahmutov/cy-api" />
4+
import { Users } from './users.jsx'
5+
import React from 'react'
6+
import { mount } from 'cypress-react-unit-test'
7+
// adds cy.api command
8+
import '@bahmutov/cy-api/support'
9+
10+
// mixes component and API tests
11+
describe('Component and API tests', () => {
12+
it('fetches and shows 3 users', () => {
13+
// no mocking, just real request to the backend REST endpoint
14+
mount(<Users />)
15+
// fetching users can take a while
16+
cy.get('li', { timeout: 20000 }).should('have.length', 3)
17+
})
18+
19+
it('checks if API responds with a list of users', () => {
20+
cy.api({
21+
// same or similar URL to the one the component is using
22+
url: 'https://jsonplaceholder.cypress.io/users?_limit=3',
23+
})
24+
.its('body')
25+
.should('have.length', 3)
26+
})
27+
28+
// another component test
29+
it('shows stubbed users', () => {
30+
cy.stub(window, 'fetch').resolves({
31+
json: cy
32+
.stub()
33+
.resolves([{ id: 101, name: 'Test User' }])
34+
.as('users'),
35+
})
36+
// no mocking, just real request to the backend REST endpoint
37+
mount(<Users />)
38+
cy.get('li').should('have.length', 1)
39+
cy.get('@users').should('have.been.calledOnce')
40+
})
41+
42+
it('fetches one user from API', () => {
43+
cy.api({
44+
url: 'https://jsonplaceholder.cypress.io/users/1',
45+
})
46+
.its('body')
47+
.should('include', {
48+
id: 1,
49+
name: 'Leanne Graham',
50+
})
51+
})
52+
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react'
2+
3+
export class Users extends React.Component {
4+
constructor(props) {
5+
super(props)
6+
this.state = {
7+
users: [],
8+
}
9+
}
10+
11+
componentDidMount() {
12+
fetch('https://jsonplaceholder.cypress.io/users?_limit=3')
13+
.then(response => {
14+
return response.json()
15+
})
16+
.then(list => {
17+
this.setState({
18+
users: list,
19+
})
20+
})
21+
}
22+
23+
render() {
24+
return (
25+
<div>
26+
{this.state.users.map(user => (
27+
<li key={user.id}>
28+
<strong>{user.id}</strong> - {user.name}
29+
</li>
30+
))}
31+
</div>
32+
)
33+
}
34+
}

lib/hooks.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,16 @@ function cleanupStyles() {
7575

7676
const styles = document.body.querySelectorAll('style')
7777
styles.forEach(styleElement => {
78-
document.body.removeChild(styleElement)
78+
if (styleElement.parentElement) {
79+
styleElement.parentElement.removeChild(styleElement)
80+
}
7981
})
8082

8183
const links = document.body.querySelectorAll('link[rel=stylesheet]')
8284
links.forEach(link => {
83-
document.body.removeChild(link)
85+
if (link.parentElement) {
86+
link.parentElement.removeChild(link)
87+
}
8488
})
8589
}
8690

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@babel/preset-env": "7.4.5",
5656
"@babel/preset-react": "7.0.0",
5757
"@babel/preset-typescript": "7.10.4",
58+
"@bahmutov/cy-api": "1.4.2",
5859
"@date-io/date-fns": "1",
5960
"@emotion/babel-preset-css-prop": "10.0.27",
6061
"@emotion/core": "10.0.22",

0 commit comments

Comments
 (0)