2017-07-28 06:54:51 -04:00
|
|
|
import { isSticky } from '~/lib/utils/sticky';
|
|
|
|
|
|
|
|
describe('sticky', () => {
|
2017-09-25 06:40:40 -04:00
|
|
|
let el;
|
2017-07-28 06:54:51 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2017-09-25 06:55:33 -04:00
|
|
|
document.body.innerHTML += `
|
2017-09-25 06:40:40 -04:00
|
|
|
<div class="parent">
|
2017-09-25 06:55:33 -04:00
|
|
|
<div id="js-sticky"></div>
|
2017-09-25 06:40:40 -04:00
|
|
|
</div>
|
|
|
|
`;
|
2017-09-15 04:01:58 -04:00
|
|
|
|
2017-09-25 06:40:40 -04:00
|
|
|
el = document.getElementById('js-sticky');
|
2017-07-28 06:54:51 -04:00
|
|
|
});
|
|
|
|
|
2017-09-25 06:55:33 -04:00
|
|
|
afterEach(() => {
|
|
|
|
el.parentNode.remove();
|
|
|
|
});
|
|
|
|
|
2017-09-25 06:40:40 -04:00
|
|
|
describe('when stuck', () => {
|
|
|
|
it('does not remove is-stuck class', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
2017-07-28 06:54:51 -04:00
|
|
|
|
|
|
|
expect(
|
2017-09-25 06:40:40 -04:00
|
|
|
el.classList.contains('is-stuck'),
|
|
|
|
).toBeTruthy();
|
2017-07-28 06:54:51 -04:00
|
|
|
});
|
|
|
|
|
2017-09-25 06:40:40 -04:00
|
|
|
it('adds is-stuck class', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
2017-07-28 06:54:51 -04:00
|
|
|
|
|
|
|
expect(
|
2017-09-25 06:40:40 -04:00
|
|
|
el.classList.contains('is-stuck'),
|
|
|
|
).toBeTruthy();
|
2017-07-28 06:54:51 -04:00
|
|
|
});
|
2017-09-15 04:01:58 -04:00
|
|
|
|
2017-09-25 06:40:40 -04:00
|
|
|
it('inserts placeholder element', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop, true);
|
2017-09-15 04:01:58 -04:00
|
|
|
|
|
|
|
expect(
|
2017-09-25 06:40:40 -04:00
|
|
|
document.querySelector('.sticky-placeholder'),
|
|
|
|
).not.toBeNull();
|
2017-09-15 04:01:58 -04:00
|
|
|
});
|
2017-07-28 06:54:51 -04:00
|
|
|
});
|
|
|
|
|
2017-09-25 06:40:40 -04:00
|
|
|
describe('when not stuck', () => {
|
|
|
|
it('removes is-stuck class', () => {
|
|
|
|
spyOn(el.classList, 'remove').and.callThrough();
|
|
|
|
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
2017-07-28 06:54:51 -04:00
|
|
|
isSticky(el, 0, 0);
|
|
|
|
|
|
|
|
expect(
|
2017-09-25 06:40:40 -04:00
|
|
|
el.classList.remove,
|
2017-07-28 06:54:51 -04:00
|
|
|
).toHaveBeenCalledWith('is-stuck');
|
2017-09-25 06:40:40 -04:00
|
|
|
expect(
|
|
|
|
el.classList.contains('is-stuck'),
|
|
|
|
).toBeFalsy();
|
2017-07-28 06:54:51 -04:00
|
|
|
});
|
|
|
|
|
2017-09-25 06:40:40 -04:00
|
|
|
it('does not add is-stuck class', () => {
|
2017-07-28 06:54:51 -04:00
|
|
|
isSticky(el, 0, 0);
|
|
|
|
|
|
|
|
expect(
|
2017-09-25 06:40:40 -04:00
|
|
|
el.classList.contains('is-stuck'),
|
|
|
|
).toBeFalsy();
|
2017-07-28 06:54:51 -04:00
|
|
|
});
|
2017-09-15 04:01:58 -04:00
|
|
|
|
2017-09-25 06:40:40 -04:00
|
|
|
it('removes placeholder', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop, true);
|
2017-09-15 04:01:58 -04:00
|
|
|
isSticky(el, 0, 0, true);
|
|
|
|
|
|
|
|
expect(
|
2017-09-25 06:40:40 -04:00
|
|
|
document.querySelector('.sticky-placeholder'),
|
|
|
|
).toBeNull();
|
2017-09-15 04:01:58 -04:00
|
|
|
});
|
2017-07-28 06:54:51 -04:00
|
|
|
});
|
|
|
|
});
|