2020-05-24 23:13:21 +05:30
|
|
|
import { setHTMLFixture } from 'helpers/fixtures';
|
2020-10-24 23:57:45 +05:30
|
|
|
import { isSticky } from '~/lib/utils/sticky';
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
const TEST_OFFSET_TOP = 500;
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
describe('sticky', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
let el;
|
2020-05-24 23:13:21 +05:30
|
|
|
let offsetTop;
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
setHTMLFixture(
|
|
|
|
`
|
2018-03-17 18:26:18 +05:30
|
|
|
<div class="parent">
|
|
|
|
<div id="js-sticky"></div>
|
|
|
|
</div>
|
2020-05-24 23:13:21 +05:30
|
|
|
`,
|
|
|
|
);
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
offsetTop = TEST_OFFSET_TOP;
|
2018-03-17 18:26:18 +05:30
|
|
|
el = document.getElementById('js-sticky');
|
2020-05-24 23:13:21 +05:30
|
|
|
Object.defineProperty(el, 'offsetTop', {
|
|
|
|
get() {
|
|
|
|
return offsetTop;
|
|
|
|
},
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
afterEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
el = null;
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(el.classList, 'remove');
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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');
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(el.classList.contains('is-stuck')).toBe(false);
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|