Skip to content

Commit

Permalink
feat(loading): Move loading ui setup back to viewers
Browse files Browse the repository at this point in the history
  • Loading branch information
jstoffan committed Mar 29, 2021
1 parent 9fd8568 commit c086e78
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
4 changes: 0 additions & 4 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,10 +1175,6 @@ class Preview extends EventEmitter {
this.ui.showLoadingDownloadButton(this.download);
}

// Update the loading indicators
this.ui.hideCrawler();
this.ui.setLoadingIcon(this.file.extension);

// Determine the asset loader to use
const loader = this.getLoader(this.file);

Expand Down
17 changes: 2 additions & 15 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1666,8 +1666,6 @@ describe('lib/Preview', () => {
stubs.destroy = jest.spyOn(preview, 'destroy');
stubs.checkPermission = jest.spyOn(file, 'checkPermission').mockReturnValue(true);
stubs.canDownload = jest.spyOn(file, 'canDownload').mockReturnValue(false);
stubs.hideCrawler = jest.spyOn(preview.ui, 'hideCrawler').mockImplementation();
stubs.setLoadingIcon = jest.spyOn(preview.ui, 'setLoadingIcon').mockImplementation();
stubs.showLoadingDownloadButton = jest.spyOn(preview.ui, 'showLoadingDownloadButton').mockImplementation();
stubs.loadPromiseResolve = Promise.resolve();
stubs.determineRepresentationStatusPromise = Promise.resolve();
Expand Down Expand Up @@ -1715,27 +1713,16 @@ describe('lib/Preview', () => {

test('should show the loading download button if file can be downloaded', () => {
stubs.canDownload.mockReturnValue(true);
preview.loadViewer();
preview.loadViewer({});
expect(stubs.showLoadingDownloadButton).toBeCalled();
});

test("should not show the loading download button if file can't be downloaded", () => {
stubs.canDownload.mockReturnValue(false);
preview.loadViewer();
preview.loadViewer({});
expect(stubs.showLoadingDownloadButton).not.toBeCalled();
});

test('should show the file-specific loading icon', () => {
preview.file.extension = 'pdf';
preview.loadViewer();
expect(stubs.setLoadingIcon).toBeCalledWith('pdf');
});

test('should hide the loading crawler', () => {
preview.loadViewer();
expect(stubs.hideCrawler).toBeCalled();
});

test('should throw an unsupported error if there is no loader for general file types', () => {
stubs.getLoader.mockReturnValue(undefined);

Expand Down
17 changes: 17 additions & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class BaseViewer extends EventEmitter {
// From the perspective of viewers bp-content holds everything
this.containerEl = container.querySelector(SELECTOR_BOX_PREVIEW_CONTENT);

// Update the loading indicators
this.setupLoading();

// Attach event listeners
this.addCommonListeners();

Expand All @@ -220,6 +223,20 @@ class BaseViewer extends EventEmitter {
this.isSetup = true;
}

/**
* Removes the crawler and sets the file type specific loading icon
*
* @return {void}
*/
setupLoading() {
const { file = {} } = this.options;

if (this.previewUI) {
this.previewUI.hideCrawler();
this.previewUI.setLoadingIcon(file.extension);
}
}

/**
* Destroys the viewer
*
Expand Down
15 changes: 14 additions & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ describe('lib/viewers/BaseViewer', () => {
},
});
base.previewUI = {
replaceHeader: jest.fn(),
hideCrawler: jest.fn(),
notification: {
show: jest.fn(),
hide: jest.fn(),
},
replaceHeader: jest.fn(),
setLoadingIcon: jest.fn(),
};
});

Expand Down Expand Up @@ -116,6 +118,17 @@ describe('lib/viewers/BaseViewer', () => {
});
});

describe('setupLoading()', () => {
test('should hide the crawler and set the file-specific loading icon', () => {
base.options.file = { extension: 'pdf' };

base.setupLoading();

expect(base.previewUI.hideCrawler).toBeCalled();
expect(base.previewUI.setLoadingIcon).toBeCalledWith('pdf');
});
});

describe('getResizeHandler()', () => {
test('should return a resize handler', () => {
expect(typeof base.getResizeHandler()).toBe('function');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/error/PreviewErrorViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PreviewErrorViewer extends BaseViewer {
* @override
* @return {void}
*/
finishLoadingSetup() {
setupLoading() {
/* no op, custom loading logic for errors */
}

Expand Down

0 comments on commit c086e78

Please sign in to comment.