Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Jun 21, 2018
1 parent aafb848 commit ef462ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/amo/components/ScreenShots.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,11 +30,15 @@ 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, {
// $FLOW_FIXME: see https://github.com/facebook/flow/issues/183
_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) {
Expand Down
25 changes: 21 additions & 4 deletions tests/unit/amo/components/TestScreenShots.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,30 @@ 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;
const width = 100;

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,
Expand All @@ -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,
Expand All @@ -181,7 +198,7 @@ describe(__filename, () => {
scrollTop,
};

const bounds = getThumbBoundsFn(0, fakeDocument);
const bounds = getThumbBoundsFn(0, { _document: fakeDocument });

expect(bounds).toEqual({
w: width,
Expand Down

0 comments on commit ef462ae

Please sign in to comment.