-
-
Save iqbmo04/7629318000311fb1473fc650ada5a680 to your computer and use it in GitHub Desktop.
Cypress assertion to compare colors
This file contains hidden or 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 hidden or 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); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment