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

Commit fffd891

Browse files
committed
clean up any injected styles before each test
1 parent 298725f commit fffd891

File tree

6 files changed

+71
-9
lines changed

6 files changed

+71
-9
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import { mount } from 'cypress-react-unit-test'
3+
4+
const Login = () => (
5+
<div>
6+
<div>Login by clicking below</div>
7+
<a href="/foo">click me</a>
8+
</div>
9+
)
10+
11+
describe('full navigation', () => {
12+
it('should not happen', () => {
13+
mount(<Login />)
14+
15+
const clicked = cy.stub()
16+
cy.get('a').invoke('on', 'click', e => e.preventDefault() || clicked())
17+
cy.get('a').click()
18+
cy.wrap(clicked).should('have.been.calledOnce')
19+
})
20+
})

cypress/component/basic/css-file/css-file-spec.js renamed to cypress/component/basic/styles/css-file/css-file-spec.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ describe('cssFile', () => {
66
it('is loaded', () => {
77
const Component = () => <button className="green">Green button</button>
88
mount(<Component />, {
9-
cssFiles: 'cypress/component/component tests/basic/css-file/index.css',
9+
cssFiles: 'cypress/component/basic/css-file/index.css',
10+
})
11+
12+
cy.get('button')
13+
.should('have.class', 'green')
14+
.and('have.css', 'background-color', 'rgb(0, 255, 0)')
15+
})
16+
17+
it('cssFile is for loading a single file', () => {
18+
const Component = () => <button className="green">Green button</button>
19+
mount(<Component />, {
20+
cssFile: 'cypress/component/basic/css-file/index.css',
1021
})
1122

1223
cy.get('button')
@@ -20,8 +31,8 @@ describe('cssFile', () => {
2031
)
2132
mount(<Component />, {
2233
cssFiles: [
23-
'cypress/component/component tests/basic/css-file/base.css',
24-
'cypress/component/component tests/basic/css-file/index.css',
34+
'cypress/component/basic/css-file/base.css',
35+
'cypress/component/basic/css-file/index.css',
2536
],
2637
})
2738

@@ -37,4 +48,19 @@ describe('cssFile', () => {
3748
// and should have style from the second css file
3849
cy.get('button').and('have.css', 'background-color', 'rgb(0, 255, 0)')
3950
})
51+
52+
it('resets the style', () => {
53+
const Component = () => (
54+
<button className="green">Large green button</button>
55+
)
56+
mount(<Component />)
57+
// the component should NOT have CSS styles
58+
59+
cy.get('button')
60+
.should('have.class', 'green')
61+
.invoke('css', 'height')
62+
.should(value => {
63+
expect(parseFloat(value), 'height is < 20px').to.be.lessThan(20)
64+
})
65+
})
4066
})

lib/hooks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,18 @@ function renderTestingPlatform() {
1717
before(() => {
1818
renderTestingPlatform()
1919
})
20+
21+
/**
22+
* Remove any style or extra link elements from the iframe placeholder
23+
* left from any previous test
24+
*
25+
*/
26+
function cleanupStyles() {
27+
const document = cy.state('document') as Document
28+
const styles = document.body.querySelectorAll('style')
29+
styles.forEach(styleElement => {
30+
document.body.removeChild(styleElement)
31+
})
32+
}
33+
34+
beforeEach(cleanupStyles)

lib/utils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,33 @@ export const injectStylesBeforeElement = (
6262
el: HTMLElement,
6363
) => {
6464
// first insert all stylesheets as Link elements
65-
const stylesheets = options.stylesheet ? [options.stylesheet] : []
65+
let stylesheets = options.stylesheet ? [options.stylesheet] : []
6666

6767
if (typeof options.stylesheets === 'string') {
6868
options.stylesheets = [options.stylesheets]
6969
}
7070
if (options.stylesheets) {
71-
stylesheets.concat(options.stylesheets)
71+
stylesheets = stylesheets.concat(options.stylesheets)
7272
}
7373

7474
insertStylesheets(stylesheets, document, el)
7575

7676
// insert any styles as <style>...</style> elements
77-
const styles = []
77+
let styles = []
7878
if (typeof options.style === 'string') {
7979
styles.push(options.style)
8080
}
8181
if (typeof options.styles === 'string') {
8282
styles.push(options.styles)
8383
} else if (Array.isArray(options.styles)) {
84-
styles.concat(options.styles)
84+
styles = styles.concat(options.styles)
8585
}
8686

8787
insertStyles(styles, document, el)
8888

8989
// now load any css files by path and add their content
9090
// as <style>...</style> elements
91-
const cssFiles = []
91+
let cssFiles = []
9292

9393
if (typeof options.cssFile === 'string') {
9494
cssFiles.push(options.cssFile)
@@ -97,7 +97,8 @@ export const injectStylesBeforeElement = (
9797
if (typeof options.cssFiles === 'string') {
9898
cssFiles.push(options.cssFiles)
9999
} else if (Array.isArray(options.cssFiles)) {
100-
cssFiles.concat(options.cssFiles)
100+
cssFiles = cssFiles.concat(options.cssFiles)
101101
}
102+
102103
return insertLocalCssFiles(cssFiles, document, el)
103104
}

0 commit comments

Comments
 (0)