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

Commit 34e1072

Browse files
committed
feat: allow passing just cssFile path to inject style
1 parent 72799e2 commit 34e1072

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ cy.readFile('cypress/integration/Button.css')
104104
})
105105
```
106106

107+
You can even let Cypress read the file and inject the style
108+
109+
```js
110+
const cssFile = 'cypress/integration/Button.css'
111+
cy.mount(<Button name='Orange' orange />, null, { cssFile })
112+
```
113+
114+
See [cypress/integration/inject-style-spec.js](cypress/integration/inject-style-spec.js) for more examples.
115+
107116
## Configuration
108117

109118
If your React and React DOM libraries are installed in non-standard paths (think monorepo scenario), you can tell this plugin where to find them. In `cypress.json` specify paths like this:

cypress/integration/inject-style-spec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/// <reference types="cypress" />
1+
// <reference types="cypress" />
2+
/// <reference types="../../lib" />
23
import React from 'react'
34

45
class Button extends React.Component {
@@ -52,6 +53,13 @@ describe('Injecting style', () => {
5253
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
5354
})
5455

56+
it('can be read automatically', () => {
57+
const cssFile = 'cypress/integration/Button.css'
58+
cy.mount(<Button name='Orange' orange />, null, { cssFile })
59+
cy.get('.orange button')
60+
.should('have.css', 'background-color', 'rgb(245, 146, 62)')
61+
})
62+
5563
context('read CSS file once', () => {
5664
before(() => {
5765
// .as('style') will save the loaded CSS text

lib/index.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ interface JSXElement {
2424
props: object
2525
}
2626

27+
type filepath = string
28+
2729
interface MountOptions {
30+
/**
31+
* CSS string to inject as a style element
32+
* before mounting the component.
33+
*/
2834
style: string
35+
/**
36+
* Read CSS file from the given path
37+
* and inject it as a style tag
38+
* before mounting the component
39+
*/
40+
cssFile: filepath
2941
}
3042

3143
declare namespace Cypress {

lib/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,25 @@ Cypress.Commands.add('copyComponentStyles', component => {
8888
})
8989

9090
const injectStyle = (options?: Partial<MountOptions>) => (w: Window) => {
91-
if (options && options.style) {
91+
// IMPORTANT: return window reference for other callbacks to inject
92+
// more things
93+
if (!options) {
94+
return w
95+
}
96+
if (options.style) {
9297
const style = w.document.createElement('style')
9398
style.appendChild(document.createTextNode(options.style))
9499
w.document.body.appendChild(style)
100+
}
101+
if (options.cssFile) {
102+
return cy.readFile(options.cssFile).then(css => {
103+
const style = w.document.createElement('style')
104+
style.appendChild(document.createTextNode(css))
105+
w.document.body.appendChild(style)
106+
return w
107+
})
95108
}
109+
return w
96110
}
97111

98112
/**

0 commit comments

Comments
 (0)