cypress-react-unit-test
Advanced tools
Comparing version 2.6.0 to 2.7.0
@@ -77,3 +77,8 @@ "use strict"; | ||
var injectStyle = function (options) { return function (w) { | ||
if (options && options.style) { | ||
// IMPORTANT: return window reference for other callbacks to inject | ||
// more things | ||
if (!options) { | ||
return w; | ||
} | ||
if (options.style) { | ||
var style = w.document.createElement('style'); | ||
@@ -83,2 +88,11 @@ style.appendChild(document.createTextNode(options.style)); | ||
} | ||
if (options.cssFile) { | ||
return cy.readFile(options.cssFile).then(function (css) { | ||
var style = w.document.createElement('style'); | ||
style.appendChild(document.createTextNode(css)); | ||
w.document.body.appendChild(style); | ||
return w; | ||
}); | ||
} | ||
return w; | ||
}; }; | ||
@@ -85,0 +99,0 @@ /** |
@@ -27,4 +27,16 @@ // I hope to get types and docs from functions imported from ./index one day | ||
type filepath = string | ||
interface MountOptions { | ||
/** | ||
* CSS string to inject as a style element | ||
* before mounting the component. | ||
*/ | ||
style: string | ||
/** | ||
* Read CSS file from the given path | ||
* and inject it as a style tag | ||
* before mounting the component | ||
*/ | ||
cssFile: filepath | ||
} | ||
@@ -31,0 +43,0 @@ |
@@ -91,7 +91,21 @@ /// <reference path="./index.d.ts" /> | ||
const injectStyle = (options?: Partial<MountOptions>) => (w: Window) => { | ||
if (options && options.style) { | ||
// IMPORTANT: return window reference for other callbacks to inject | ||
// more things | ||
if (!options) { | ||
return w | ||
} | ||
if (options.style) { | ||
const style = w.document.createElement('style') | ||
style.appendChild(document.createTextNode(options.style)) | ||
w.document.body.appendChild(style) | ||
} | ||
if (options.cssFile) { | ||
return cy.readFile(options.cssFile).then(css => { | ||
const style = w.document.createElement('style') | ||
style.appendChild(document.createTextNode(css)) | ||
w.document.body.appendChild(style) | ||
return w | ||
}) | ||
} | ||
return w | ||
} | ||
@@ -98,0 +112,0 @@ |
{ | ||
"name": "cypress-react-unit-test", | ||
"version": "2.6.0", | ||
"version": "2.7.0", | ||
"description": "Unit test React components using Cypress", | ||
@@ -5,0 +5,0 @@ "main": "dist", |
@@ -107,2 +107,11 @@ # cypress-react-unit-test [![CircleCI](https://circleci.com/gh/bahmutov/cypress-react-unit-test/tree/master.svg?style=svg)](https://circleci.com/gh/bahmutov/cypress-react-unit-test/tree/master) [![Cypress.io tests](https://img.shields.io/badge/cypress.io-tests-green.svg?style=flat-square)](https://dashboard.cypress.io/#/projects/z9dxah) [![renovate-app badge][renovate-badge]][renovate-app] [![This project is using Percy.io for visual regression testing.](https://percy.io/static/images/percy-badge.svg)](https://percy.io/bahmutov/cypress-react-unit-test) | ||
You can even let Cypress read the file and inject the style | ||
```js | ||
const cssFile = 'cypress/integration/Button.css' | ||
cy.mount(<Button name='Orange' orange />, null, { cssFile }) | ||
``` | ||
See [cypress/integration/inject-style-spec.js](cypress/integration/inject-style-spec.js) for more examples. | ||
## Configuration | ||
@@ -109,0 +118,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29566
581
183