From f389f9081f92b68d8d8369d29e8f05a65f47e9dc Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Mon, 25 Sep 2017 11:40:40 +0100 Subject: [PATCH] refactor tests to actually test browser behaviour --- app/assets/javascripts/lib/utils/sticky.js | 2 +- spec/javascripts/lib/utils/sticky_spec.js | 99 ++++++++++------------ 2 files changed, 45 insertions(+), 56 deletions(-) diff --git a/app/assets/javascripts/lib/utils/sticky.js b/app/assets/javascripts/lib/utils/sticky.js index 2803939c7cd..64db42701ce 100644 --- a/app/assets/javascripts/lib/utils/sticky.js +++ b/app/assets/javascripts/lib/utils/sticky.js @@ -22,7 +22,7 @@ export const isSticky = (el, scrollY, stickyTop, insertPlaceholder) => { } else if (top > stickyTop && el.classList.contains('is-stuck')) { el.classList.remove('is-stuck'); - if (insertPlaceholder && el.nextElementSibling.classList.contains('sticky-placeholder')) { + if (insertPlaceholder && el.nextElementSibling && el.nextElementSibling.classList.contains('sticky-placeholder')) { el.nextElementSibling.remove(); } } diff --git a/spec/javascripts/lib/utils/sticky_spec.js b/spec/javascripts/lib/utils/sticky_spec.js index 5ffc41589e5..8ddf86c4ee4 100644 --- a/spec/javascripts/lib/utils/sticky_spec.js +++ b/spec/javascripts/lib/utils/sticky_spec.js @@ -1,86 +1,75 @@ import { isSticky } from '~/lib/utils/sticky'; describe('sticky', () => { - const el = { - offsetTop: 0, - classList: {}, - parentNode: {}, - nextElementSibling: {}, - }; - let isStuck = false; + let el; beforeEach(() => { - el.offsetTop = 0; - el.classList.add = jasmine.createSpy('classListAdd'); - 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), - }; + document.body.innerHTML = ` +
+
+
+ `; + + el = document.getElementById('js-sticky'); }); - afterEach(() => { - isStuck = false; - }); - - describe('classList.remove', () => { - it('does not call classList.remove when stuck', () => { - isSticky(el, 0, 0); + describe('when stuck', () => { + it('does not remove is-stuck class', () => { + isSticky(el, 0, el.offsetTop); + isSticky(el, 0, el.offsetTop); expect( - el.classList.remove, - ).not.toHaveBeenCalled(); + el.classList.contains('is-stuck'), + ).toBeTruthy(); }); - it('calls classList.remove when no longer stuck', () => { - isStuck = true; + it('adds is-stuck class', () => { + isSticky(el, 0, el.offsetTop); - el.offsetTop = 10; + expect( + el.classList.contains('is-stuck'), + ).toBeTruthy(); + }); + + it('inserts placeholder element', () => { + isSticky(el, 0, el.offsetTop, true); + + expect( + document.querySelector('.sticky-placeholder'), + ).not.toBeNull(); + }); + }); + + describe('when not stuck', () => { + it('removes is-stuck class', () => { + spyOn(el.classList, 'remove').and.callThrough(); + + isSticky(el, 0, el.offsetTop); isSticky(el, 0, 0); expect( el.classList.remove, ).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(); + el.classList.contains('is-stuck'), + ).toBeFalsy(); }); - }); - describe('classList.add', () => { - it('calls classList.add when stuck', () => { + it('does not add is-stuck class', () => { isSticky(el, 0, 0); expect( - el.classList.add, - ).toHaveBeenCalledWith('is-stuck'); + el.classList.contains('is-stuck'), + ).toBeFalsy(); }); - it('does not call classList.add when not stuck', () => { - el.offsetTop = 10; - isSticky(el, 0, 0); - - expect( - el.classList.add, - ).not.toHaveBeenCalled(); - }); - - it('inserts placeholder element when stuck', () => { + it('removes placeholder', () => { + isSticky(el, 0, el.offsetTop, true); isSticky(el, 0, 0, true); expect( - el.parentNode.insertBefore, - ).toHaveBeenCalled(); + document.querySelector('.sticky-placeholder'), + ).toBeNull(); }); }); });