Skip to content

Commit 05ddd18

Browse files
authored
Add root style recipe (#437)
* add new CSS variable example * add new recipe to circle * fix missing job * document the spec more
1 parent aeff1e3 commit 05ddd18

File tree

10 files changed

+99
-0
lines changed

10 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Recipe | Description
2828
[Waiting for static resource](./examples/testing-dom__wait-for-resource) | Shows how to wait for CSS, image, or any other static resource to load
2929
[CSV load and table test](./examples/testing-dom__csv-table) | Loads CSV file and compares objects against cells in a table
3030
[Evaluate performance metrics](./examples/testing-dom__performance-metrics) | Utilize Cypress to monitor a website
31+
[Root style](./examples/testing-dom__root-style) | Trigger input color change that modifies CSS variable
3132

3233
## Logging in recipes
3334

circle.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ jobs:
241241
<<: *defaults
242242
testing-dom__csv-table:
243243
<<: *defaults
244+
testing-dom__root-style:
245+
<<: *defaults
244246
unit-testing__application-code:
245247
<<: *defaults
246248
unit-testing__react:
@@ -420,6 +422,9 @@ all_jobs: &all_jobs
420422
- testing-dom__performance-metrics:
421423
requires:
422424
- build
425+
- testing-dom__root-style:
426+
requires:
427+
- build
423428
- unit-testing__application-code:
424429
requires:
425430
- build
@@ -502,6 +507,7 @@ all_jobs: &all_jobs
502507
- testing-dom__wait-for-resource
503508
- testing-dom__csv-table
504509
- testing-dom__performance-metrics
510+
- testing-dom__root-style
505511
- unit-testing__application-code
506512
- unit-testing__react
507513
# "meta" jobs
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Testing how the app sets the root document style variable
2+
3+
The application in [index.html](index.html) uses `<input type="color">` to set the CSS variable that controls the background style, see [app.css](app.css)
4+
5+
![triggered color change](images/red.gif)
6+
7+
Find tests in [cypress/integration/spec.js](cypress/integration/spec.js) file.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:root {
2+
--main-color: #fff;
3+
--background-color: #000;
4+
}
5+
body {
6+
color: var(--main-color);
7+
background-color: var(--background-color);
8+
font-size: xx-large;
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* eslint-env browser */
2+
/* eslint-disable no-console */
3+
document.querySelector('input[type=color]').addEventListener('change', (e) => {
4+
console.log('setting new background color to: %s', e.target.value)
5+
document.documentElement.style.setProperty('--background-color', e.target.value)
6+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fixturesFolder": false,
3+
"supportFile": false,
4+
"pluginsFile": false,
5+
"viewportWidth": 300,
6+
"viewportHeight": 300
7+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference types="cypress" />
2+
/* eslint-disable mocha/no-global-tests */
3+
it('changes background color', () => {
4+
// when the app starts, the background is black
5+
cy.visit('index.html')
6+
// tip: add additional matchers like "chai-colors"
7+
// to express desired colors better
8+
// see recipe "Extending Cypress Chai Assertions"
9+
cy.get('body').should('have.css', 'background-color', 'rgb(0, 0, 0)')
10+
11+
// select the new color value in the <input type="color">
12+
// element and trigger "change" event
13+
cy.get('input[type=color]')
14+
.invoke('val', '#ff0000')
15+
.trigger('change')
16+
17+
// check the background color has been changed
18+
cy.get('body').should('have.css', 'background-color', 'rgb(255, 0, 0)')
19+
})
20+
21+
it('can spy on native methods', () => {
22+
cy.visit('index.html')
23+
cy.get('body').should('have.css', 'background-color', 'rgb(0, 0, 0)')
24+
25+
// Cypress has direct access to browser APIs
26+
// thus we can spy directly on method calls
27+
cy.document().its('documentElement.style')
28+
.then((style) => cy.spy(style, 'setProperty').as('setColor'))
29+
30+
cy.get('input[type=color]')
31+
.invoke('val', '#ff0000')
32+
.trigger('change')
33+
34+
cy.get('body').should('have.css', 'background-color', 'rgb(255, 0, 0)')
35+
// find spy by its alias and confirm it was called as expected
36+
cy.get('@setColor').should('have.been.calledWith', '--background-color', '#ff0000')
37+
38+
// tip: you can use Sinon match placeholders
39+
// for example, if you don't care precisely about '--background-color'
40+
// value, but know it should be a string, then use
41+
cy.get('@setColor').should('have.been.calledWith', Cypress.sinon.match.string, '#ff0000')
42+
})
1.84 MB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<head>
2+
<link rel="stylesheet" type="text/css" href="app.css" />
3+
</head>
4+
<body>
5+
This is the body of the document
6+
<div>Change color using this color input
7+
<input type="color">
8+
</div>
9+
<script src="app.js"></script>
10+
</body>
11+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "root-style",
3+
"version": "1.0.0",
4+
"description": "Tests how the app changes CSS variable using input color element",
5+
"scripts": {
6+
"cypress:open": "../../node_modules/.bin/cypress open",
7+
"cypress:run": "../../node_modules/.bin/cypress run",
8+
"test:ci": "npm run cypress:run"
9+
}
10+
}

0 commit comments

Comments
 (0)