debian-mirror-gitlab/spec/javascripts/lib/utils/sticky_spec.js

80 lines
1.7 KiB
JavaScript
Raw Normal View History

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
expect(
2018-03-17 18:26:18 +05:30
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
expect(
2018-03-17 18:26:18 +05:30
el.classList.contains('is-stuck'),
).toBeTruthy();
});
it('inserts placeholder element', () => {
isSticky(el, 0, el.offsetTop, true);
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);
expect(
2018-03-17 18:26:18 +05:30
el.classList.remove,
2017-09-10 17:25:29 +05:30
).toHaveBeenCalledWith('is-stuck');
2018-03-17 18:26:18 +05:30
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);
expect(
2018-03-17 18:26:18 +05:30
el.classList.contains('is-stuck'),
).toBeFalsy();
});
it('removes placeholder', () => {
isSticky(el, 0, el.offsetTop, true);
isSticky(el, 0, 0, true);
expect(
document.querySelector('.sticky-placeholder'),
).toBeNull();
2017-09-10 17:25:29 +05:30
});
});
});