Skip to content

Upgrade cypress to 8.3.1, use new Component Test stuff, make fully self contained #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ yarn-debug.log*
yarn-error.log*
.nyc_output
cypress/videos

.idea
21 changes: 3 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
# test-material-ui
> Testing [Material UI](https://material-ui.com/) components using Cypress
> Testing [Material UI](https://material-ui.com/) components using Cypress Component Testing

![cypress version](https://img.shields.io/badge/cypress-5.6.0-brightgreen) ![cypress-react-unit-test version](https://img.shields.io/badge/cypress--react--unit--test-4.17.2-brightgreen)

Uses [cypress-react-unit-test](https://github.com/bahmutov/cypress-react-unit-test)

Installation

```shell
$ npx create-react-app test-material-ui
$ cd test-material-ui

$ yarn add @material-ui/core @material-ui/icons @material-ui/lab
info Direct dependencies
├─ @material-ui/core@4.9.9
├─ @material-ui/icons@4.9.1
└─ @material-ui/lab@4.0.0-alpha.48

$ yarn add -D cypress-react-unit-test@3.0.0-cypress-mount-mode.14
$ yarn install
$ yarn cypress open-ct
```

Add [cypress.json](cypress.json) with experimental flag

Write components and specs in [src](src) folder. I recommend looking at [src/autocomplete-spec.js](src/autocomplete-spec.js)

![Autocomplete test](images/autocomplete-demo.gif)
Expand Down
9 changes: 4 additions & 5 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"pluginsFile": "node_modules/cypress-react-unit-test/plugins/cra-v3",
"supportFile": "node_modules/cypress-react-unit-test/support",
"testFiles": "**/*-spec.js",
"experimentalComponentTesting": true,
"componentFolder": "src",
"component": {
"componentFolder": "src",
"testFiles": "**/*spec.{js,jsx,ts,tsx}"
},
"viewportWidth": 500
}
7 changes: 7 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (on, config) => {
if (config.testingType === 'component') {
require('@cypress/react/plugins/react-scripts')(on, config)
}

return config
}
46 changes: 25 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
{
"name": "test-material-ui",
"description": "Testing Material UI components using Cypress",
"version": "0.1.0",
"private": true,
"dependencies": {
"@date-io/date-fns": "1",
"@material-ui/core": "4.9.9",
"@material-ui/icons": "4.9.1",
"@material-ui/lab": "4.0.0-alpha.48",
"@material-ui/pickers": "3.2.10",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"date-fns": "2.12.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
"@date-io/date-fns": "^1.3.13",
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@material-ui/lab": "^4.0.0-alpha.60",
"@material-ui/pickers": "^3.3.10",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"date-fns": "^2.23.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "cypress-expect run --passing 8",
"eject": "react-scripts eject",
"update:badges": "update-badge cypress cypress-react-unit-test"
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
"extends": [
"react-app",
"react-app/jest",
"plugin:cypress/recommended"
]
},
"browserslist": {
"production": [
Expand All @@ -40,9 +43,10 @@
]
},
"devDependencies": {
"cypress": "5.6.0",
"cypress-expect": "2.4.3",
"cypress-react-unit-test": "4.17.2",
"dependency-version-badge": "1.5.0"
"@cypress/react": "^5.10.0",
"@cypress/webpack-dev-server": "^1.6.0",
"cypress": "^8.3.1",
"eslint-plugin-cypress": "^2.11.3",
"webpack-dev-server": "3.11.2"
}
}
Binary file modified public/favicon.ico
Binary file not shown.
5 changes: 0 additions & 5 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
"packagePatterns": [
"*"
],
"excludePackagePatterns": [
"cypress",
"cypress-expect",
"cypress-react-unit-test"
],
"enabled": false
}
]
Expand Down
38 changes: 38 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
25 changes: 25 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logo from './logo.svg';
import './App.css';

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}

export default App;
8 changes: 8 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
2 changes: 1 addition & 1 deletion src/autocomplete-spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="cypress" />
import { mount } from 'cypress-react-unit-test'
import { mount } from '@cypress/react'
// select example from
// https://material-ui.com/components/autocomplete/

Expand Down
2 changes: 1 addition & 1 deletion src/button-spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { mount } from 'cypress-react-unit-test'
import { mount } from '@cypress/react'
import Button from '@material-ui/core/Button'

it('renders a button', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/checkbox-labels-spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { mount } from 'cypress-react-unit-test'
import { mount } from '@cypress/react'
import CheckboxLabels from './checkbox-labels'

it('renders checkboxes', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/date-picker-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
// https://material-ui.com/components/pickers/
// https://codesandbox.io/s/z9k3z
import DatePickers from './date-picker'
import {mount} from 'cypress-react-unit-test'
import {mount} from '@cypress/react'

describe('Date pickers', () => {
it('picks a day', () => {
Expand Down Expand Up @@ -39,6 +39,7 @@ describe('Date pickers', () => {

cy.log('**check time**')
cy.get('.MuiInputAdornment-positionEnd button').eq(2).click()
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.get('.MuiPickersClock-clock').should('be.visible')
.wait(500) // for the demo

Expand Down
13 changes: 13 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
3 changes: 2 additions & 1 deletion src/list-item-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="cypress" />
import React from 'react'
import { mount } from 'cypress-react-unit-test'
import { mount } from '@cypress/react'
import ListItem from '@material-ui/core/ListItem'
import { ListItemText } from '@material-ui/core'
import SimpleList from './list-demo'
Expand All @@ -19,6 +19,7 @@ it('renders a list item', () => {
it('renders full list', () => {
cy.viewport(500, 800)
mount(<SimpleList />)
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.contains('Drafts')
.click()
.wait(1000)
Expand Down
1 change: 1 addition & 0 deletions src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/reportWebVitals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
getLCP(onPerfEntry);
getTTFB(onPerfEntry);
});
}
};

export default reportWebVitals;
2 changes: 1 addition & 1 deletion src/select-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'cypress-react-unit-test'
import { mount } from '@cypress/react'
// select example from
// https://material-ui.com/components/selects/
import React from 'react'
Expand Down
5 changes: 5 additions & 0 deletions src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
3 changes: 1 addition & 2 deletions src/simple-rating-spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/// <reference types="cypress" />
import { mount } from 'cypress-react-unit-test'
import { mount } from '@cypress/react'
// select example from
// https://material-ui.com/components/rating/

import React from 'react'
import Rating from '@material-ui/lab/Rating'
import Typography from '@material-ui/core/Typography'
import Box from '@material-ui/core/Box'
import { ExpansionPanelActions } from '@material-ui/core'

export default function SimpleRating({ onSetRating }) {
const [value, setValue] = React.useState(2)
Expand Down
Loading