From 6a70457ca02baf92867ca19b7c63a719c29697f2 Mon Sep 17 00:00:00 2001 From: William Durand Date: Wed, 20 Jun 2018 23:12:18 +0200 Subject: [PATCH] fixes --- src/amo/components/ScreenShots.js | 18 ++++++++++---- tests/unit/amo/components/TestScreenShots.js | 25 ++++++++++++++++---- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/amo/components/ScreenShots.js b/src/amo/components/ScreenShots.js index bd1a7dc833d..13e038ef567 100644 --- a/src/amo/components/ScreenShots.js +++ b/src/amo/components/ScreenShots.js @@ -14,6 +14,11 @@ type ThumbBounds = false | {| y: number, |}; +type GetThumbBoundsExtraParams = {| + _document: typeof document | null, + _window: typeof window | null, +|}; + export const PHOTO_SWIPE_OPTIONS = { closeEl: true, captionEl: true, @@ -25,11 +30,14 @@ export const PHOTO_SWIPE_OPTIONS = { preloaderEl: true, // Overload getThumbBoundsFn as workaround to // https://github.com/minhtranite/react-photoswipe/issues/23 - getThumbBoundsFn: ( - index: number, - _document: typeof document = document, - _window: typeof window = window - ): ThumbBounds => { + getThumbBoundsFn: (index: number, { + _document = typeof document !== 'undefined' ? document : null, + _window = typeof window !== 'undefined' ? window : null, + }: GetThumbBoundsExtraParams = {}): ThumbBounds => { + if (!_document || !_window) { + return false; + } + const thumbnail = _document.querySelectorAll('.pswp-thumbnails')[index]; if (thumbnail && thumbnail.getElementsByTagName) { diff --git a/tests/unit/amo/components/TestScreenShots.js b/tests/unit/amo/components/TestScreenShots.js index ad674a2f299..8eb45c35916 100644 --- a/tests/unit/amo/components/TestScreenShots.js +++ b/tests/unit/amo/components/TestScreenShots.js @@ -133,6 +133,21 @@ describe(__filename, () => { expect(bounds).toEqual(false); }); + it('returns false if _document is null', () => { + const bounds = getThumbBoundsFn(0, { _document: null }); + + expect(bounds).toEqual(false); + }); + + it('returns false if _window is null', () => { + const bounds = getThumbBoundsFn(0, { + _document: getFakeDocument({ left: 1, top: 2, width: 3 }), + _window: null, + }); + + expect(bounds).toEqual(false); + }); + it('returns an object with x, y and w values', () => { const left = 123; const top = 124; @@ -140,9 +155,8 @@ describe(__filename, () => { const fakeDocument = getFakeDocument({ left, top, width }); - const bounds = getThumbBoundsFn(0, fakeDocument); + const bounds = getThumbBoundsFn(0, { _document: fakeDocument }); - expect(bounds).toBeInstanceOf(Object); expect(bounds).toEqual({ w: width, x: left, @@ -161,7 +175,10 @@ describe(__filename, () => { pageYOffset: 20, }; - const bounds = getThumbBoundsFn(0, fakeDocument, fakeWindow); + const bounds = getThumbBoundsFn(0, { + _document: fakeDocument, + _window: fakeWindow, + }); expect(bounds).toEqual({ w: width, @@ -181,7 +198,7 @@ describe(__filename, () => { scrollTop, }; - const bounds = getThumbBoundsFn(0, fakeDocument); + const bounds = getThumbBoundsFn(0, { _document: fakeDocument }); expect(bounds).toEqual({ w: width,