Skip to content

Commit a205b96

Browse files
chuckcarpenterRobbieTheWagner
authored andcommitted
test: Add more coverage to the module (#375)
* test: Add more coverage to the module * test: Remove commented mock * test: Update to match PR comments
1 parent 0d49bf0 commit a205b96

3 files changed

Lines changed: 159 additions & 1 deletion

File tree

src/js/constraint.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const BOUNDS_FORMAT = ['left', 'top', 'right', 'bottom'];
1313
* @return {*[]|HTMLElement|ActiveX.IXMLDOMElement}
1414
*/
1515
function getBoundingRect(body, tether, to) {
16+
// arg to is required
17+
if (!to) {
18+
return null;
19+
}
1620
if (to === 'scrollParent') {
1721
to = tether.scrollParents[0];
1822
} else if (to === 'window') {

src/js/utils/classes.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export function addClass(el, name) {
1616
*/
1717
export function getClass(key = '', classes, classPrefix) {
1818
if (!isUndefined(classes) && !isUndefined(classes[key])) {
19-
2019
if (classes[key] === false) {
2120
return '';
2221
}

test/unit/constraint.spec.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,97 @@
11
import Constraint from '../../src/js/constraint.js';
22

33
describe('Constraint', () => {
4+
describe('getBoundingRect()', () => {
5+
const getBoundingRect = Constraint.__get__('getBoundingRect');
6+
let element;
7+
8+
beforeEach(() => {
9+
element = document.createElement('div');
10+
element.classList.add('element');
11+
document.body.appendChild(element);
12+
});
13+
14+
afterEach(() => {
15+
document.body.removeChild(element);
16+
element = null;
17+
});
18+
19+
it('returns null with no args', () => {
20+
expect(getBoundingRect()).toBeNull();
21+
});
22+
23+
it('return bounds from border width when constraint is scrollParent', () => {
24+
element.style.borderWidth = '4px';
25+
const scrollParentBounds = getBoundingRect(document.body, { scrollParents: [element] }, 'scrollParent');
26+
27+
expect(scrollParentBounds).toHaveLength(4);
28+
expect(scrollParentBounds).toEqual(expect.arrayContaining([4, 4, -4, -4]));
29+
});
30+
31+
it('return bounds from current window when constraint is window', () => {
32+
const windowBounds = getBoundingRect(document.body, { scrollParents: [element] }, 'window');
33+
34+
expect(windowBounds).toHaveLength(4);
35+
expect(windowBounds).toEqual(expect.arrayContaining([0, 0, 1024, 768]));
36+
});
37+
38+
it('return bounds from document window when constraint is document', () => {
39+
document.documentElement.style.borderWidth = '0';
40+
const windowBounds = getBoundingRect(document.body, { scrollParents: [] }, document);
41+
42+
expect(windowBounds).toHaveLength(4);
43+
expect(windowBounds).toEqual(expect.arrayContaining([0, 0, 0, 0]));
44+
});
45+
});
46+
47+
describe('_addOutOfBoundsClass()', () => {
48+
const _addOutOfBoundsClass = Constraint.__get__('_addOutOfBoundsClass');
49+
let oob;
50+
51+
beforeEach(() => {
52+
oob = [];
53+
});
54+
55+
56+
it('adds nothing if out of bounds array is empty', () => {
57+
_addOutOfBoundsClass(oob, [], [], '', '');
58+
59+
expect(oob).toHaveLength(0);
60+
});
61+
62+
it('does not add a class if oob class option is false', () => {
63+
oob.push('top');
64+
const addClasses = [];
65+
_addOutOfBoundsClass(oob, addClasses, {
66+
'out-of-bounds': false
67+
});
68+
69+
expect(addClasses).toHaveLength(2);
70+
expect(addClasses).toEqual(expect.arrayContaining(['', '-top']));
71+
});
72+
it('adds classes for oob prefix and options classes', () => {
73+
oob.push('top');
74+
const addClasses = [];
75+
_addOutOfBoundsClass(oob, addClasses, {
76+
'out-of-bounds': 'added'
77+
});
78+
79+
expect(addClasses).toHaveLength(2);
80+
expect(addClasses).toEqual(expect.arrayContaining(['added', 'added-top']));
81+
});
82+
83+
it('uses extra prefix for outOfBoundsClass', () => {
84+
oob.push('top');
85+
const addClasses = [];
86+
_addOutOfBoundsClass(oob, addClasses, {
87+
'out-of-bounds': 'added'
88+
}, '', 'extra');
89+
90+
expect(addClasses).toHaveLength(2);
91+
expect(addClasses).toEqual(expect.arrayContaining(['extra', 'extra-top']));
92+
});
93+
});
94+
495
describe('_calculateOOBAndPinnedLeft', () => {
596
const _calculateOOBAndPinnedLeft = Constraint.__get__('_calculateOOBAndPinnedLeft');
697
const bounds = [10, 10, 20, 20];
@@ -293,4 +384,68 @@ describe('Constraint', () => {
293384
});
294385
});
295386
});
387+
388+
describe('_getAllClasses', () => {
389+
const _getAllClasses = Constraint.__get__('_getAllClasses');
390+
391+
it('returns all the base classes when no changes passed', () => {
392+
const baseClasses = _getAllClasses({}, '', []);
393+
394+
expect(baseClasses).toHaveLength(10);
395+
expect(baseClasses).toEqual(expect.arrayContaining(['pinned',
396+
'out-of-bounds',
397+
'pinned-left',
398+
'pinned-top',
399+
'pinned-right',
400+
'pinned-bottom',
401+
'out-of-bounds-left',
402+
'out-of-bounds-top',
403+
'out-of-bounds-right',
404+
'out-of-bounds-bottom']));
405+
});
406+
407+
it('returns all the base classes with the passed prefix', () => {
408+
const prefixClasses = _getAllClasses({}, 'prefix', []);
409+
410+
expect(prefixClasses).toHaveLength(10);
411+
expect(prefixClasses).toEqual(expect.arrayContaining([
412+
'prefix-pinned',
413+
'prefix-out-of-bounds',
414+
'prefix-pinned-left',
415+
'prefix-pinned-top',
416+
'prefix-pinned-right',
417+
'prefix-pinned-bottom',
418+
'prefix-out-of-bounds-left',
419+
'prefix-out-of-bounds-top',
420+
'prefix-out-of-bounds-right',
421+
'prefix-out-of-bounds-bottom']));
422+
});
423+
424+
it('replaces a class when a replacement name is passed', () => {
425+
const replaceClass = _getAllClasses({
426+
'pinned': 'stuck'
427+
}, '', []);
428+
429+
expect(replaceClass).toHaveLength(10);
430+
expect(replaceClass).toEqual(expect.arrayContaining([
431+
'stuck',
432+
'out-of-bounds',
433+
'stuck-left',
434+
'stuck-top',
435+
'stuck-right',
436+
'stuck-bottom',
437+
'out-of-bounds-left',
438+
'out-of-bounds-top',
439+
'out-of-bounds-right',
440+
'out-of-bounds-bottom']));
441+
});
442+
443+
it('adds a constraint class and variations for sides', () => {
444+
const constraintClasses = _getAllClasses({}, '', [{ outOfBoundsClass: 'constraintOob' }]);
445+
446+
expect(constraintClasses).toHaveLength(15);
447+
expect(constraintClasses).toContain('constraintOob');
448+
expect(constraintClasses).toContain('constraintOob-top');
449+
});
450+
});
296451
});

0 commit comments

Comments
 (0)