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

Commit ab5ca4c

Browse files
chore: Add css modules example (#417)
1 parent 269d6ed commit ab5ca4c

File tree

6 files changed

+118
-1
lines changed

6 files changed

+118
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Spec | Description
139139
[stateless-spec.js](cypress/component/basic/stateless-spec.js) | Passes Cypress stub to the component, confirms the component calls it on click
140140
[window-spec.js](cypress/component/basic/window-spec.js) | In the component test, the spec `window` and the application's `window` where the component is running should be the same object
141141
[css](cypress/component/basic/css) | Shows that component with `import './Button.css'` works
142+
[css modules](cypress/component/basic/css-modules) | Shows that component that using css modules styles works
142143
[network](cypress/component/basic/network) | Confirms we can use `cy.route` to stub / spy on component's network calls
143144
[no-visit](cypress/component/basic/no-visit) | Component specs cannot call `cy.visit`
144145
[react-book-by-chris-noring](cypress/component/basic/react-book-by-chris-noring) | Copied test examples from [React Book](https://softchris.github.io/books/react) and adapted for Cypress component tests
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react'
2+
import * as PropTypes from 'prop-types'
3+
import styles from './Button.modules.css'
4+
5+
export const Button = ({ name, orange, wide }) => {
6+
const className = [
7+
styles.componentButton,
8+
orange ? styles.orange : '',
9+
wide ? styles.wide : '',
10+
]
11+
12+
return (
13+
<div className={className.join(' ').trim()}>
14+
<button>{name}</button>
15+
</div>
16+
)
17+
}
18+
19+
Button.propTypes = {
20+
orange: PropTypes.bool,
21+
wide: PropTypes.bool,
22+
name: PropTypes.string.isRequired,
23+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.componentButton {
2+
display: inline-flex;
3+
width: 25%;
4+
flex: 1 0 auto;
5+
}
6+
7+
.componentButton.wide {
8+
width: 50%;
9+
}
10+
11+
.componentButton button {
12+
background-color: #e0e0e0;
13+
border: 0;
14+
font-size: 12px;
15+
margin: 0 1px 0 0;
16+
flex: 1 0 auto;
17+
padding: 0;
18+
}
19+
20+
.componentButton:last-child button {
21+
margin-right: 0;
22+
}
23+
24+
.componentButton.orange button {
25+
background-color: #f5923e;
26+
color: white;
27+
}
28+
29+
@media (min-width: 200px) and (min-height: 200px) {
30+
.componentButton button {
31+
font-size: 25px;
32+
}
33+
}
34+
35+
@media (min-width: 300px) and (min-height: 300px) {
36+
.componentButton button {
37+
font-size: 30px;
38+
}
39+
}
40+
41+
@media (min-width: 400px) and (min-height: 400px) {
42+
.componentButton button {
43+
font-size: 35px;
44+
}
45+
}
46+
47+
@media (min-width: 500px) and (min-height: 500px) {
48+
.componentButton button {
49+
font-size: 40px;
50+
}
51+
}
52+
53+
@media (min-width: 600px) and (min-height: 600px) {
54+
.componentButton button {
55+
font-size: 60px;
56+
}
57+
}
58+
59+
@media (min-width: 800px) and (min-height: 800px) {
60+
.componentButton button {
61+
font-size: 70px;
62+
}
63+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import styles from './Button.modules.css'
5+
import { Button } from './Button.jsx'
6+
7+
describe('Button', () => {
8+
it('renders orange styles', () => {
9+
mount(<Button name="Orange" orange />)
10+
11+
cy.get('div > button')
12+
.parent()
13+
.should('have.class', styles.orange)
14+
.find('button')
15+
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
16+
})
17+
})

cypress/plugins/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,21 @@ const webpackOptions = {
2424
options: babelConfig,
2525
},
2626
{
27-
test: /\.css$/,
27+
test: /\.modules\.css$/i,
2828
exclude: [/node_modules/],
29+
use: [
30+
'style-loader',
31+
{
32+
loader: 'css-loader',
33+
options: {
34+
modules: true,
35+
},
36+
},
37+
],
38+
},
39+
{
40+
test: /\.css$/,
41+
exclude: [/node_modules/, /\.modules\.css$/i],
2942
use: ['style-loader', 'css-loader'],
3043
},
3144
{

0 commit comments

Comments
 (0)