Skip to content

Commit

Permalink
Fix: Prevent navigation on empty collections (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Press authored and tonyjin committed Jan 27, 2018
1 parent e8992c7 commit 217fbce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1370,7 +1374,7 @@ class Preview extends EventEmitter {
/**
* Shows a preview of the previous file.
*
* @private
* @public
* @return {void}
*/
navigateLeft() {
Expand All @@ -1384,7 +1388,7 @@ class Preview extends EventEmitter {
/**
* Shows a preview of the next file.
*
* @private
* @public
* @return {void}
*/
navigateRight() {
Expand Down
18 changes: 14 additions & 4 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});

Expand Down

0 comments on commit 217fbce

Please sign in to comment.