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

Commit 8facc70

Browse files
committed
fix: copy styles from correct frame, close #49
1 parent 9093fec commit 8facc70

File tree

7 files changed

+408
-57
lines changed

7 files changed

+408
-57
lines changed

cypress.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"viewportWidth": 300,
33
"viewportHeight": 100,
44
"video": false,
5-
"projectId": "z9dxah"
5+
"projectId": "z9dxah",
6+
"ignoreTestFiles": "*.css"
67
}

cypress/integration/Button.css

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.component-button {
2+
display: inline-flex;
3+
width: 25%;
4+
flex: 1 0 auto;
5+
}
6+
7+
.component-button.wide {
8+
width: 50%;
9+
}
10+
11+
.component-button 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+
.component-button:last-child button {
21+
margin-right: 0;
22+
}
23+
24+
.component-button.orange button {
25+
background-color: #F5923E;
26+
color: white;
27+
}
28+
29+
@media (min-width: 200px) and (min-height: 200px) {
30+
.component-button button {
31+
font-size: 25px;
32+
}
33+
}
34+
35+
@media (min-width: 300px) and (min-height: 300px) {
36+
.component-button button {
37+
font-size: 30px;
38+
}
39+
}
40+
41+
@media (min-width: 400px) and (min-height: 400px) {
42+
.component-button button {
43+
font-size: 35px;
44+
}
45+
}
46+
47+
@media (min-width: 500px) and (min-height: 500px) {
48+
.component-button button {
49+
font-size: 40px;
50+
}
51+
}
52+
53+
@media (min-width: 600px) and (min-height: 600px) {
54+
.component-button button {
55+
font-size: 60px;
56+
}
57+
}
58+
59+
@media (min-width: 800px) and (min-height: 800px) {
60+
.component-button button {
61+
font-size: 70px;
62+
}
63+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import './Button.css'
4+
5+
class Button extends React.Component {
6+
handleClick () {
7+
this.props.clickHandler(this.props.name)
8+
}
9+
10+
render () {
11+
const className = [
12+
'component-button',
13+
this.props.orange ? 'orange' : '',
14+
this.props.wide ? 'wide' : ''
15+
]
16+
17+
return (
18+
<div className={className.join(' ').trim()}>
19+
<button onClick={this.handleClick.bind(this)}>{this.props.name}</button>
20+
</div>
21+
)
22+
}
23+
}
24+
25+
describe('Button', () => {
26+
it('can be orange', () => {
27+
cy.mount(<Button name='Orange' orange />)
28+
cy.get('.component-button')
29+
.should('have.class', 'orange')
30+
.find('button')
31+
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
32+
})
33+
})

cypress/plugins/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ const webpackOptions = {
77
loader: 'babel-loader',
88
options: {
99
presets: ['@babel/preset-env', '@babel/preset-react'],
10-
plugins: ['@babel/plugin-proposal-class-properties'],
11-
},
10+
plugins: ['@babel/plugin-proposal-class-properties']
11+
}
12+
},
13+
{
14+
test: /\.css$/,
15+
exclude: [/node_modules/],
16+
use: ['style-loader', 'css-loader']
1217
}
1318
]
1419
}

lib/index.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ Cypress.Commands.add('copyComponentStyles', component => {
5151
// need to find same component when component is recompiled
5252
// by the JSX preprocessor. Thus have to use something else,
5353
// like component name
54+
const parentDocument = window.parent.document
55+
// @ts-ignore
56+
const specDocument = parentDocument.querySelector('iframe.spec-iframe').contentDocument
57+
// @ts-ignore
58+
const appDocument = parentDocument.querySelector('iframe.aut-iframe').contentDocument
59+
5460
const hash = component.type.name
55-
const document = cy.state('document')
56-
let styles = document.querySelectorAll('head style')
61+
let styles = specDocument.querySelectorAll('head style')
5762
if (styles.length) {
58-
cy.log('injected %d styles', styles.length)
63+
cy.log(`injected ${styles.length} style(s)`)
5964
Cypress.stylesCache.set(hash, styles)
6065
} else {
6166
cy.log('No styles injected for this component, checking cache')
@@ -68,14 +73,7 @@ Cypress.Commands.add('copyComponentStyles', component => {
6873
if (!styles) {
6974
return
7075
}
71-
const parentDocument = window.parent.document
72-
// hmm, is this really project name?
73-
// @ts-ignore
74-
const projectName = Cypress.config('projectName')
75-
const appIframeId = "Your App: '" + projectName + "'"
76-
const appIframe = parentDocument.getElementById(appIframeId)
77-
// @ts-ignore
78-
var head = appIframe.contentDocument.querySelector('head')
76+
const head = appDocument.querySelector('head')
7977
styles.forEach(function (style) {
8078
head.appendChild(style)
8179
})

0 commit comments

Comments
 (0)