Last active
November 8, 2023 15:50
-
-
Save NicholasBoll/e584991b36986a85acf0e95101752bc0 to your computer and use it in GitHub Desktop.
Cypress assertion to compare colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compareColor = (color, property) => (targetElement) => { | |
const tempElement = document.createElement('div'); | |
tempElement.style.color = color; | |
tempElement.style.display = 'none'; // make sure it doesn't actually render | |
document.body.appendChild(tempElement); // append so that `getComputedStyle` actually works | |
const tempColor = getComputedStyle(tempElement).color; | |
const targetColor = getComputedStyle(targetElement[0])[property]; | |
document.body.removeChild(tempElement); // remove it because we're done with it | |
expect(tempColor).to.equal(targetColor); | |
}; | |
Cypress.Commands.overwrite('should', (originalFn, subject, expectation, ...args) => { | |
const customMatchers = { | |
'have.backgroundColor': compareColor(args[0], 'backgroundColor'), | |
'have.color': compareColor(args[0], 'color'), | |
}; | |
// See if the expectation is a string and if it is a member of Jest's expect | |
if (typeof expectation === 'string' && customMatchers[expectation]) { | |
return originalFn(subject, customMatchers[expectation]); | |
} | |
return originalFn(subject, expectation, ...args); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compareColor = (color: string, property: keyof CSSStyleDeclaration) => ( | |
targetElement: JQuery | |
) => { | |
const tempElement = document.createElement('div'); | |
tempElement.style.color = color; | |
tempElement.style.display = 'none'; // make sure it doesn't actually render | |
document.body.appendChild(tempElement); // append so that `getComputedStyle` actually works | |
const tempColor = getComputedStyle(tempElement).color; | |
const targetColor = getComputedStyle(targetElement[0])[property]; | |
document.body.removeChild(tempElement); // remove it because we're done with it | |
expect(tempColor).to.equal(targetColor); | |
}; | |
Cypress.Commands.overwrite('should', (originalFn: Function, subject: any, expectation: any, ...args: any[]) => { | |
const customMatchers = { | |
'have.backgroundColor': compareColor(args[0], 'backgroundColor'), | |
'have.color': compareColor(args[0], 'color'), | |
}; | |
// See if the expectation is a string and if it is a member of Jest's expect | |
if (typeof expectation === 'string' && customMatchers[expectation]) { | |
return originalFn(subject, customMatchers[expectation]); | |
} | |
return originalFn(subject, expectation, ...args); | |
}); |
No, We haven't changed the atlas incoming color for ex: #455D82 if this is the color in the atlas in UI it will render as same RGB(69, 93, 130)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are you able to verify on a simpler test case? The snippet was made to solve the problem where the browser might force a different color return than the one you enter. For example, you may set the color to a hex, but get back an rgba from
getComputedStyle
. So the function creates a temporary element and sets the color and usesgetComputedStyle
so you're using the browser`s color space preference instead of your own. A color difference is unexpected. Is Atlas changing incoming colors to meet accessible contrast requirements?