From 217fbce3cef36f77acdc62882f93fe4ddd6d4691 Mon Sep 17 00:00:00 2001 From: Jeremy Press Date: Fri, 26 Jan 2018 18:21:51 -0800 Subject: [PATCH] Fix: Prevent navigation on empty collections (#607) --- README.md | 6 ++++++ src/lib/Preview.js | 10 +++++++--- src/lib/__tests__/Preview-test.js | 18 ++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fdce0eb81..b94bd82d9 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,12 @@ Additional Methods `preview.getCurrentCollection()` returns the current collection if any. +`preview.navigateLeft()` shows previous file in the collection. + +`preview.navigateRight()` shows next file in the collection. + +`preview.navigateToIndex(index)` shows the specified index in the current collection. + `preview.getCurrentFile()` returns the current file being previewed if any. The file object structure is the same as returned by the [Box API](https://developer.box.com/reference#files). `preview.getCurrentViewer()` returns the current viewer instance. May be undefined if the viewer isn't ready yet and waiting on conversion to happen. diff --git a/src/lib/Preview.js b/src/lib/Preview.js index 6abba8b40..acf6190a5 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -1356,11 +1356,15 @@ class Preview extends EventEmitter { /** * Shows a preview of a file at the specified index in the current collection. * - * @private + * @public * @param {number} index - Index of file to preview * @return {void} */ navigateToIndex(index) { + if (!Array.isArray(this.collection) || this.collection.length < 2) { + return; + } + const fileId = this.collection[index]; this.emit('navigate', fileId); this.count.navigation += 1; @@ -1370,7 +1374,7 @@ class Preview extends EventEmitter { /** * Shows a preview of the previous file. * - * @private + * @public * @return {void} */ navigateLeft() { @@ -1384,7 +1388,7 @@ class Preview extends EventEmitter { /** * Shows a preview of the next file. * - * @private + * @public * @return {void} */ navigateRight() { diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index c5ecbcae0..0fd614af5 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -2076,9 +2076,19 @@ describe('lib/Preview', () => { navigation: 0 }; - preview.collection = { - 2: 'file' - }; + preview.collection = [ + 'file', 'file2', 'file3' + ] + }); + + it('should do nothing if the collection is invalid', () => { + preview.collection = 'foo' + preview.navigateToIndex(1); + expect(stubs.emit).to.not.be.called; + + preview.collection = [] + preview.navigateToIndex(1); + expect(stubs.emit).to.not.be.called; }); it('should emit the navigation event', () => { @@ -2093,7 +2103,7 @@ describe('lib/Preview', () => { it('should load the requested file', () => { preview.navigateToIndex(2); - expect(stubs.load).to.be.calledWith('file'); + expect(stubs.load).to.be.calledWith('file3'); }); });