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

Commit 1d2c411

Browse files
committed
chore: refactor style injection
1 parent 0f2defa commit 1d2c411

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"titleBar.inactiveForeground": "#15202b99",
1919
"statusBar.background": "#7e9c00",
2020
"statusBarItem.hoverBackground": "#556900",
21-
"statusBar.foreground": "#15202b"
21+
"statusBar.foreground": "#15202b",
22+
"statusBar.border": "#7e9c00",
23+
"titleBar.border": "#7e9c00"
2224
},
2325
"peacock.color": "#7e9c00",
2426
"javascript.updateImportsOnFileMove.enabled": "never",

lib/index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ interface ReactModule {
99
source: string
1010
}
1111

12+
/**
13+
* Additional styles to inject into the document.
14+
* A component might need 3rd party libraries from CDN,
15+
* local CSS files and custom styles.
16+
*/
17+
interface StyleOptions {
18+
stylesheets: string | string[]
19+
style: string
20+
cssFile: string
21+
}
22+
23+
interface MountReactComponentOptions {
24+
alias: string
25+
ReactDom: typeof ReactDOM
26+
}
27+
28+
type MountOptions = Partial<StyleOptions & MountReactComponentOptions>
29+
1230
/**
1331
* The `type` property from the transpiled JSX object.
1432
* @example

lib/index.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
22
import ReactDOM, { unmountComponentAtNode } from 'react-dom'
33
import getDisplayName from './getDisplayName'
4+
import { injectStylesBeforeElement } from './utils'
45

56
function checkMountModeEnabled() {
67
// @ts-ignore
@@ -11,50 +12,13 @@ function checkMountModeEnabled() {
1112
}
1213
}
1314

14-
interface MountOptions {
15-
alias?: string
16-
ReactDom?: typeof ReactDOM
17-
stylesheets?: string | string[]
18-
style?: string
19-
cssFile?: string
20-
}
21-
2215
/**
2316
* Inject custom style text or CSS file or 3rd party style resources
2417
*/
2518
const injectStyles = (options: MountOptions) => () => {
2619
const document = cy.state('document')
27-
2820
const el = document.getElementById('cypress-jsdom')
29-
30-
// insert any custom global styles before the component
31-
if (typeof options.stylesheets === 'string') {
32-
options.stylesheets = [options.stylesheets]
33-
}
34-
if (Array.isArray(options.stylesheets)) {
35-
// console.log('adding stylesheets')
36-
options.stylesheets.forEach(href => {
37-
const link = document.createElement('link')
38-
link.type = 'text/css'
39-
link.rel = 'stylesheet'
40-
link.href = href
41-
document.body.insertBefore(link, el)
42-
})
43-
}
44-
45-
if (options.style) {
46-
const style = document.createElement('style')
47-
style.appendChild(document.createTextNode(options.style))
48-
document.body.insertBefore(style, el)
49-
}
50-
51-
if (options.cssFile) {
52-
return cy.readFile(options.cssFile).then(css => {
53-
const style = document.createElement('style')
54-
style.appendChild(document.createTextNode(css))
55-
document.body.appendChild(style)
56-
})
57-
}
21+
return injectStylesBeforeElement(options, document, el)
5822
}
5923

6024
/**

lib/utils.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Injects custom style text or CSS file or 3rd party style resources
3+
* into the given document.
4+
*/
5+
export const injectStylesBeforeElement = (
6+
options: Partial<StyleOptions>,
7+
document: Document,
8+
el: HTMLElement,
9+
) => {
10+
// insert any custom global styles before the component
11+
if (typeof options.stylesheets === 'string') {
12+
options.stylesheets = [options.stylesheets]
13+
}
14+
if (Array.isArray(options.stylesheets)) {
15+
// console.log('adding stylesheets')
16+
options.stylesheets.forEach(href => {
17+
const link = document.createElement('link')
18+
link.type = 'text/css'
19+
link.rel = 'stylesheet'
20+
link.href = href
21+
document.body.insertBefore(link, el)
22+
})
23+
}
24+
25+
if (options.style) {
26+
const style = document.createElement('style')
27+
style.appendChild(document.createTextNode(options.style))
28+
document.body.insertBefore(style, el)
29+
}
30+
31+
if (options.cssFile) {
32+
return cy.readFile(options.cssFile).then(css => {
33+
const style = document.createElement('style')
34+
style.appendChild(document.createTextNode(css))
35+
document.body.insertBefore(style, el)
36+
})
37+
}
38+
}

0 commit comments

Comments
 (0)