|
| 1 | +import { HoverBox } from '../src/Index'; |
| 2 | + |
| 3 | +jest.useFakeTimers(); |
| 4 | + |
| 5 | +describe('testing index file', () => { |
| 6 | + let boxEl: HTMLDivElement, hoverBox: HoverBox; |
| 7 | + |
| 8 | + boxEl = document.createElement('div'); |
| 9 | + |
| 10 | + boxEl.classList.add('test'); |
| 11 | + boxEl = document.body.insertAdjacentElement( |
| 12 | + 'beforeend', |
| 13 | + boxEl, |
| 14 | + ) as HTMLInputElement; |
| 15 | + |
| 16 | + // Add HoverData (which would be revealed) |
| 17 | + const div = document.createElement('div'); |
| 18 | + div.classList.add('HoverData'); |
| 19 | + div.innerHTML = 'Test data'; |
| 20 | + div.hidden = true; |
| 21 | + boxEl.appendChild(div); |
| 22 | + |
| 23 | + describe('Post-setup checks', () => { |
| 24 | + hoverBox = new HoverBox('.test', { |
| 25 | + transitionDelay: 0, |
| 26 | + transitionDuration: 0, |
| 27 | + }); |
| 28 | + |
| 29 | + it('has added element', () => { |
| 30 | + expect(boxEl).not.toBeNull(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('has created hoverBox', () => { |
| 34 | + expect(hoverBox).toBeDefined(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + test('Initial state = stopped', () => |
| 39 | + // @ts-ignore |
| 40 | + expect(hoverBox._listening).toBe(true)); |
| 41 | + |
| 42 | + describe('Event', () => { |
| 43 | + const eventFired = boxEl.dispatchEvent(new Event('mouseenter')); |
| 44 | + |
| 45 | + jest.advanceTimersByTime(1); |
| 46 | + |
| 47 | + it('should fire', () => expect(eventFired).toBe(true)); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('After event - hoverContainer', () => { |
| 51 | + const hoverCont = document.getElementById(boxEl.dataset.hoverBoxId!); |
| 52 | + |
| 53 | + it('exists', () => expect(hoverCont).toBeDefined()); |
| 54 | + |
| 55 | + it('is on display', () => |
| 56 | + expect(hoverCont?.style.display).toBe('block')); |
| 57 | + |
| 58 | + // Jest doesn't have a renderer so just make sure it's defined (always 0px) |
| 59 | + it('has position', () => expect(hoverCont?.style.top).toBeDefined()); |
| 60 | + }); |
| 61 | +}); |
0 commit comments