From 26281751ec167fb5ebbdd4be57a28a990b6894bc Mon Sep 17 00:00:00 2001 From: Antonio Laguna Date: Mon, 29 May 2017 23:35:05 +0200 Subject: [PATCH] Testing love --- src/js/utils/custom-event.js | 3 +++ test/utils/dom.test.js | 19 ++++++++++++++- test/utils/scroll-to.test.js | 45 ++++++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/js/utils/custom-event.js b/src/js/utils/custom-event.js index e032cde..df0807b 100644 --- a/src/js/utils/custom-event.js +++ b/src/js/utils/custom-event.js @@ -15,6 +15,7 @@ function canIuseNativeCustom() { return 't' === p.type && 'b' === p.detail.a; } catch (e) { } + /* istanbul ignore next: hard to reproduce on test environment */ return false; } @@ -25,6 +26,7 @@ function canIuseNativeCustom() { * @return {Event} * @constructor */ +/* istanbul ignore next: hard to reproduce on test environment */ const IECustomEvent = function CustomEvent(type, params) { const e = document.createEvent('CustomEvent'); @@ -37,6 +39,7 @@ const IECustomEvent = function CustomEvent(type, params) { return e; }; +/* istanbul ignore next: hard to reproduce on test environment */ const WSCustomEvent = canIuseNativeCustom() ? NativeCustomEvent : IECustomEvent; export default WSCustomEvent; diff --git a/test/utils/dom.test.js b/test/utils/dom.test.js index a83f2fa..dcca928 100644 --- a/test/utils/dom.test.js +++ b/test/utils/dom.test.js @@ -191,6 +191,23 @@ describe('Show/hide', () => { DOM.hide(el); expect(el.style.display).toBe('none'); }); + + test('Is visible', () => { + // offsetParent doesn't work nice with JSDom + const el = DOM.createNode('div'); + let offsetParent = document.body; + el.style.display = 'block'; + + document.body.appendChild(el); + Object.defineProperty(el, 'offsetParent', {get: () => offsetParent}); + expect(DOM.isVisible(el)).toBe(true); + + DOM.hide(el); + offsetParent = null; + + expect(DOM.isVisible(el)).toBe(false); + document.body.removeChild(el); + }); }); describe('Custom Event', () => { @@ -229,7 +246,7 @@ describe('To Array', () => { }); }); -describe('Focusble Element', () => { +describe('Focusable Element', () => { beforeEach(() => { document.body.innerHTML = `

diff --git a/test/utils/scroll-to.test.js b/test/utils/scroll-to.test.js index c01c4d2..c8cfcec 100644 --- a/test/utils/scroll-to.test.js +++ b/test/utils/scroll-to.test.js @@ -7,17 +7,52 @@ beforeAll(() => { document.body.innerHTML = `
${brs}
`; }); -test('ScrollTo utility', () => { +afterAll(() => { + jest.clearAllTimers(); +}); + +test('ScrollTo with defaults', () => { const ws = document.getElementById('webslides'); - const cb = jest.fn(); - scrollTo(100, 500, cb); + scrollTo(100); - expect(cb).not.toBeCalled(); expect(ws.scrollTop).toBe(0); + jest.runTimersToTime(400); + + expect(ws.scrollTop).toBeLessThan(100); + jest.runAllTimers(); - expect(cb).toBeCalled(); expect(ws.scrollTop).toBe(100); }); + +test('ScrollTo with custom duration', () => { + const ws = document.getElementById('webslides'); + ws.scrollTop = 0; + scrollTo(100, 2000); + + expect(ws.scrollTop).toBe(0); + jest.runTimersToTime(500); + expect(ws.scrollTop).toBeLessThan(100); + jest.runTimersToTime(700); + expect(ws.scrollTop).toBeLessThan(100); + jest.runAllTimers(); + + expect(ws.scrollTop).toBe(100); +}); + +test('ScrollTo with custom callback', () => { + const ws = document.getElementById('webslides'); + ws.scrollTop = 0; + const cb = jest.fn(); + + scrollTo(100, 500, cb); + expect(ws.scrollTop).toBe(0); + expect(cb).not.toBeCalled(); + + jest.runAllTimers(); + + expect(ws.scrollTop).toBe(100); + expect(cb).toBeCalled(); +});