2020-05-19 09:08:12 +00:00
|
|
|
import { setHTMLFixture } from 'helpers/fixtures';
|
2020-08-17 21:09:56 +00:00
|
|
|
import { isSticky } from '~/lib/utils/sticky';
|
2020-05-19 09:08:12 +00:00
|
|
|
|
|
|
|
const TEST_OFFSET_TOP = 500;
|
2018-06-01 16:41:50 +00:00
|
|
|
|
|
|
|
describe('sticky', () => {
|
|
|
|
let el;
|
2020-05-19 09:08:12 +00:00
|
|
|
let offsetTop;
|
2018-06-01 16:41:50 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-05-19 09:08:12 +00:00
|
|
|
setHTMLFixture(
|
|
|
|
`
|
2018-06-01 16:41:50 +00:00
|
|
|
<div class="parent">
|
|
|
|
<div id="js-sticky"></div>
|
|
|
|
</div>
|
2020-05-19 09:08:12 +00:00
|
|
|
`,
|
|
|
|
);
|
2018-06-01 16:41:50 +00:00
|
|
|
|
2020-05-19 09:08:12 +00:00
|
|
|
offsetTop = TEST_OFFSET_TOP;
|
2018-06-01 16:41:50 +00:00
|
|
|
el = document.getElementById('js-sticky');
|
2020-05-19 09:08:12 +00:00
|
|
|
Object.defineProperty(el, 'offsetTop', {
|
|
|
|
get() {
|
|
|
|
return offsetTop;
|
|
|
|
},
|
|
|
|
});
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-05-19 09:08:12 +00:00
|
|
|
el = null;
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when stuck', () => {
|
|
|
|
it('does not remove is-stuck class', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(el.classList.contains('is-stuck')).toBeTruthy();
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('adds is-stuck class', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(el.classList.contains('is-stuck')).toBeTruthy();
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts placeholder element', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop, true);
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(document.querySelector('.sticky-placeholder')).not.toBeNull();
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when not stuck', () => {
|
|
|
|
it('removes is-stuck class', () => {
|
2020-05-19 09:08:12 +00:00
|
|
|
jest.spyOn(el.classList, 'remove');
|
2018-06-01 16:41:50 +00:00
|
|
|
|
|
|
|
isSticky(el, 0, el.offsetTop);
|
|
|
|
isSticky(el, 0, 0);
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(el.classList.remove).toHaveBeenCalledWith('is-stuck');
|
2020-05-19 09:08:12 +00:00
|
|
|
expect(el.classList.contains('is-stuck')).toBe(false);
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not add is-stuck class', () => {
|
|
|
|
isSticky(el, 0, 0);
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(el.classList.contains('is-stuck')).toBeFalsy();
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('removes placeholder', () => {
|
|
|
|
isSticky(el, 0, el.offsetTop, true);
|
|
|
|
isSticky(el, 0, 0, true);
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
expect(document.querySelector('.sticky-placeholder')).toBeNull();
|
2018-06-01 16:41:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|