Skip to content

Commit

Permalink
feat(aci): use feature check for aci (#1508)
Browse files Browse the repository at this point in the history
* feat(aci): use feature check for aci

* feat(aci): resolve comments

* feat(aci): change updateContentInsightsOptions function argument

* feat(aci): address comments
  • Loading branch information
arturfrombox authored Oct 4, 2023
1 parent 11cfaa7 commit 4616da7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
24 changes: 15 additions & 9 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
VIEWER_EVENT,
} from './events';
import { getClientLogDetails, getISOTime } from './logUtils';
import { isFeatureEnabled } from './featureChecking';
import './Preview.scss';

const DEFAULT_DISABLED_VIEWERS = ['Office']; // viewers disabled by default
Expand Down Expand Up @@ -1001,9 +1002,6 @@ class Preview extends EventEmitter {
// Add the response interceptor to the preview instance
this.options.responseInterceptor = options.responseInterceptor;

// Advanced Content Insights
this.options.advancedContentInsights = options.advancedContentInsights;

// Features
// This makes features available everywhere that features is passed in, which includes
// all of the viewers for different files
Expand Down Expand Up @@ -1439,15 +1437,19 @@ class Preview extends EventEmitter {
this.api
.post(`${apiHost}/2.0/events`, data, { headers })
.then(() => {
this.pageTrackerReporter(true);
if (isFeatureEnabled(this.options.features, 'advancedContentInsights.enabled')) {
this.pageTrackerReporter(true);
}
// Reset retry count after successfully logging
this.logRetryCount = 0;
})
.catch(() => {
// Don't retry more than the retry limit
this.logRetryCount += 1;
if (this.logRetryCount > LOG_RETRY_COUNT) {
this.pageTrackerReporter(false);
if (isFeatureEnabled(this.options.features, 'advancedContentInsights.enabled')) {
this.pageTrackerReporter(false);
}
this.logRetryCount = 0;
clearTimeout(this.logRetryTimeout);
return;
Expand Down Expand Up @@ -1841,13 +1843,17 @@ class Preview extends EventEmitter {
* Updates advanced content insights options after props have changed in parent app
*
* @public
* @param {Object} options - new content insights options value
* @param {Object} newOptions - new content insights options value
* @return {void}
*/
updateContentInsightsOptions(options) {
updateContentInsightsOptions(newOptions) {
this.options.features = { ...this.options.features, advancedContentInsights: newOptions };
if (this.viewer && this.viewer.pageTracker) {
this.previewOptions = { ...this.previewOptions, contentInsights: options };
this.viewer.pageTracker.updateOptions(this.previewOptions.contentInsights);
this.previewOptions = {
...this.previewOptions,
features: { ...this.previewOptions.features, advancedContentInsights: newOptions },
};
this.viewer.pageTracker.updateOptions(newOptions);
}
}

Expand Down
32 changes: 24 additions & 8 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import loaders from '../loaders';
import { API_HOST, CLASS_NAVIGATION_VISIBILITY, ENCODING_TYPES } from '../constants';
import { VIEWER_EVENT, ERROR_CODE, LOAD_METRIC, PREVIEW_METRIC } from '../events';
import PageTracker from '../PageTracker';
import { isFeatureEnabled } from '../featureChecking';

jest.mock('../Logger');
jest.mock('../util', () => ({
Expand All @@ -25,6 +26,7 @@ jest.mock('../util', () => ({
}),
getHeaders: jest.fn(),
}));
jest.mock('../featureChecking');

const tokens = require('../tokens');

Expand Down Expand Up @@ -2109,18 +2111,34 @@ describe('lib/Preview', () => {
});
});

test('should fire the preview_event_report success event on a successful access stats post', () => {
test('should fire the preview_event_report success event on a successful access stats post if aci enabled', () => {
preview.viewer = {
emit: jest.fn(),
};
isFeatureEnabled.mockReturnValueOnce(true);
stubs.emit = jest.spyOn(preview.viewer, 'emit');
jest.spyOn(Api.prototype, 'post').mockReturnValue(stubs.promiseResolve);
preview.logPreviewEvent(0, {});

return stubs.promiseResolve.then(() => {
expect(stubs.emit).toBeCalledWith('preview_event_report', true);
});
});

test('should not fire the preview_event_report success event on a successful access stats post if aci disabled', () => {
preview.viewer = {
emit: jest.fn(),
};
isFeatureEnabled.mockReturnValueOnce(false);
stubs.emit = jest.spyOn(preview.viewer, 'emit');
jest.spyOn(Api.prototype, 'post').mockReturnValue(stubs.promiseResolve);
preview.logPreviewEvent(0, {});

return stubs.promiseResolve.then(() => {
expect(stubs.emit).not.toBeCalled();
});
});

test('should fire the preview_event_report failed event if the post fails and retry limit has been reached', () => {
preview.viewer = {
emit: jest.fn(),
Expand Down Expand Up @@ -2961,12 +2979,8 @@ describe('lib/Preview', () => {

describe('updateContentInsightsOptions()', () => {
test('should update the content insights options', () => {
preview.options = {
enableAdvancedContentInsights: true,
};

preview.previewOptions = {
contentInsights: { isActive: false },
preview.previewOptions.features = {
advancedContentInsights: { isActive: false },
};

preview.viewer = {
Expand All @@ -2977,7 +2991,9 @@ describe('lib/Preview', () => {
const options = { isActive: true };

preview.updateContentInsightsOptions(options);
expect(preview.previewOptions.contentInsights).toBe(options);

expect(preview.options.features.advancedContentInsights).toBe(options);
expect(preview.previewOptions.features.advancedContentInsights).toBe(options);
expect(stubs.updateOptions).toBeCalledWith(options);
});
});
Expand Down
8 changes: 6 additions & 2 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
VIEWER_EVENT,
} from '../../events';
import Timer from '../../Timer';
import { getFeatureConfig, isFeatureEnabled } from '../../featureChecking';

export const DISCOVERABILITY_STATES = [
AnnotationState.HIGHLIGHT_TEMP,
Expand Down Expand Up @@ -192,8 +193,11 @@ class DocBaseViewer extends BaseViewer {
}

this.updateDiscoverabilityResinTag();
if (this.options.advancedContentInsights) {
this.pageTracker = new PageTracker(this.options.advancedContentInsights, this.options.file);

const advancedContentInsightsConfig = getFeatureConfig(this.options.features, 'advancedContentInsights');

if (isFeatureEnabled(this.options.features, 'advancedContentInsights.enabled')) {
this.pageTracker = new PageTracker(advancedContentInsightsConfig, this.options.file);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
id: '0',
extension: 'ppt',
},
advancedContentInsights: {},
features: { advancedContentInsights: { enabled: true } },
});
docBase.containerEl = containerEl;
docBase.rootEl = rootEl;
Expand Down

0 comments on commit 4616da7

Please sign in to comment.