Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(feature-toggles): Add feature object to standardize feature toggles #1477

Merged
merged 4 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,11 @@ class Preview extends EventEmitter {
// 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
this.options.features = options.features || {};

// Disable or enable viewers based on viewer options
Object.keys(this.options.viewers).forEach(viewerName => {
const isDisabled = this.options.viewers[viewerName].disabled;
Expand Down
19 changes: 19 additions & 0 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ let preview;
let containerEl;

describe('lib/Preview', () => {
const features = { shouldUseFeature: { enabled: true } };

beforeEach(() => {
fixture.load('__tests__/Preview-test.html');
containerEl = document.querySelector('.container');
Expand Down Expand Up @@ -1242,6 +1244,7 @@ describe('lib/Preview', () => {
sharedLinkPassword: stubs.sharedLinkPassword,
apiHost: stubs.apiHost,
appHost: stubs.appHost,
features,
header: stubs.header,
logoUrl: stubs.logoUrl,
showDownload: true,
Expand Down Expand Up @@ -1387,6 +1390,22 @@ describe('lib/Preview', () => {

expect(preview.options.responseInterceptor).toBe(responseInterceptor);
});

test('should set features to the Preview options field', () => {
expect(preview.options.features).toBeUndefined();

preview.parseOptions(preview.previewOptions);

expect(preview.options.features).toEqual(features);
});

test('should default Preview options features to {} when theres no features passed in', () => {
expect(preview.options.features).toBeUndefined();

preview.parseOptions({ ...preview.previewOptions, features: undefined });

expect(preview.options.features).toEqual({});
});
});

describe('createViewerOptions()', () => {
Expand Down
33 changes: 33 additions & 0 deletions src/lib/__tests__/featureChecking-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getFeatureConfig, isFeatureEnabled } from '../featureChecking';

const features = {
shouldUseCar: {
enabled: true,
},
shouldUsePlane: {
enabled: false,
},
};

describe('featureChecking', () => {
describe('isFeatureEnabled', () => {
test('returns truthy when object is enabled and falsy when object is disabled', () => {
expect(isFeatureEnabled(features, 'shouldUseCar.enabled')).toBe(true);
expect(isFeatureEnabled(features, 'shouldUsePlane.enabled')).toBe(false);
});
test('defaults to false if object does not exist', () => {
expect(isFeatureEnabled(features, 'unknownKey')).toBe(false);
});
});

describe('getFeatureConfig', () => {
test('returns feature object if key exists', () => {
expect(getFeatureConfig(features, 'shouldUseCar')).toEqual({ enabled: true });
expect(getFeatureConfig(features, 'shouldUsePlane')).toEqual({ enabled: false });
});

test('defaults to empty object', () => {
expect(getFeatureConfig({}, 'unknownKey')).toEqual({});
});
});
});
17 changes: 17 additions & 0 deletions src/lib/featureChecking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import get from 'lodash/get';

type FeatureOptions = {
[key: string]: {};
};

type FeatureConfig = {
[key: string]: FeatureOptions;
};

export const isFeatureEnabled = (features: FeatureConfig, featureName: string): boolean => {
return !!get(features, featureName, false);
};

export const getFeatureConfig = (features: FeatureConfig, featureName: string): FeatureConfig => {
return get(features, featureName, {});
};