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

Commit c551c96

Browse files
committed
show how components window is same as specs
1 parent bc3c6a4 commit c551c96

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Spec | Description
162162
[fails-correctly](cypress/component/basic/fails-correctly) | Cypress test fails correctly when interacting with disabled elements
163163
[pure-component-spec.js](cypress/component/basic/pure-component.spec.js) | Tests stateless component
164164
[stateless-spec.js](cypress/component/basic/stateless-spec.js) | Passes Cypress stub to the component, confirms the component calls it on click
165-
[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
165+
[window](cypress/component/basic/window) | In the component test, the spec `window` and the application's `window` where the component is running should be the same object
166166
[css](cypress/component/basic/css) | Shows that component with `import './Button.css'` works
167167
[css modules](cypress/component/basic/css-modules) | Shows that component that using css modules styles works
168168
[network](cypress/component/basic/network) | Confirms we can use `cy.route` to stub / spy on component's network calls
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# window
2+
3+
When running a component test, the spec's `window` and the component's `window` is the same object. See [spec.js](spec.js) and [component.js](component.js)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
3+
export class Component extends React.Component {
4+
constructor(props) {
5+
super(props)
6+
console.log(
7+
'set window.counter to this component in window',
8+
window.location.pathname,
9+
)
10+
window.component = this
11+
// save the reference to the window object
12+
// as "seen" by the component
13+
window.componentsWindow = window
14+
}
15+
16+
render() {
17+
return <p>component</p>
18+
}
19+
}
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
/// <reference types="cypress" />
22
import React from 'react'
33
import { mount } from 'cypress-react-unit-test'
4-
5-
export class Component extends React.Component {
6-
constructor(props) {
7-
super(props)
8-
console.log(
9-
'set window.counter to this component in window',
10-
window.location.pathname,
11-
)
12-
window.component = this
13-
}
14-
15-
render() {
16-
return <p>component</p>
17-
}
18-
}
4+
import { Component } from './component'
195

206
it('has the same window from the component as from test', () => {
217
cy.window()
@@ -24,12 +10,17 @@ it('has the same window from the component as from test', () => {
2410
.and('not.equal', 'blank')
2511

2612
mount(<Component />)
13+
2714
cy.contains('component')
2815
cy.window()
2916
.its('location.pathname')
3017
// this filename
31-
.should('match', /window-spec\.js$/)
18+
.should('match', /window\/spec\.js$/)
3219

3320
// the window should have property set by the component
3421
cy.window().should('have.property', 'component')
22+
23+
// the "window" object as seen by the component
24+
// is the window object of the spec code
25+
cy.window().should('have.property', 'componentsWindow', window)
3526
})

0 commit comments

Comments
 (0)