Commit 734bb736 authored by Phil Hughes's avatar Phil Hughes

fixed and added specs for removing placeholder element

parent 7ce70dd5
...@@ -4,12 +4,25 @@ describe('sticky', () => { ...@@ -4,12 +4,25 @@ describe('sticky', () => {
const el = { const el = {
offsetTop: 0, offsetTop: 0,
classList: {}, classList: {},
parentNode: {},
nextElementSibling: {},
}; };
let isStuck = false;
beforeEach(() => { beforeEach(() => {
el.offsetTop = 0; el.offsetTop = 0;
el.classList.add = jasmine.createSpy('spy'); el.classList.add = jasmine.createSpy('classListAdd');
el.classList.remove = jasmine.createSpy('spy'); el.classList.remove = jasmine.createSpy('classListRemove');
el.classList.contains = jasmine.createSpy('classListContains').and.callFake(() => isStuck);
el.parentNode.insertBefore = jasmine.createSpy('insertBefore');
el.nextElementSibling.remove = jasmine.createSpy('nextElementSibling');
el.nextElementSibling.classList = {
contains: jasmine.createSpy('classListContains').and.returnValue(true),
};
});
afterEach(() => {
isStuck = false;
}); });
describe('classList.remove', () => { describe('classList.remove', () => {
...@@ -21,7 +34,9 @@ describe('sticky', () => { ...@@ -21,7 +34,9 @@ describe('sticky', () => {
).not.toHaveBeenCalled(); ).not.toHaveBeenCalled();
}); });
it('calls classList.remove when not stuck', () => { it('calls classList.remove when no longer stuck', () => {
isStuck = true;
el.offsetTop = 10; el.offsetTop = 10;
isSticky(el, 0, 0); isSticky(el, 0, 0);
...@@ -29,6 +44,17 @@ describe('sticky', () => { ...@@ -29,6 +44,17 @@ describe('sticky', () => {
el.classList.remove, el.classList.remove,
).toHaveBeenCalledWith('is-stuck'); ).toHaveBeenCalledWith('is-stuck');
}); });
it('removes placeholder when no longer stuck', () => {
isStuck = true;
el.offsetTop = 10;
isSticky(el, 0, 0, true);
expect(
el.nextElementSibling.remove,
).toHaveBeenCalled();
});
}); });
describe('classList.add', () => { describe('classList.add', () => {
...@@ -48,5 +74,13 @@ describe('sticky', () => { ...@@ -48,5 +74,13 @@ describe('sticky', () => {
el.classList.add, el.classList.add,
).not.toHaveBeenCalled(); ).not.toHaveBeenCalled();
}); });
it('inserts placeholder element when stuck', () => {
isSticky(el, 0, 0, true);
expect(
el.parentNode.insertBefore,
).toHaveBeenCalled();
});
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment