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

Commit ad51a71

Browse files
committed
feat: reset the styles better
1 parent fffd891 commit ad51a71

File tree

6 files changed

+199
-13
lines changed

6 files changed

+199
-13
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('cssFile', () => {
66
it('is loaded', () => {
77
const Component = () => <button className="green">Green button</button>
88
mount(<Component />, {
9-
cssFiles: 'cypress/component/basic/css-file/index.css',
9+
cssFiles: 'cypress/component/basic/styles/css-file/index.css',
1010
})
1111

1212
cy.get('button')
@@ -17,7 +17,7 @@ describe('cssFile', () => {
1717
it('cssFile is for loading a single file', () => {
1818
const Component = () => <button className="green">Green button</button>
1919
mount(<Component />, {
20-
cssFile: 'cypress/component/basic/css-file/index.css',
20+
cssFile: 'cypress/component/basic/styles/css-file/index.css',
2121
})
2222

2323
cy.get('button')
@@ -31,8 +31,8 @@ describe('cssFile', () => {
3131
)
3232
mount(<Component />, {
3333
cssFiles: [
34-
'cypress/component/basic/css-file/base.css',
35-
'cypress/component/basic/css-file/index.css',
34+
'cypress/component/basic/styles/css-file/base.css',
35+
'cypress/component/basic/styles/css-file/index.css',
3636
],
3737
})
3838

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
5+
describe('style', () => {
6+
let baseStyle
7+
let indexStyle
8+
9+
before(() => {
10+
cy.readFile('cypress/component/basic/styles/css-file/base.css').then(
11+
css => {
12+
baseStyle = css
13+
},
14+
)
15+
cy.readFile('cypress/component/basic/styles/css-file/index.css').then(
16+
css => {
17+
indexStyle = css
18+
},
19+
)
20+
})
21+
22+
context('options.style', () => {
23+
it('string', () => {
24+
const Component = () => <button className="green">Green button</button>
25+
mount(<Component />, {
26+
style: indexStyle,
27+
})
28+
29+
cy.get('button')
30+
.should('have.class', 'green')
31+
.and('have.css', 'background-color', 'rgb(0, 255, 0)')
32+
})
33+
34+
it('string[]', () => {
35+
const Component = () => <button className="green">Green button</button>
36+
mount(<Component />, {
37+
style: [indexStyle],
38+
})
39+
40+
cy.get('button')
41+
.should('have.class', 'green')
42+
.and('have.css', 'background-color', 'rgb(0, 255, 0)')
43+
})
44+
})
45+
46+
context('options.styles', () => {
47+
it('string', () => {
48+
const Component = () => <button className="green">Green button</button>
49+
mount(<Component />, {
50+
styles: indexStyle,
51+
})
52+
53+
cy.get('button')
54+
.should('have.class', 'green')
55+
.and('have.css', 'background-color', 'rgb(0, 255, 0)')
56+
})
57+
58+
it('sets several', () => {
59+
const Component = () => (
60+
<button className="green">Large green button</button>
61+
)
62+
mount(<Component />, {
63+
styles: [baseStyle, indexStyle],
64+
})
65+
66+
// check the style from the first css file
67+
cy.get('button')
68+
.should('have.class', 'green')
69+
.invoke('css', 'height')
70+
.should(value => {
71+
// round the height, since in real browser it is never exactly 50
72+
expect(parseFloat(value), 'height is 50px').to.be.closeTo(50, 1)
73+
})
74+
75+
// and should have style from the second css file
76+
cy.get('button').and('have.css', 'background-color', 'rgb(0, 255, 0)')
77+
})
78+
79+
it('resets the style', () => {
80+
const Component = () => (
81+
<button className="green">Large green button</button>
82+
)
83+
mount(<Component />)
84+
// the component should NOT have CSS styles
85+
86+
cy.get('button')
87+
.should('have.class', 'green')
88+
.invoke('css', 'height')
89+
.should(value => {
90+
expect(parseFloat(value), 'height is < 20px').to.be.lessThan(20)
91+
})
92+
})
93+
})
94+
})
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
5+
describe('stylesheets', () => {
6+
const baseUrl = '/__root/cypress/component/basic/styles/css-file/base.css'
7+
const indexUrl = '/__root/cypress/component/basic/styles/css-file/index.css'
8+
9+
context('options.stylesheet', () => {
10+
it('options.stylesheet string', () => {
11+
const Component = () => <button className="green">Green button</button>
12+
mount(<Component />, {
13+
stylesheet: indexUrl,
14+
})
15+
16+
cy.get('button')
17+
.should('have.class', 'green')
18+
.and('have.css', 'background-color', 'rgb(0, 255, 0)')
19+
})
20+
21+
it('options.stylesheet []', () => {
22+
const Component = () => <button className="green">Green button</button>
23+
mount(<Component />, {
24+
stylesheet: [indexUrl],
25+
})
26+
27+
cy.get('button')
28+
.should('have.class', 'green')
29+
.and('have.css', 'background-color', 'rgb(0, 255, 0)')
30+
})
31+
})
32+
33+
context('options.stylesheets', () => {
34+
it('allows loading several CSS files', () => {
35+
const Component = () => (
36+
<button className="green">Large green button</button>
37+
)
38+
mount(<Component />, {
39+
stylesheets: [baseUrl, indexUrl],
40+
})
41+
42+
// check the style from the first css file
43+
cy.get('button')
44+
.should('have.class', 'green')
45+
.invoke('css', 'height')
46+
.should(value => {
47+
// round the height, since in real browser it is never exactly 50
48+
expect(parseFloat(value), 'height is 50px').to.be.closeTo(50, 1)
49+
})
50+
51+
// and should have style from the second css file
52+
cy.get('button').and('have.css', 'background-color', 'rgb(0, 255, 0)')
53+
})
54+
55+
it('resets the style', () => {
56+
const Component = () => (
57+
<button className="green">Large green button</button>
58+
)
59+
mount(<Component />)
60+
// the component should NOT have CSS styles
61+
62+
cy.get('button')
63+
.should('have.class', 'green')
64+
.invoke('css', 'height')
65+
.should(value => {
66+
expect(parseFloat(value), 'height is < 20px').to.be.lessThan(20)
67+
})
68+
})
69+
})
70+
})

lib/hooks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ before(() => {
2525
*/
2626
function cleanupStyles() {
2727
const document = cy.state('document') as Document
28+
2829
const styles = document.body.querySelectorAll('style')
2930
styles.forEach(styleElement => {
3031
document.body.removeChild(styleElement)
3132
})
33+
34+
const links = document.body.querySelectorAll('link[rel=stylesheet]')
35+
links.forEach(link => {
36+
document.body.removeChild(link)
37+
})
3238
}
3339

3440
beforeEach(cleanupStyles)

lib/index.d.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,35 @@ interface ReactModule {
1717
interface StyleOptions {
1818
/**
1919
* Creates <link href="..." /> element for each stylesheet
20+
* @alias stylesheet
2021
*/
2122
stylesheets: string | string[]
2223
/**
23-
* Single CSS stylesheet URL
24+
* Creates <link href="..." /> element for each stylesheet
25+
* @alias stylesheets
2426
*/
25-
stylesheet: string
27+
stylesheet: string | string[]
2628
/**
27-
* Creates <style>...</style> element and inserts given CSS
29+
* Creates <style>...</style> element and inserts given CSS.
30+
* @alias styles
2831
*/
29-
style: string
32+
style: string | string[]
3033
/**
31-
* Creates <style>...</style> element for each given CSS text
34+
* Creates <style>...</style> element for each given CSS text.
35+
* @alias style
3236
*/
3337
styles: string | string[]
3438
/**
3539
* Loads each file and creates a <style>...</style> element
3640
* with the loaded CSS
41+
* @alias cssFile
3742
*/
3843
cssFiles: string | string[]
3944
/**
4045
* Single CSS file to load into a <style></style> element
46+
* @alias cssFile
4147
*/
42-
cssFile: string
48+
cssFile: string | string[]
4349
}
4450

4551
interface MountReactComponentOptions {

lib/utils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ export const injectStylesBeforeElement = (
6262
el: HTMLElement,
6363
) => {
6464
// first insert all stylesheets as Link elements
65-
let stylesheets = options.stylesheet ? [options.stylesheet] : []
65+
let stylesheets: string[] = []
66+
67+
if (typeof options.stylesheet === 'string') {
68+
stylesheets.push(options.stylesheet)
69+
} else if (Array.isArray(options.stylesheet)) {
70+
stylesheets = stylesheets.concat(options.stylesheet)
71+
}
6672

6773
if (typeof options.stylesheets === 'string') {
6874
options.stylesheets = [options.stylesheets]
@@ -74,9 +80,11 @@ export const injectStylesBeforeElement = (
7480
insertStylesheets(stylesheets, document, el)
7581

7682
// insert any styles as <style>...</style> elements
77-
let styles = []
83+
let styles: string[] = []
7884
if (typeof options.style === 'string') {
7985
styles.push(options.style)
86+
} else if (Array.isArray(options.style)) {
87+
styles = styles.concat(options.style)
8088
}
8189
if (typeof options.styles === 'string') {
8290
styles.push(options.styles)
@@ -88,10 +96,12 @@ export const injectStylesBeforeElement = (
8896

8997
// now load any css files by path and add their content
9098
// as <style>...</style> elements
91-
let cssFiles = []
99+
let cssFiles: string[] = []
92100

93101
if (typeof options.cssFile === 'string') {
94102
cssFiles.push(options.cssFile)
103+
} else if (Array.isArray(options.cssFile)) {
104+
cssFiles = cssFiles.concat(options.cssFile)
95105
}
96106

97107
if (typeof options.cssFiles === 'string') {

0 commit comments

Comments
 (0)