Skip to content

Commit 68e67ed

Browse files
author
Jose Gaston
committed
feat(auth): accept optional features in prefetch()
prefetch() relied on this.options.features which is only set by show(). Callers that prefetch before show() (e.g. folder hover prefetch) had no way to pass features into the viewer, so featureEnabled() checks in viewer prefetch() methods always read defaults. Add an optional features param to prefetch() and merge it into per-call viewer options without mutating this.options.
1 parent a226e8b commit 68e67ed

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/lib/Preview.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ class Preview extends EventEmitter {
693693
* @param {string} options.sharedLink - Shared link
694694
* @param {string} options.sharedLinkPassword - Shared link password
695695
* @param {boolean} options.preload - Is this prefetch for a preload
696+
* @param {Object} [options.features] - Feature flags to merge into viewer options for this prefetch
696697
* @param {string} token - Access token
697698
* @return {void}
698699
*/
@@ -705,6 +706,7 @@ class Preview extends EventEmitter {
705706
preload = false,
706707
isDocFirstPrefetchEnabled = false,
707708
docFirstPagesConfig = null,
709+
features,
708710
}) {
709711
let file;
710712
let loader;
@@ -747,6 +749,13 @@ class Preview extends EventEmitter {
747749
options.docFirstPagesConfig = docFirstPagesConfig;
748750
}
749751

752+
// If features are passed in for this prefetch call (e.g. from callers that
753+
// invoke prefetch() before show() has set this.options.features), merge them
754+
// into the viewer options for this call without mutating this.options.
755+
if (features) {
756+
options.features = { ...this.options.features, ...features };
757+
}
758+
750759
const viewerInstance = new viewer.CONSTRUCTOR(this.createViewerOptions(options));
751760
if (typeof viewerInstance.prefetch === 'function') {
752761
viewerInstance.prefetch({

src/lib/__tests__/Preview-test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,51 @@ describe('lib/Preview', () => {
798798

799799
preview.prefetch({ fileId, token, sharedLink, sharedLinkPassword, preload: true });
800800
});
801+
802+
test('should merge per-call features into viewer options when features are passed', () => {
803+
jest.spyOn(loader, 'determineViewer').mockReturnValue(viewer);
804+
jest.spyOn(preview, 'createViewerOptions');
805+
preview.options.features = { existingFeature: true };
806+
807+
preview.prefetch({
808+
fileId,
809+
token,
810+
sharedLink,
811+
sharedLinkPassword,
812+
features: { migrateAccessTokenToHeader: true },
813+
});
814+
815+
expect(preview.createViewerOptions).toHaveBeenCalledWith(
816+
expect.objectContaining({
817+
features: { existingFeature: true, migrateAccessTokenToHeader: true },
818+
}),
819+
);
820+
});
821+
822+
test('should not set features on viewer options when features are omitted', () => {
823+
jest.spyOn(loader, 'determineViewer').mockReturnValue(viewer);
824+
jest.spyOn(preview, 'createViewerOptions');
825+
826+
preview.prefetch({ fileId, token, sharedLink, sharedLinkPassword });
827+
828+
const callArgs = preview.createViewerOptions.mock.calls[0][0];
829+
expect(callArgs).not.toHaveProperty('features');
830+
});
831+
832+
test('should not mutate this.options.features when per-call features are passed', () => {
833+
jest.spyOn(loader, 'determineViewer').mockReturnValue(viewer);
834+
preview.options.features = { existingFeature: true };
835+
836+
preview.prefetch({
837+
fileId,
838+
token,
839+
sharedLink,
840+
sharedLinkPassword,
841+
features: { migrateAccessTokenToHeader: true },
842+
});
843+
844+
expect(preview.options.features).toEqual({ existingFeature: true });
845+
});
801846
});
802847

803848
describe('prefetchViewers()', () => {

0 commit comments

Comments
 (0)