Skip to content

Commit 4cf3896

Browse files
author
Barthélémy Ledoux
authored
fix(component-testing): make content adjust to size of window (#14876)
1 parent 62df1e7 commit 4cf3896

File tree

8 files changed

+99
-61
lines changed

8 files changed

+99
-61
lines changed

npm/react/cypress/component/viewport-spec.js

-23
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react'
2+
import { mount } from '@cypress/react'
3+
4+
const viewportWidth = 200
5+
const viewportHeight = 100
6+
7+
describe('cypress.json viewport',
8+
{ viewportWidth, viewportHeight },
9+
() => {
10+
it('should have the correct dimensions', () => {
11+
// cy.should cannot be the first cy command we run
12+
cy.window().should((w) => {
13+
expect(w.innerWidth).to.eq(viewportWidth)
14+
expect(w.innerHeight).to.eq(viewportHeight)
15+
})
16+
})
17+
})
18+
19+
describe('cy.viewport', () => {
20+
it('should resize the viewport', () => {
21+
cy.viewport(viewportWidth, viewportHeight).should(() => {
22+
expect(window.innerWidth).to.eq(viewportWidth)
23+
expect(window.innerHeight).to.eq(viewportHeight)
24+
})
25+
})
26+
27+
it('should make it scale down when overflowing', () => {
28+
mount(<p>
29+
Lorem, ipsum dolor sit amet consectetur adipisicing elit.
30+
Incidunt necessitatibus quia quo obcaecati tempora numquam nobis
31+
minima libero vel? Nam sequi iusto quod fugit vel rerum eligendi beatae voluptatibus numquam.
32+
</p>)
33+
34+
expect(getComputedStyle(window.parent.document.querySelector('iframe').parentElement).transform).to.contain('matrix(1')
35+
cy.viewport(2000, 200).should(() => {
36+
expect(getComputedStyle(window.parent.document.querySelector('iframe').parentElement).transform).not.to.contain('matrix(1')
37+
})
38+
})
39+
})

packages/runner-ct/src/app/app.scss

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ main.app-ct {
7373
}
7474

7575
.runner-ct {
76-
left: $specs-list-offset;
76+
left: 0;
7777

7878
header {
7979
position: static;
@@ -82,6 +82,9 @@ main.app-ct {
8282
right: unset;
8383
bottom: unset;
8484
}
85+
.size-container{
86+
transform-origin: 0 0;
87+
}
8588
}
8689

8790
.ct-plugins {

packages/runner-ct/src/app/app.tsx

+40-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ export interface ExtendedConfigOptions extends Cypress.ConfigOptions {
2525
}
2626

2727
interface AppProps {
28-
state: State;
29-
// eslint-disable-next-line
28+
state: State
3029
eventManager: typeof EventManager
3130
config: ExtendedConfigOptions
3231
}
3332

33+
const DEFAULT_LEFT_SIDE_OF_SPLITPANE_WIDTH = 355
34+
// needs to account for the left bar + the margins around the viewport
35+
const VIEWPORT_SIDE_MARGIN = 40 + 17
36+
3437
const App: React.FC<AppProps> = observer(
3538
function App (props: AppProps) {
3639
const pluginRootContainer = React.useRef<null | HTMLDivElement>(null)
@@ -40,13 +43,46 @@ const App: React.FC<AppProps> = observer(
4043
const [pluginsHeight, setPluginsHeight] = React.useState(500)
4144
const [isResizing, setIsResizing] = React.useState(false)
4245
const [isSpecsListOpen, setIsSpecsListOpen] = React.useState(true)
46+
const [leftSideOfSplitPaneWidth, setLeftSideOfSplitPaneWidth] = React.useState(DEFAULT_LEFT_SIDE_OF_SPLITPANE_WIDTH)
47+
const headerRef = React.useRef(null)
48+
49+
function monitorWindowResize () {
50+
// I can't use forwardref in class based components
51+
// Header still is a class component
52+
// FIXME: use a forwardRef when available
53+
const header = headerRef.current.headerRef
54+
55+
function onWindowResize () {
56+
state.updateWindowDimensions({
57+
windowWidth: window.innerWidth,
58+
windowHeight: window.innerHeight,
59+
reporterWidth: leftSideOfSplitPaneWidth + VIEWPORT_SIDE_MARGIN,
60+
headerHeight: header.offsetHeight || 0,
61+
})
62+
}
63+
64+
window.addEventListener('resize', onWindowResize)
65+
window.dispatchEvent(new Event('resize'))
66+
}
4367

4468
React.useEffect(() => {
4569
if (pluginRootContainer.current) {
4670
state.initializePlugins(config, pluginRootContainer.current)
4771
}
72+
73+
monitorWindowResize()
4874
}, [])
4975

76+
function onSplitPaneChange (newWidth: number) {
77+
setLeftSideOfSplitPaneWidth(newWidth)
78+
state.updateWindowDimensions({
79+
reporterWidth: newWidth + VIEWPORT_SIDE_MARGIN,
80+
windowWidth: null,
81+
windowHeight: null,
82+
headerHeight: null,
83+
})
84+
}
85+
5086
return (
5187
<>
5288
<main className="app-ct">
@@ -73,6 +109,7 @@ const App: React.FC<AppProps> = observer(
73109
defaultSize={355}
74110
onDragStarted={() => setIsResizing(true)}
75111
onDragFinished={() => setIsResizing(false)}
112+
onChange={onSplitPaneChange}
76113
className={cs('reporter-pane', { 'is-reporter-resizing': isResizing })}
77114
>
78115
<div>
@@ -105,7 +142,7 @@ const App: React.FC<AppProps> = observer(
105142
}
106143
>
107144
<div className="runner runner-ct container">
108-
<Header {...props} />
145+
<Header {...props} ref={headerRef}/>
109146
<Iframes {...props} />
110147
<Message state={state}/>
111148
</div>

packages/runner-ct/src/header/header.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import selectorPlaygroundModel from '../selector-playground/selector-playground-
1212

1313
@observer
1414
export default class Header extends Component {
15-
@observable showingViewportMenu = false
15+
headerRef = React.createRef()
16+
17+
@observable showingViewportMenu = false;
1618

1719
render () {
1820
const { state, config } = this.props
1921

2022
return (
2123
<header
22-
ref='header'
24+
ref={this.headerRef}
2325
className={cs({
2426
'showing-selector-playground': selectorPlaygroundModel.isOpen,
2527
})}
@@ -77,7 +79,7 @@ export default class Header extends Component {
7779
componentDidUpdate () {
7880
if (selectorPlaygroundModel.isOpen !== this.previousSelectorPlaygroundOpen) {
7981
this.props.state.updateWindowDimensions({
80-
headerHeight: $(this.refs.header).outerHeight(),
82+
headerHeight: this.headerRef.current.offsetHeight,
8183
})
8284

8385
this.previousSelectorPlaygroundOpen = selectorPlaygroundModel.isOpen

packages/runner-ct/src/iframe/iframes.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class Iframes extends Component {
2626
}
2727

2828
render () {
29-
const { height, width, scriptError } = this.props.state
29+
const { height, width, scriptError, scale } = this.props.state
3030

3131
return (
3232
<div className={cs('iframes-ct-container', { 'has-error': !!scriptError })}>
@@ -36,6 +36,7 @@ export default class Iframes extends Component {
3636
style={{
3737
height,
3838
width,
39+
transform: `scale(${scale})`,
3940
}}
4041
/>
4142
<ScriptError error={scriptError} />

packages/runner-ct/src/iframe/iframes.scss

-29
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
1-
$cypress_blue: #3380FF;
2-
$hairline-color: $cypress_blue;
3-
$hairline-thickness: 1px;
4-
$hairline-size: min(60px, 80%);
5-
$hairline-offset: 15px;
6-
71
.iframes-ct-container {
82
margin: 8px;
93
}
104

115
.size-container {
126
width: 100%;
137
overflow: auto;
14-
resize: both;
158
background: none;
169
box-shadow: none;
17-
18-
&:after, &:before {
19-
content: "";
20-
position: absolute;
21-
background: $hairline-color;
22-
opacity: .5;
23-
padding: -2px;
24-
}
25-
26-
&:after {
27-
height: $hairline-size;
28-
width: $hairline-thickness;
29-
right: 0;
30-
bottom: $hairline-offset;
31-
}
32-
33-
&:before {
34-
width: $hairline-size;
35-
height: $hairline-thickness;
36-
right: $hairline-offset;
37-
bottom: 0;
38-
}
3910
}
4011

4112
.app-ct .runner {

packages/runner-ct/src/lib/state.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,15 @@ export default class State {
163163
}
164164
}
165165

166-
@action updateWindowDimensions ({ windowWidth, windowHeight, reporterWidth, headerHeight }) {
166+
@action updateWindowDimensions ({
167+
windowWidth, windowHeight, reporterWidth, headerHeight,
168+
}:
169+
{
170+
windowWidth: number | null
171+
windowHeight: number | null
172+
reporterWidth: number | null
173+
headerHeight: number | null
174+
}) {
167175
if (windowWidth != null) this.windowWidth = windowWidth
168176

169177
if (windowHeight != null) this.windowHeight = windowHeight

0 commit comments

Comments
 (0)