-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSizePercent.test.js
77 lines (72 loc) · 2.47 KB
/
getSizePercent.test.js
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
describe('getSizePercent', () => {
const sizeProperties = ['width', 'height'];
sizeProperties.forEach(sizeProperty => {
describe(sizeProperty, () => {
const expectedPercent = 30;
let element;
let markup = `
<style>
.parent {
${sizeProperty}: 100px;
}
.parent .target {
${sizeProperty}: ${expectedPercent}%;
}
</style>
<div class="parent">
<div class="target"></div>
</div>
`;
beforeEach(() => {
element = document.createElement('div');
element.innerHTML = markup;
document.body.appendChild(element);
});
afterEach(() => {
element.remove();
});
it(`should get element's ${sizeProperty}`, () => {
const percentWidth = getSizePercent({ selector: '.parent .target', property: sizeProperty })
expect(percentWidth).to.equal(`${expectedPercent}%`);
});
const differentWidths = ['80px', '120px', '20em', '40vw'];
differentWidths.forEach(parentWidth => {
it(`should get element's ${sizeProperty} when parent is ${parentWidth}`, () => {
element.querySelector('.parent').style[sizeProperty] = parentWidth;
const percentWidth = getSizePercent({ selector: '.parent .target', property: sizeProperty })
expect(percentWidth).to.equal(`${expectedPercent}%`);
});
});
describe('with pseudo element', () => {
beforeEach(() => {
markup = `
<style>
.target {
${sizeProperty}: 80px;
}
.target::after {
content: '';
display: block;
${sizeProperty}: ${expectedPercent}%;
}
</style>
<div class="target"></div>
`;
element = document.createElement('div');
element.innerHTML = markup;
document.body.appendChild(element);
});
afterEach(() => {
element.remove();
});
differentWidths.forEach(parentWidth => {
it(`should get element's ${sizeProperty} when parent is ${parentWidth}`, () => {
element.querySelector('.target').style[sizeProperty] = parentWidth;
const percentWidth = getSizePercent({ selector: '.target::after', property: sizeProperty })
expect(percentWidth).to.equal(`${expectedPercent}%`);
});
});
});
});
});
});