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

Commit 298725f

Browse files
committed
feat: add css aliases to mounting options
1 parent 9d06119 commit 298725f

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

lib/index.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,41 @@ interface ReactModule {
1515
* local CSS files and custom styles.
1616
*/
1717
interface StyleOptions {
18+
/**
19+
* Creates <link href="..." /> element for each stylesheet
20+
*/
1821
stylesheets: string | string[]
22+
/**
23+
* Single CSS stylesheet URL
24+
*/
25+
stylesheet: string
26+
/**
27+
* Creates <style>...</style> element and inserts given CSS
28+
*/
1929
style: string
30+
/**
31+
* Creates <style>...</style> element for each given CSS text
32+
*/
33+
styles: string | string[]
34+
/**
35+
* Loads each file and creates a <style>...</style> element
36+
* with the loaded CSS
37+
*/
2038
cssFiles: string | string[]
39+
/**
40+
* Single CSS file to load into a <style></style> element
41+
*/
42+
cssFile: string
2143
}
2244

2345
interface MountReactComponentOptions {
2446
alias: string
2547
ReactDom: typeof ReactDOM
48+
/**
49+
* Log the mounting command into Cypress Command Log,
50+
* true by default.
51+
*/
52+
log: boolean
2653
}
2754

2855
type MountOptions = Partial<StyleOptions & MountReactComponentOptions>

lib/utils.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ function insertStylesheets(
1818
/**
1919
* Inserts a single stylesheet element
2020
*/
21-
function insertStyles(style: string, document: Document, el: HTMLElement) {
22-
const styleElement = document.createElement('style')
23-
styleElement.appendChild(document.createTextNode(style))
24-
document.body.insertBefore(styleElement, el)
21+
function insertStyles(styles: string[], document: Document, el: HTMLElement) {
22+
styles.forEach(style => {
23+
const styleElement = document.createElement('style')
24+
styleElement.appendChild(document.createTextNode(style))
25+
document.body.insertBefore(styleElement, el)
26+
})
2527
}
2628

2729
function insertSingleCssFile(
@@ -59,26 +61,43 @@ export const injectStylesBeforeElement = (
5961
document: Document,
6062
el: HTMLElement,
6163
) => {
62-
// insert any custom global styles before the component
64+
// first insert all stylesheets as Link elements
65+
const stylesheets = options.stylesheet ? [options.stylesheet] : []
66+
6367
if (typeof options.stylesheets === 'string') {
6468
options.stylesheets = [options.stylesheets]
6569
}
66-
if (Array.isArray(options.stylesheets)) {
67-
insertStylesheets(options.stylesheets, document, el)
70+
if (options.stylesheets) {
71+
stylesheets.concat(options.stylesheets)
6872
}
6973

70-
if (options.style) {
71-
insertStyles(options.style, document, el)
74+
insertStylesheets(stylesheets, document, el)
75+
76+
// insert any styles as <style>...</style> elements
77+
const styles = []
78+
if (typeof options.style === 'string') {
79+
styles.push(options.style)
80+
}
81+
if (typeof options.styles === 'string') {
82+
styles.push(options.styles)
83+
} else if (Array.isArray(options.styles)) {
84+
styles.concat(options.styles)
7285
}
7386

74-
if (Array.isArray(options.cssFiles)) {
75-
return insertLocalCssFiles(options.cssFiles, document, el)
87+
insertStyles(styles, document, el)
88+
89+
// now load any css files by path and add their content
90+
// as <style>...</style> elements
91+
const cssFiles = []
92+
93+
if (typeof options.cssFile === 'string') {
94+
cssFiles.push(options.cssFile)
7695
}
7796

7897
if (typeof options.cssFiles === 'string') {
79-
return insertSingleCssFile(options.cssFiles, document, el)
98+
cssFiles.push(options.cssFiles)
99+
} else if (Array.isArray(options.cssFiles)) {
100+
cssFiles.concat(options.cssFiles)
80101
}
81-
82-
// force this function to have a return value from all branches
83-
return cy.wrap(null)
102+
return insertLocalCssFiles(cssFiles, document, el)
84103
}

0 commit comments

Comments
 (0)