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

chore: Removes duplicated featureFlags.ts #24935

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import logger from './logging';

// We can codegen the enum definition based on a list of supported flags that we
// check into source control. We're hardcoding the supported flags for now.
export enum FeatureFlag {
Expand Down Expand Up @@ -85,11 +87,17 @@ declare global {
}
}

export function initFeatureFlags(featureFlags?: FeatureFlagMap) {
if (!window.featureFlags) {
window.featureFlags = featureFlags || {};
}
}

export function isFeatureEnabled(feature: FeatureFlag): boolean {
try {
return !!window.featureFlags[feature];
} catch (error) {
console.error(`Failed to query feature flag ${feature}`);
logger.error(`Failed to query feature flag ${feature}`);
}
return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,52 @@
* specific language governing permissions and limitations
* under the License.
*/
import mockConsole from 'jest-mock-console';
import { isFeatureEnabled, FeatureFlag } from '@superset-ui/core';
import * as uiCore from '@superset-ui/core';

it('returns false and raises console error if feature flags have not been initialized', () => {
mockConsole();
it('initializes feature flags', () => {
Object.defineProperty(window, 'featureFlags', {
value: undefined,
});
uiCore.initFeatureFlags();
expect(window.featureFlags).toEqual({});
});

expect(isFeatureEnabled(FeatureFlag.DRILL_BY)).toEqual(false);
expect(console.error).toHaveBeenCalled();
// @ts-expect-error
expect(console.error.mock.calls[0][0]).toEqual(
'Failed to query feature flag DRILL_BY',
);
it('initializes feature flags with predefined values', () => {
Object.defineProperty(window, 'featureFlags', {
value: undefined,
});
const featureFlags = {
CLIENT_CACHE: true,
DRILL_BY: false,
};
uiCore.initFeatureFlags(featureFlags);
expect(window.featureFlags).toEqual(featureFlags);
});

it('does nothing if feature flags are already initialized', () => {
const featureFlags = { DRILL_BY: false };
Object.defineProperty(window, 'featureFlags', {
value: featureFlags,
});
uiCore.initFeatureFlags({ DRILL_BY: true });
expect(window.featureFlags).toEqual(featureFlags);
});

it('returns false and raises console error if feature flags have not been initialized', () => {
const logging = jest.spyOn(uiCore.logging, 'error');
Object.defineProperty(window, 'featureFlags', {
value: undefined,
});
expect(uiCore.isFeatureEnabled(uiCore.FeatureFlag.DRILL_BY)).toEqual(false);
expect(uiCore.logging.error).toHaveBeenCalled();
expect(logging).toHaveBeenCalledWith('Failed to query feature flag DRILL_BY');
});

it('returns false for unset feature flag', () => {
Object.defineProperty(window, 'featureFlags', {
value: {},
});

expect(isFeatureEnabled(FeatureFlag.DRILL_BY)).toEqual(false);
expect(uiCore.isFeatureEnabled(uiCore.FeatureFlag.DRILL_BY)).toEqual(false);
});

it('returns true for set feature flag', () => {
Expand All @@ -47,6 +70,7 @@ it('returns true for set feature flag', () => {
CLIENT_CACHE: true,
},
});

expect(isFeatureEnabled(FeatureFlag.CLIENT_CACHE)).toEqual(true);
expect(uiCore.isFeatureEnabled(uiCore.FeatureFlag.CLIENT_CACHE)).toEqual(
true,
);
});
8 changes: 6 additions & 2 deletions superset-frontend/src/SqlLab/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import React from 'react';
import persistState from 'redux-localstorage';
import { Provider } from 'react-redux';
import { hot } from 'react-hot-loader/root';
import { FeatureFlag, ThemeProvider } from '@superset-ui/core';
import {
FeatureFlag,
ThemeProvider,
initFeatureFlags,
isFeatureEnabled,
} from '@superset-ui/core';
import { GlobalStyles } from 'src/GlobalStyles';
import { initFeatureFlags, isFeatureEnabled } from 'src/featureFlags';
import { setupStore } from 'src/views/store';
import setupExtensions from 'src/setup/setupExtensions';
import getBootstrapData from 'src/utils/getBootstrapData';
Expand Down
8 changes: 6 additions & 2 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
*/
import shortid from 'shortid';
import rison from 'rison';
import { FeatureFlag, SupersetClient, t } from '@superset-ui/core';
import {
FeatureFlag,
SupersetClient,
t,
isFeatureEnabled,
} from '@superset-ui/core';
import invert from 'lodash/invert';
import mapKeys from 'lodash/mapKeys';
import { isFeatureEnabled } from 'src/featureFlags';

import { now } from 'src/utils/dates';
import {
Expand Down
6 changes: 3 additions & 3 deletions superset-frontend/src/SqlLab/actions/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import fetchMock from 'fetch-mock';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import shortid from 'shortid';
import * as featureFlags from 'src/featureFlags';
import * as uiCore from '@superset-ui/core';
import * as actions from 'src/SqlLab/actions/sqlLab';
import { LOG_EVENT } from 'src/logger/actions';
import {
Expand Down Expand Up @@ -492,7 +492,7 @@ describe('async actions', () => {

beforeEach(() => {
isFeatureEnabledMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(
feature => feature === 'SQLLAB_BACKEND_PERSISTENCE',
);
Expand Down Expand Up @@ -758,7 +758,7 @@ describe('async actions', () => {
describe('with backend persistence flag off', () => {
it('does not update the tab state in the backend', () => {
const backendPersistenceOffMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(
feature => !(feature === 'SQLLAB_BACKEND_PERSISTENCE'),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import React from 'react';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import * as featureFlags from 'src/featureFlags';
import * as uiCore from '@superset-ui/core';
import { Provider } from 'react-redux';
import { supersetTheme, ThemeProvider } from '@superset-ui/core';
import { render, screen, act } from '@testing-library/react';
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('ShareSqlLabQuery', () => {
describe('via /kv/store', () => {
beforeAll(() => {
isFeatureEnabledMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(() => true);
});

Expand Down Expand Up @@ -150,7 +150,7 @@ describe('ShareSqlLabQuery', () => {
describe('via saved query', () => {
beforeAll(() => {
isFeatureEnabledMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(() => false);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
* under the License.
*/
import React from 'react';
import { FeatureFlag, styled, t, useTheme } from '@superset-ui/core';
import {
FeatureFlag,
styled,
t,
useTheme,
isFeatureEnabled,
} from '@superset-ui/core';
import Button from 'src/components/Button';
import Icons from 'src/components/Icons';
import withToasts from 'src/components/MessageToasts/withToasts';
import CopyToClipboard from 'src/components/CopyToClipboard';
import { storeQuery } from 'src/utils/common';
import { getClientErrorObject } from 'src/utils/getClientErrorObject';
import { isFeatureEnabled } from 'src/featureFlags';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';

interface ShareSqlLabQueryProps {
Expand Down
3 changes: 1 addition & 2 deletions superset-frontend/src/SqlLab/components/SouthPane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import shortid from 'shortid';
import Alert from 'src/components/Alert';
import Tabs from 'src/components/Tabs';
import { EmptyStateMedium } from 'src/components/EmptyState';
import { FeatureFlag, styled, t } from '@superset-ui/core';
import { FeatureFlag, styled, t, isFeatureEnabled } from '@superset-ui/core';

import { setActiveSouthPaneTab } from 'src/SqlLab/actions/sqlLab';
import { isFeatureEnabled } from 'src/featureFlags';

import Label from 'src/components/Label';
import { SqlLabRootState } from 'src/SqlLab/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Split from 'react-split';
import {
css,
FeatureFlag,
isFeatureEnabled,
styled,
t,
useTheme,
Expand Down Expand Up @@ -84,7 +85,6 @@ import {
LocalStorageKeys,
setItem,
} from 'src/utils/localStorageHelpers';
import { isFeatureEnabled } from 'src/featureFlags';
import { EmptyStateBig } from 'src/components/EmptyState';
import getBootstrapData from 'src/utils/getBootstrapData';
import { isEmpty } from 'lodash';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import { EditableTabs } from 'src/components/Tabs';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import URI from 'urijs';
import { FeatureFlag, styled, t } from '@superset-ui/core';
import { isFeatureEnabled } from 'src/featureFlags';
import { FeatureFlag, styled, t, isFeatureEnabled } from '@superset-ui/core';
import { Tooltip } from 'src/components/Tooltip';
import { detectOS } from 'src/utils/common';
import * as Actions from 'src/SqlLab/actions/sqlLab';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import React from 'react';
import fetchMock from 'fetch-mock';
import * as featureFlags from 'src/featureFlags';
import * as uiCore from '@superset-ui/core';
import { FeatureFlag } from '@superset-ui/core';
import TableElement, { Column } from 'src/SqlLab/components/TableElement';
import { table, initialState } from 'src/SqlLab/fixtures';
Expand Down Expand Up @@ -140,7 +140,7 @@ test('removes the table', async () => {
const updateTableSchemaEndpoint = 'glob:*/tableschemaview/*';
fetchMock.delete(updateTableSchemaEndpoint, {});
const isFeatureEnabledMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(
featureFlag => featureFlag === FeatureFlag.SQLLAB_BACKEND_PERSISTENCE,
);
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/components/Chart/Chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import React from 'react';
import {
ensureIsArray,
FeatureFlag,
isFeatureEnabled,
logging,
styled,
t,
} from '@superset-ui/core';
import { isFeatureEnabled } from 'src/featureFlags';
import { PLACEHOLDER_DATASOURCE } from 'src/dashboard/constants';
import Loading from 'src/components/Loading';
import { EmptyStateBig } from 'src/components/EmptyState';
Expand Down
9 changes: 7 additions & 2 deletions superset-frontend/src/components/Chart/chartAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
/* eslint no-undef: 'error' */
/* eslint no-param-reassign: ["error", { "props": false }] */
import moment from 'moment';
import { FeatureFlag, isDefined, SupersetClient, t } from '@superset-ui/core';
import {
FeatureFlag,
isDefined,
SupersetClient,
t,
isFeatureEnabled,
} from '@superset-ui/core';
import { getControlsState } from 'src/explore/store';
import { isFeatureEnabled } from 'src/featureFlags';
import {
getAnnotationJsonUrl,
getExploreUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Badge from 'src/components/Badge';
import shortid from 'shortid';
import {
css,
isFeatureEnabled,
getCurrencySymbol,
ensureIsArray,
FeatureFlag,
Expand All @@ -51,7 +52,6 @@ import TextControl from 'src/explore/components/controls/TextControl';
import TextAreaControl from 'src/explore/components/controls/TextAreaControl';
import SpatialControl from 'src/explore/components/controls/SpatialControl';
import withToasts from 'src/components/MessageToasts/withToasts';
import { isFeatureEnabled } from 'src/featureFlags';
import Icons from 'src/components/Icons';
import CurrencyControl from 'src/explore/components/controls/CurrencyControl';
import CollectionTable from './CollectionTable';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import userEvent from '@testing-library/user-event';
import { render, screen, waitFor } from 'spec/helpers/testing-library';
import DatasourceEditor from 'src/components/Datasource/DatasourceEditor';
import mockDatasource from 'spec/fixtures/mockDatasource';
import * as featureFlags from 'src/featureFlags';
import * as uiCore from '@superset-ui/core';

const props = {
datasource: mockDatasource['7__table'],
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('DatasourceEditor', () => {
describe('enable edit Source tab', () => {
beforeAll(() => {
isFeatureEnabledMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(() => false);
});

Expand Down Expand Up @@ -194,7 +194,7 @@ describe('DatasourceEditor', () => {
describe('render editor with feature flag false', () => {
beforeAll(() => {
isFeatureEnabledMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.spyOn(uiCore, 'isFeatureEnabled')
.mockImplementation(() => true);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { defaultStore as store } from 'spec/helpers/testing-library';
import Modal from 'src/components/Modal';
import { DatasourceModal } from 'src/components/Datasource';
import DatasourceEditor from 'src/components/Datasource/DatasourceEditor';
import * as featureFlags from 'src/featureFlags';
import * as uiCore from '@superset-ui/core';
import mockDatasource from 'spec/fixtures/mockDatasource';
import { api } from 'src/hooks/apiResources/queryApi';

Expand Down Expand Up @@ -69,7 +69,7 @@ describe('DatasourceModal', () => {
let wrapper;
let isFeatureEnabledMock;
beforeEach(async () => {
isFeatureEnabledMock = jest.spyOn(featureFlags, 'isFeatureEnabled');
isFeatureEnabledMock = jest.spyOn(uiCore, 'isFeatureEnabled');
fetchMock.reset();
wrapper = await mountAndWait();
});
Expand Down Expand Up @@ -122,7 +122,6 @@ describe('DatasourceModal', () => {
});

it('renders a legacy data source btn', () => {
featureFlags.DISABLE_LEGACY_DATASOURCE_EDITOR = false;
expect(
wrapper.find('button[data-test="datasource-modal-legacy-edit"]'),
).toExist();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Button from 'src/components/Button';
import {
FeatureFlag,
isDefined,
isFeatureEnabled,
Metric,
styled,
SupersetClient,
Expand All @@ -30,8 +31,6 @@ import {

import Modal from 'src/components/Modal';
import AsyncEsmComponent from 'src/components/AsyncEsmComponent';
import { isFeatureEnabled } from 'src/featureFlags';

import { getClientErrorObject } from 'src/utils/getClientErrorObject';
import withToasts from 'src/components/MessageToasts/withToasts';
import { useSelector } from 'react-redux';
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/components/DynamicPlugins/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import React, { useContext, useEffect, useReducer } from 'react';
import {
ChartMetadata,
defineSharedModules,
isFeatureEnabled,
FeatureFlag,
getChartMetadataRegistry,
logging,
makeApi,
} from '@superset-ui/core';
import { isFeatureEnabled } from 'src/featureFlags';
import { omitBy } from 'lodash';

const metadataRegistry = getChartMetadataRegistry();
Expand Down
Loading