2017-09-10 17:25:29 +05:30
|
|
|
import { isSticky } from '~/lib/utils/sticky';
|
|
|
|
|
|
|
|
describe('sticky', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
let el;
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-03-17 18:26:18 +05:30
|
|
|
document.body.innerHTML += `
|
|
|
|
<div class="parent">
|
|
|
|
<div id="js-sticky"></div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
el = document.getElementById('js-sticky');
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
afterEach(() => {
|
|
|
|
el.parentNode.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when stuck', () => {
|
|
|
|
it('does not remove is-stuck class', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(el.classList.contains('is-stuck')).toBeTruthy();
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('adds is-stuck class', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(el.classList.contains('is-stuck')).toBeTruthy();
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts placeholder element', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop, true);
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(document.querySelector('.sticky-placeholder')).not.toBeNull();
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe('when not stuck', () => {
|
|
|
|
it('removes is-stuck class', () => {
|
|
|
|
spyOn(el.classList, 'remove').and.callThrough();
|
|
|
|
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
2017-09-10 17:25:29 +05:30
|
|
|
isSticky(el, 0, 0);
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(el.classList.remove).toHaveBeenCalledWith('is-stuck');
|
|
|
|
|
|
|
|
expect(el.classList.contains('is-stuck')).toBeFalsy();
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('does not add is-stuck class', () => {
|
2017-09-10 17:25:29 +05:30
|
|
|
isSticky(el, 0, 0);
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(el.classList.contains('is-stuck')).toBeFalsy();
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('removes placeholder', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop, true);
|
|
|
|
isSticky(el, 0, 0, true);
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(document.querySelector('.sticky-placeholder')).toBeNull();
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|