Skip to content

Commit 1b368ad

Browse files
committed
fix(preview): add preview sdk load check
1 parent 5653500 commit 1b368ad

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/elements/content-preview/ContentPreview.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ class ContentPreview extends React.PureComponent<Props, State> {
226226

227227
stagedFile: ?BoxItem;
228228

229+
previewLibraryLoaded: boolean = false;
230+
229231
updateVersionToCurrent: ?() => void;
230232

231233
initialState: State = {
@@ -448,6 +450,13 @@ class ContentPreview extends React.PureComponent<Props, State> {
448450
const fileVersionId = getProp(file, 'file_version.id');
449451
let loadPreview = false;
450452

453+
// Check if preview library just became available and we haven't loaded preview yet
454+
// This handles cases where library loads asynchronously after file is already set
455+
if (!this.previewLibraryLoaded && this.isPreviewLibraryLoaded() && file && !this.preview) {
456+
this.previewLibraryLoaded = true;
457+
return true;
458+
}
459+
451460
if (selectedVersionId !== prevSelectedVersionId) {
452461
const isPreviousCurrent = fileVersionId === prevSelectedVersionId || !prevSelectedVersionId;
453462
const isSelectedCurrent = fileVersionId === selectedVersionId || !selectedVersionId;
@@ -800,8 +809,9 @@ class ContentPreview extends React.PureComponent<Props, State> {
800809
...rest
801810
}: Props = this.props;
802811
const { file, selectedVersion, startAt }: State = this.state;
812+
this.previewLibraryLoaded = this.isPreviewLibraryLoaded();
803813

804-
if (!this.isPreviewLibraryLoaded() || !file || !tokenOrTokenFunction) {
814+
if (!this.previewLibraryLoaded || !file || !tokenOrTokenFunction) {
805815
return;
806816
}
807817

src/elements/content-preview/__tests__/ContentPreview.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ describe('elements/content-preview/ContentPreview', () => {
175175

176176
expect(instance.shouldLoadPreview({ selectedVersion: { id: '1' } })).toBe(false);
177177
});
178+
179+
test("should return true if the preview library just became available and we haven't loaded preview yet", () => {
180+
instance.previewLibraryLoaded = false;
181+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(true);
182+
instance.preview = undefined;
183+
expect(instance.shouldLoadPreview({ file })).toBe(true);
184+
expect(instance.previewLibraryLoaded).toBe(true);
185+
});
178186
});
179187

180188
describe('canDownload()', () => {
@@ -494,6 +502,35 @@ describe('elements/content-preview/ContentPreview', () => {
494502
}
495503
},
496504
);
505+
506+
test('should return if the preview library is not loaded', async () => {
507+
const wrapper = getWrapper(props);
508+
wrapper.setState({ file });
509+
const instance = wrapper.instance();
510+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(false);
511+
const spy = jest.spyOn(instance, 'getFileId');
512+
await instance.loadPreview();
513+
expect(spy).not.toHaveBeenCalled();
514+
});
515+
516+
test('should return false if the file is not set', async () => {
517+
const wrapper = getWrapper(props);
518+
const instance = wrapper.instance();
519+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(true);
520+
const spy = jest.spyOn(instance, 'getFileId');
521+
await instance.loadPreview();
522+
expect(spy).not.toHaveBeenCalled();
523+
});
524+
525+
test('should return if the token is not set', async () => {
526+
const wrapper = getWrapper({ ...props, token: undefined });
527+
wrapper.setState({ file });
528+
const instance = wrapper.instance();
529+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(true);
530+
const spy = jest.spyOn(instance, 'getFileId');
531+
await instance.loadPreview();
532+
expect(spy).not.toHaveBeenCalled();
533+
});
497534
});
498535

499536
describe('fetchFile()', () => {

0 commit comments

Comments
 (0)