mirror of
https://github.com/apache/superset.git
synced 2024-09-06 13:57:40 -04:00
refactor: Feature flags getter (#22015)
This commit is contained in:
parent
97e3e790a3
commit
d52d72ce64
@ -87,14 +87,11 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isFeatureEnabled(feature: FeatureFlag) {
|
export function isFeatureEnabled(feature: FeatureFlag): boolean {
|
||||||
try {
|
try {
|
||||||
return !!window.featureFlags[feature];
|
return !!window.featureFlags[feature];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line no-console
|
console.error(`Failed to query feature flag ${feature}`);
|
||||||
console.error(`Failed to query feature flag ${feature} (see error below)`);
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error(error);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -16,48 +16,41 @@
|
|||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
import mockConsole from 'jest-mock-console';
|
||||||
import { FeatureFlag, isFeatureEnabled } from '@superset-ui/core';
|
import { isFeatureEnabled, FeatureFlag } from '@superset-ui/core';
|
||||||
|
|
||||||
const originalFeatureFlags = window.featureFlags;
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
const originalConsoleError = console.error;
|
|
||||||
const reset = () => {
|
|
||||||
window.featureFlags = originalFeatureFlags;
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error = originalConsoleError;
|
|
||||||
};
|
|
||||||
|
|
||||||
it('returns false and raises console error if feature flags have not been initialized', () => {
|
it('returns false and raises console error if feature flags have not been initialized', () => {
|
||||||
// eslint-disable-next-line no-console
|
mockConsole();
|
||||||
console.error = jest.fn();
|
Object.defineProperty(window, 'featureFlags', {
|
||||||
delete (window as any).featureFlags;
|
value: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
expect(isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING)).toEqual(
|
expect(isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING)).toEqual(
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
expect(console.error).toHaveBeenCalled();
|
||||||
// eslint-disable-next-line no-console
|
// @ts-expect-error
|
||||||
expect(console.error).toHaveBeenNthCalledWith(
|
expect(console.error.mock.calls[0][0]).toEqual(
|
||||||
1,
|
'Failed to query feature flag ALLOW_DASHBOARD_DOMAIN_SHARDING',
|
||||||
'Failed to query feature flag ALLOW_DASHBOARD_DOMAIN_SHARDING (see error below)',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
reset();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns false for unset feature flag', () => {
|
it('returns false for unset feature flag', () => {
|
||||||
|
Object.defineProperty(window, 'featureFlags', {
|
||||||
|
value: {},
|
||||||
|
});
|
||||||
|
|
||||||
expect(isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING)).toEqual(
|
expect(isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING)).toEqual(
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
reset();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns true for set feature flag', () => {
|
it('returns true for set feature flag', () => {
|
||||||
window.featureFlags = {
|
Object.defineProperty(window, 'featureFlags', {
|
||||||
[FeatureFlag.CLIENT_CACHE]: true,
|
value: {
|
||||||
};
|
CLIENT_CACHE: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
expect(isFeatureEnabled(FeatureFlag.CLIENT_CACHE)).toEqual(true);
|
expect(isFeatureEnabled(FeatureFlag.CLIENT_CACHE)).toEqual(true);
|
||||||
reset();
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user