import { isSticky } from '~/lib/utils/sticky';

describe('sticky', () => {
  let el;

  beforeEach(() => {
    document.body.innerHTML = `
      <div class="parent">
        <div id="js-sticky" style="position: relative;"></div>
      </div>
    `;

    el = document.getElementById('js-sticky');
  });

  describe('when stuck', () => {
    it('does not remove is-stuck class', () => {
      isSticky(el, 0, el.offsetTop);
      isSticky(el, 0, el.offsetTop);

      expect(
        el.classList.contains('is-stuck'),
      ).toBeTruthy();
    });

    it('adds is-stuck class', () => {
      isSticky(el, 0, el.offsetTop);

      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');
      expect(
        el.classList.contains('is-stuck'),
      ).toBeFalsy();
    });

    it('does not add is-stuck class', () => {
      isSticky(el, 0, 0);

      expect(
        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();
    });
  });
});

@@ -128,16 +128,17 @@
 nerHTML 
++
 = %60%0A    
@@ -191,36 +191,8 @@
 cky%22
- style=%22position: relative;%22
 %3E%3C/d
@@ -262,32 +262,87 @@
 ticky');%0A  %7D);%0A%0A
+  afterEach(() =%3E %7B%0A    el.parentNode.remove();%0A  %7D);%0A%0A
   describe('when

remove the sticky el from the test DOM after each test