Skip to content

Commit

Permalink
fix(component-testing): make content adjust to size of window (#14876)
Browse files Browse the repository at this point in the history
  • Loading branch information
elevatebart committed Feb 5, 2021
1 parent 62df1e7 commit 4cf3896
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 61 deletions.
23 changes: 0 additions & 23 deletions npm/react/cypress/component/viewport-spec.js

This file was deleted.

39 changes: 39 additions & 0 deletions npm/react/cypress/component/viewport-spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react'
import { mount } from '@cypress/react'

const viewportWidth = 200
const viewportHeight = 100

describe('cypress.json viewport',
{ viewportWidth, viewportHeight },
() => {
it('should have the correct dimensions', () => {
// cy.should cannot be the first cy command we run
cy.window().should((w) => {
expect(w.innerWidth).to.eq(viewportWidth)
expect(w.innerHeight).to.eq(viewportHeight)
})
})
})

describe('cy.viewport', () => {
it('should resize the viewport', () => {
cy.viewport(viewportWidth, viewportHeight).should(() => {
expect(window.innerWidth).to.eq(viewportWidth)
expect(window.innerHeight).to.eq(viewportHeight)
})
})

it('should make it scale down when overflowing', () => {
mount(<p>
Lorem, ipsum dolor sit amet consectetur adipisicing elit.
Incidunt necessitatibus quia quo obcaecati tempora numquam nobis
minima libero vel? Nam sequi iusto quod fugit vel rerum eligendi beatae voluptatibus numquam.
</p>)

expect(getComputedStyle(window.parent.document.querySelector('iframe').parentElement).transform).to.contain('matrix(1')
cy.viewport(2000, 200).should(() => {
expect(getComputedStyle(window.parent.document.querySelector('iframe').parentElement).transform).not.to.contain('matrix(1')
})
})
})
5 changes: 4 additions & 1 deletion packages/runner-ct/src/app/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ main.app-ct {
}

.runner-ct {
left: $specs-list-offset;
left: 0;

header {
position: static;
Expand All @@ -82,6 +82,9 @@ main.app-ct {
right: unset;
bottom: unset;
}
.size-container{
transform-origin: 0 0;
}
}

.ct-plugins {
Expand Down
43 changes: 40 additions & 3 deletions packages/runner-ct/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ export interface ExtendedConfigOptions extends Cypress.ConfigOptions {
}

interface AppProps {
state: State;
// eslint-disable-next-line
state: State
eventManager: typeof EventManager
config: ExtendedConfigOptions
}

const DEFAULT_LEFT_SIDE_OF_SPLITPANE_WIDTH = 355
// needs to account for the left bar + the margins around the viewport
const VIEWPORT_SIDE_MARGIN = 40 + 17

const App: React.FC<AppProps> = observer(
function App (props: AppProps) {
const pluginRootContainer = React.useRef<null | HTMLDivElement>(null)
Expand All @@ -40,13 +43,46 @@ const App: React.FC<AppProps> = observer(
const [pluginsHeight, setPluginsHeight] = React.useState(500)
const [isResizing, setIsResizing] = React.useState(false)
const [isSpecsListOpen, setIsSpecsListOpen] = React.useState(true)
const [leftSideOfSplitPaneWidth, setLeftSideOfSplitPaneWidth] = React.useState(DEFAULT_LEFT_SIDE_OF_SPLITPANE_WIDTH)
const headerRef = React.useRef(null)

function monitorWindowResize () {
// I can't use forwardref in class based components
// Header still is a class component
// FIXME: use a forwardRef when available
const header = headerRef.current.headerRef

function onWindowResize () {
state.updateWindowDimensions({
windowWidth: window.innerWidth,
windowHeight: window.innerHeight,
reporterWidth: leftSideOfSplitPaneWidth + VIEWPORT_SIDE_MARGIN,
headerHeight: header.offsetHeight || 0,
})
}

window.addEventListener('resize', onWindowResize)
window.dispatchEvent(new Event('resize'))
}

React.useEffect(() => {
if (pluginRootContainer.current) {
state.initializePlugins(config, pluginRootContainer.current)
}

monitorWindowResize()
}, [])

function onSplitPaneChange (newWidth: number) {
setLeftSideOfSplitPaneWidth(newWidth)
state.updateWindowDimensions({
reporterWidth: newWidth + VIEWPORT_SIDE_MARGIN,
windowWidth: null,
windowHeight: null,
headerHeight: null,
})
}

return (
<>
<main className="app-ct">
Expand All @@ -73,6 +109,7 @@ const App: React.FC<AppProps> = observer(
defaultSize={355}
onDragStarted={() => setIsResizing(true)}
onDragFinished={() => setIsResizing(false)}
onChange={onSplitPaneChange}
className={cs('reporter-pane', { 'is-reporter-resizing': isResizing })}
>
<div>
Expand Down Expand Up @@ -105,7 +142,7 @@ const App: React.FC<AppProps> = observer(
}
>
<div className="runner runner-ct container">
<Header {...props} />
<Header {...props} ref={headerRef}/>
<Iframes {...props} />
<Message state={state}/>
</div>
Expand Down
8 changes: 5 additions & 3 deletions packages/runner-ct/src/header/header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import selectorPlaygroundModel from '../selector-playground/selector-playground-

@observer
export default class Header extends Component {
@observable showingViewportMenu = false
headerRef = React.createRef()

@observable showingViewportMenu = false;

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

return (
<header
ref='header'
ref={this.headerRef}
className={cs({
'showing-selector-playground': selectorPlaygroundModel.isOpen,
})}
Expand Down Expand Up @@ -77,7 +79,7 @@ export default class Header extends Component {
componentDidUpdate () {
if (selectorPlaygroundModel.isOpen !== this.previousSelectorPlaygroundOpen) {
this.props.state.updateWindowDimensions({
headerHeight: $(this.refs.header).outerHeight(),
headerHeight: this.headerRef.current.offsetHeight,
})

this.previousSelectorPlaygroundOpen = selectorPlaygroundModel.isOpen
Expand Down
3 changes: 2 additions & 1 deletion packages/runner-ct/src/iframe/iframes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Iframes extends Component {
}

render () {
const { height, width, scriptError } = this.props.state
const { height, width, scriptError, scale } = this.props.state

return (
<div className={cs('iframes-ct-container', { 'has-error': !!scriptError })}>
Expand All @@ -36,6 +36,7 @@ export default class Iframes extends Component {
style={{
height,
width,
transform: `scale(${scale})`,
}}
/>
<ScriptError error={scriptError} />
Expand Down
29 changes: 0 additions & 29 deletions packages/runner-ct/src/iframe/iframes.scss
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
$cypress_blue: #3380FF;
$hairline-color: $cypress_blue;
$hairline-thickness: 1px;
$hairline-size: min(60px, 80%);
$hairline-offset: 15px;

.iframes-ct-container {
margin: 8px;
}

.size-container {
width: 100%;
overflow: auto;
resize: both;
background: none;
box-shadow: none;

&:after, &:before {
content: "";
position: absolute;
background: $hairline-color;
opacity: .5;
padding: -2px;
}

&:after {
height: $hairline-size;
width: $hairline-thickness;
right: 0;
bottom: $hairline-offset;
}

&:before {
width: $hairline-size;
height: $hairline-thickness;
right: $hairline-offset;
bottom: 0;
}
}

.app-ct .runner {
Expand Down
10 changes: 9 additions & 1 deletion packages/runner-ct/src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,15 @@ export default class State {
}
}

@action updateWindowDimensions ({ windowWidth, windowHeight, reporterWidth, headerHeight }) {
@action updateWindowDimensions ({
windowWidth, windowHeight, reporterWidth, headerHeight,
}:
{
windowWidth: number | null
windowHeight: number | null
reporterWidth: number | null
headerHeight: number | null
}) {
if (windowWidth != null) this.windowWidth = windowWidth

if (windowHeight != null) this.windowHeight = windowHeight
Expand Down

4 comments on commit 4cf3896

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4cf3896 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.5.0/circle-develop-4cf3896ecbb074831709f73f22768457fdaf5779/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4cf3896 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.5.0/appveyor-develop-4cf3896ecbb074831709f73f22768457fdaf5779/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4cf3896 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.5.0/appveyor-develop-4cf3896ecbb074831709f73f22768457fdaf5779/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4cf3896 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.5.0/circle-develop-4cf3896ecbb074831709f73f22768457fdaf5779/cypress.tgz

Please sign in to comment.