forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss-help.test.ts
More file actions
96 lines (95 loc) · 3.35 KB
/
css-help.test.ts
File metadata and controls
96 lines (95 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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('getStyle', () => {
it('should return an ExtendedCSSStyleDeclartion object of length 1', () => {
expect(t.getStyle('*')?.length).toEqual(1);
});
it('should return a non-empty ExtendedCSSStyleDeclaration object', () => {
expect(t.getStyle('.bb1')).toBeTruthy();
});
it('should return a whitespaceless string', () => {
expect(t.getStyle('.bb1d')?.getPropVal('background', true)).toEqual(
'linear-gradient(var(--building-color1)50%,var(--window-color1))'
);
});
});
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.getStyle(':root')?.getPropertyValue('--building-color1')?.trim()
).toEqual('#aa80ff');
});
it('should return value to existing property', () => {
expect(
t.getStyle('.bb4a')?.getPropertyValue('background-color')
).toBeTruthy();
});
it('should return property value without evaluating result', () => {
expect(t.getStyle('.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 = '';
});
});