[Security Solution] Deprecate useVariation (#188525)

This commit is contained in:
Angela Chuang 2024-07-18 15:14:20 +01:00 committed by GitHub
parent 179b78b499
commit e4852dc703
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 3 additions and 122 deletions

View file

@ -7,7 +7,6 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { useLocation } from 'react-router-dom';
import { useVariationMock } from '../../../common/components/utils.mocks';
import { GlobalHeader } from '.';
import {
ADD_DATA_PATH,
@ -60,10 +59,6 @@ describe('global header', () => {
};
const store = createMockStore(state);
beforeEach(() => {
useVariationMock.mockReset();
});
it('has add data link', () => {
(useLocation as jest.Mock).mockReturnValue([
{ pageName: SecurityPageName.overview, detailName: undefined },
@ -100,26 +95,6 @@ describe('global header', () => {
expect(link?.getAttribute('href')).toBe(ADD_THREAT_INTELLIGENCE_DATA_PATH);
});
it('points to the resolved Add data URL by useVariation', () => {
(useLocation as jest.Mock).mockReturnValue([
{ pageName: SecurityPageName.overview, detailName: undefined },
]);
const customResolvedUrl = '/test/url';
useVariationMock.mockImplementationOnce(
(cloudExperiments, featureFlagName, defaultValue, setter) => {
setter(customResolvedUrl);
}
);
const { queryByTestId } = render(
<TestProviders store={store}>
<GlobalHeader />
</TestProviders>
);
const link = queryByTestId('add-data');
expect(link?.getAttribute('href')).toBe(customResolvedUrl);
});
it.each(sourcererPaths)('shows sourcerer on %s page', (pathname) => {
(useLocation as jest.Mock).mockReturnValue({ pathname });

View file

@ -1,18 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { useVariation } from './utils';
export const useVariationMock: jest.MockedFunction<typeof useVariation> = jest.fn();
jest.doMock('./utils', () => {
const actualUtils = jest.requireActual('./utils');
return {
...actualUtils,
useVariation: useVariationMock,
};
});

View file

@ -1,40 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { renderHook } from '@testing-library/react-hooks';
import { cloudExperimentsMock } from '@kbn/cloud-experiments-plugin/common/mocks';
import { useVariation } from './utils';
describe('useVariation', () => {
test('it should call the setter if cloudExperiments is enabled', async () => {
const cloudExperiments = cloudExperimentsMock.createStartMock();
cloudExperiments.getVariation.mockResolvedValue('resolved value');
const setter = jest.fn();
const { result } = renderHook(() =>
useVariation(
cloudExperiments,
'security-solutions.add-integrations-url',
'my default value',
setter
)
);
await new Promise((resolve) => process.nextTick(resolve));
expect(result.error).toBe(undefined);
expect(setter).toHaveBeenCalledTimes(1);
expect(setter).toHaveBeenCalledWith('resolved value');
});
test('it should not call the setter if cloudExperiments is not enabled', async () => {
const setter = jest.fn();
const { result } = renderHook(() =>
useVariation(undefined, 'security-solutions.add-integrations-url', 'my default value', setter)
);
await new Promise((resolve) => process.nextTick(resolve));
expect(result.error).toBe(undefined);
expect(setter).not.toHaveBeenCalled();
});
});

View file

@ -6,14 +6,10 @@
*/
import { throttle } from 'lodash/fp';
import { useEffect, useMemo, useState } from 'react';
import { useMemo, useState } from 'react';
import useResizeObserver from 'use-resize-observer/polyfilled';
import { niceTimeFormatByDay, timeFormatter } from '@elastic/charts';
import moment from 'moment-timezone';
import type {
CloudExperimentsFeatureFlagNames,
CloudExperimentsPluginStart,
} from '@kbn/cloud-experiments-plugin/common';
export const getDaysDiff = (minDate: moment.Moment, maxDate: moment.Moment) => {
const diff = maxDate.diff(minDate, 'days');
@ -39,26 +35,3 @@ export const useThrottledResizeObserver = (wait = 100) => {
return { ref, ...size };
};
/**
* Retrieves the variation of the feature flag if the cloudExperiments plugin is enabled.
* @param cloudExperiments {@link CloudExperimentsPluginStart}
* @param featureFlagName The name of the feature flag {@link CloudExperimentsFeatureFlagNames}
* @param defaultValue The default value in case it cannot retrieve the feature flag
* @param setter The setter from {@link useState} to update the value.
*/
export const useVariation = <Data>(
cloudExperiments: CloudExperimentsPluginStart | undefined,
featureFlagName: CloudExperimentsFeatureFlagNames,
defaultValue: Data,
setter: (value: Data) => void
) => {
useEffect(() => {
(async function loadVariation() {
const variationUrl = await cloudExperiments?.getVariation(featureFlagName, defaultValue);
if (variationUrl) {
setter(variationUrl);
}
})();
}, [cloudExperiments, featureFlagName, defaultValue, setter]);
};

View file

@ -4,11 +4,10 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useMemo } from 'react';
import { useLocation } from 'react-router-dom';
import { ADD_DATA_PATH, ADD_THREAT_INTELLIGENCE_DATA_PATH } from '../../../common/constants';
import { isThreatIntelligencePath } from '../../helpers';
import { useVariation } from '../components/utils';
import { useKibana, useNavigateTo } from '../lib/kibana';
@ -17,7 +16,6 @@ export const useAddIntegrationsUrl = () => {
http: {
basePath: { prepend },
},
cloudExperiments,
} = useKibana().services;
const { pathname } = useLocation();
const { navigateTo } = useNavigateTo();
@ -25,15 +23,8 @@ export const useAddIntegrationsUrl = () => {
const isThreatIntelligence = isThreatIntelligencePath(pathname);
const integrationsUrl = isThreatIntelligence ? ADD_THREAT_INTELLIGENCE_DATA_PATH : ADD_DATA_PATH;
const [addIntegrationsUrl, setAddIntegrationsUrl] = useState(integrationsUrl);
useVariation(
cloudExperiments,
'security-solutions.add-integrations-url',
integrationsUrl,
setAddIntegrationsUrl
);
const href = useMemo(() => prepend(addIntegrationsUrl), [prepend, addIntegrationsUrl]);
const href = useMemo(() => prepend(integrationsUrl), [prepend, integrationsUrl]);
const onClick = useCallback(
(e) => {