Files
freeCodeCamp/client/src/utils/css-help.test.ts
Shaun Hamilton d5cc9ba259 feat(dev): add css helper function (#42148)
* feat(dev): add css helper function

* add media declaration methods

* use a selector to grab styles

* add CSSHelp class with annoying TS warnings (no-verify)

* replace indexing with getPropertyValue

Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>

* connect CSSHelp to helpers, clean up types

* fix: return extended object, not modify prototype

* fix: unnecessary assertion error

* add tests with fixtures

* add afterAll document clean-up

* 'fix' tests

* undo jest transform config

* remove erroneous comments and logs

* reafactor: use existing types and narrow types

* refactor test descriptions

* rename files to appease Mrugesh 😄

* remove probably unnecessary document clean

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2021-05-25 07:34:55 -07:00

96 lines
3.2 KiB
TypeScript

/* global describe it expect */
import { cssString } from './__fixtures/curriculum-helpers-css';
import CSSHelp from './css-help';
describe('css-help', () => {
const doc = document;
let t: CSSHelp;
beforeEach(() => {
const style = doc.createElement('style');
style.innerHTML = cssString as string;
doc.head.appendChild(style);
t = new CSSHelp(doc);
// JSDOM does not implement latest CSSOM spec. As such,
// conditionText property needs to be manually added.
// REF: https://github.com/freeCodeCamp/freeCodeCamp/pull/42148#issuecomment-847291137
const mediaRule = t.getCSSRules('media')?.[0] as CSSMediaRule;
const conditionText = mediaRule.media[0];
mediaRule.conditionText = conditionText;
});
describe('getStyleDeclaration', () => {
it('should return a CSSStyleDeclartion array of length 1', () => {
expect(t.getStyleDeclarations('*')?.length).toEqual(1);
});
it('should return a non-empty CSSStyleDeclaration array', () => {
expect(t.getStyleDeclaration('.bb1')).toBeTruthy();
});
});
describe('isPropertyUsed', () => {
it('should return true on existing properties', () => {
expect(t.isPropertyUsed('height')).toBeTruthy();
});
it('should return true on existing custom properties', () => {
expect(t.isPropertyUsed('--building-color1')).toBeTruthy();
});
});
describe('isDeclaredAfter', () => {
it('should return true if existing style is declared after another', () => {
expect(t.getStyleRule('.bb1a')?.isDeclaredAfter('.bb1')).toBeTruthy();
});
});
describe('getPropertyValue', () => {
it('should return custom property value needing trim', () => {
expect(
t
.getStyleDeclaration(':root')
?.getPropertyValue('--building-color1')
.trim()
).toEqual('#aa80ff');
});
it('should return value to existing property', () => {
expect(
t.getStyleDeclaration('.bb4a')?.getPropertyValue('background-color')
).toBeTruthy();
});
it('should return property value without evaluating result', () => {
expect(
t.getStyleDeclaration('.bb4a')?.getPropertyValue('background-color')
).toEqual('var(--building-color4)');
});
});
describe('getCSSRules', () => {
it('should return a CSSRules array of length 1', () => {
expect(t.getCSSRules('media')?.length).toEqual(1);
});
});
describe('getRuleListsWithinMedia', () => {
it('should return a CSSMediaRule array with a selectable CSSStyleRule', () => {
expect(
t
.getRuleListsWithinMedia('(max-width: 1000px)')
.find(x => x.selectorText === '.sky')
).toBeTruthy();
});
it('should return CSSStyleDeclaration property with complex value', () => {
// NOTE: JSDOM causes value to have tabbed characters, DOM has single-line values.
expect(
t
.getRuleListsWithinMedia('(max-width: 1000px)')
.find(x => x.selectorText === '.sky')?.style?.background
).toEqual(
`radial-gradient(
closest-corner circle at 15% 15%,
#ffcf33,
#ffcf33 20%,
#ffff66 21%,
#bbeeff 100%
)`
);
});
});
afterEach(() => {
document.body.innerHTML = '';
document.head.innerHTML = '';
});
});