-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
424 additions
and
0 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
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,17 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-react", | ||
{ | ||
"plugins": ["@babel/plugin-proposal-class-properties"] | ||
}, | ||
"@emotion/babel-preset-css-prop" | ||
], | ||
"env": { | ||
"test": { | ||
"plugins": [ | ||
"babel-plugin-istanbul" | ||
] | ||
} | ||
} | ||
} |
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 @@ | ||
package-lock=false |
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,29 @@ | ||
# example: using-babel | ||
|
||
Component testing for projects using Babel config | ||
|
||
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency. | ||
|
||
```shell | ||
npm run cy:open | ||
# or run headless tests | ||
npm test | ||
``` | ||
|
||
![Example component test](images/dynamic.gif) | ||
|
||
## Specs | ||
|
||
See specs [src/Post.spec.js](src/Post.spec.js) and [src/Skeleton.spec.js](src/Skeleton.spec.js). The specs are bundled using [.babelrc](.babelrc) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file preprocessor | ||
|
||
```js | ||
// let's bundle spec files and the components they include using | ||
// the same bundling settings as the project by loading .babelrc | ||
const preprocessor = require('cypress-react-unit-test/plugins/babelrc') | ||
module.exports = (on, config) => { | ||
preprocessor(on, config) | ||
// IMPORTANT to return the config object | ||
// with the any changed environment variables | ||
return config | ||
} | ||
``` |
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,8 @@ | ||
{ | ||
"fixturesFolder": false, | ||
"testFiles": "**/*spec.js", | ||
"viewportWidth": 500, | ||
"viewportHeight": 500, | ||
"experimentalComponentTesting": true, | ||
"componentFolder": "src" | ||
} |
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,6 @@ | ||
/// <reference types="cypress" /> | ||
describe('integration spec', () => { | ||
it('works', () => { | ||
expect(1).to.equal(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,9 @@ | ||
// let's bundle spec files and the components they include using | ||
// the same bundling settings as the project by loading .babelrc | ||
const preprocessor = require('cypress-react-unit-test/plugins/babelrc') | ||
module.exports = (on, config) => { | ||
preprocessor(on, config) | ||
// IMPORTANT to return the config object | ||
// with the any changed environment variables | ||
return config | ||
} |
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 @@ | ||
require('cypress-react-unit-test/dist/hooks') |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
{ | ||
"name": "example-using-babel", | ||
"description": "Component testing for projects using Babel config", | ||
"private": true, | ||
"scripts": { | ||
"test": "../../node_modules/.bin/cypress run", | ||
"cy:open": "../../node_modules/.bin/cypress open" | ||
}, | ||
"devDependencies": { | ||
"cypress-react-unit-test": "file:../.." | ||
} | ||
} |
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,36 @@ | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import Skeleton from 'react-loading-skeleton' | ||
|
||
export default class Post extends Component { | ||
static propTypes = { | ||
title: PropTypes.string, | ||
children: PropTypes.node, | ||
size: PropTypes.oneOf(['small', 'large']), | ||
} | ||
|
||
static defaultProps = { | ||
size: 'small', | ||
} | ||
|
||
getStyle() { | ||
const { size } = this.props | ||
const baseStyle = { | ||
padding: 8, | ||
width: '20em', | ||
} | ||
return Object.assign(baseStyle, { | ||
fontSize: size === 'small' ? 16 : 25, | ||
lineHeight: size === 'small' ? 'normal' : 2, | ||
}) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div style={this.getStyle()}> | ||
<h1>{this.props.title || <Skeleton />}</h1> | ||
<p>{this.props.children || <Skeleton count={5} />}</p> | ||
</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,85 @@ | ||
/// <reference types="cypress" /> | ||
import React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
|
||
import SideBySide from './SideBySide' | ||
import Post from './Post' | ||
import { SkeletonTheme } from 'react-loading-skeleton' | ||
|
||
// matches Post.story.js | ||
describe('Post skeletons', () => { | ||
it('default', () => { | ||
mount( | ||
<SideBySide> | ||
<Post /> | ||
<Post title="A title"> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum | ||
nec justo feugiat, auctor nunc ac, volutpat arcu. Suspendisse faucibus | ||
aliquam ante, sit amet iaculis dolor posuere et. In ut placerat leo. | ||
</Post> | ||
</SideBySide>, | ||
) | ||
}) | ||
|
||
it('large size', () => { | ||
mount( | ||
<SideBySide> | ||
<Post size="large" /> | ||
<Post size="large" title="A title"> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum | ||
nec justo feugiat, auctor nunc ac, volutpat arcu. Suspendisse faucibus | ||
aliquam ante, sit amet iaculis dolor posuere et. In ut placerat leo. | ||
</Post> | ||
</SideBySide>, | ||
) | ||
}) | ||
|
||
it('different colors', () => { | ||
const Test = () => ( | ||
<div> | ||
<SkeletonTheme color="#1D5CA6" highlightColor="#164999"> | ||
<Post /> | ||
</SkeletonTheme> | ||
<SkeletonTheme color="#333" highlightColor="#4a4a4a"> | ||
<Post /> | ||
</SkeletonTheme> | ||
</div> | ||
) | ||
|
||
mount(<Test />) | ||
}) | ||
|
||
// extra test - set Post title after a timeout | ||
it('loads title after timeout', () => { | ||
const Demo = () => { | ||
// at first there is not title, no children | ||
const [title, setTitle] = React.useState('') | ||
const [text, setText] = React.useState('') | ||
|
||
setTimeout(() => { | ||
setTitle('Post title 👍') | ||
}, 1000) | ||
|
||
setTimeout(() => { | ||
setText(`The text has arrived ... | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec | ||
justo feugiat, auctor nunc ac, volutpat arcu. Suspendisse faucibus | ||
aliquam ante, sit amet iaculis dolor posuere et. In ut placerat leo. | ||
`) | ||
}, 2000) | ||
|
||
return <Post title={title} children={text} /> | ||
} | ||
mount(<Demo />) | ||
// at first, the title and the text are 💀 | ||
cy.get('h1 .react-loading-skeleton').should('have.length', 1) | ||
cy.get('p .react-loading-skeleton').should('have.length', 5) | ||
|
||
// then the title arrives, but the text is still skeletons | ||
cy.contains('h1', 'Post title 👍').should('be.visible') | ||
cy.get('p .react-loading-skeleton').should('have.length', 5) | ||
|
||
// and then no skeletons remain | ||
cy.get('.react-loading-skeleton').should('not.exist') | ||
}) | ||
}) |
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,28 @@ | ||
import React from 'react' | ||
|
||
const style = { | ||
alignItems: 'flex-start', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
} | ||
|
||
const arrowStyle = { | ||
alignSelf: 'center', | ||
fontSize: 20, | ||
padding: '0 20px', | ||
} | ||
|
||
export default ({ children }) => { | ||
const childrenWithArrows = [] | ||
React.Children.forEach(children, (child, index) => { | ||
if (index > 0) { | ||
childrenWithArrows.push( | ||
<div key={index} style={arrowStyle}> | ||
→ | ||
</div>, | ||
) | ||
} | ||
childrenWithArrows.push(child) | ||
}) | ||
return <div style={style}>{childrenWithArrows}</div> | ||
} |
Oops, something went wrong.