From 690bbb662dbca604ab077652f002504002430782 Mon Sep 17 00:00:00 2001 From: Bilawal Hameed Date: Thu, 26 Mar 2020 21:07:06 +0100 Subject: [PATCH] [plugins/welcome] split feature flag button into separate Component (with tests) --- .../ToggleFeatureFlagButton.test.tsx | 68 +++++++++++++++++++ .../WelcomePage/ToggleFeatureFlagButton.tsx | 49 +++++++++++++ .../components/WelcomePage/WelcomePage.tsx | 22 +----- 3 files changed, 119 insertions(+), 20 deletions(-) create mode 100644 plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.test.tsx create mode 100644 plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.tsx diff --git a/plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.test.tsx b/plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.test.tsx new file mode 100644 index 0000000000..8efb25f0a6 --- /dev/null +++ b/plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.test.tsx @@ -0,0 +1,68 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import ToggleFeatureFlagButton from './ToggleFeatureFlagButton'; +import { + ApiRegistry, + featureFlagsApiRef, + ApiProvider, + FeatureFlags, +} from '@backstage/core'; + +describe('ToggleFeatureFlagButton', () => { + beforeEach(() => { + window.localStorage.clear(); + }); + + it('should enable the feature flag', () => { + const rendered = render( + + + , + ); + + const button = rendered.getByTestId('button-switch-feature-flag-state'); + expect(button).toBeInTheDocument(); + + expect(window.localStorage.featureFlags).toBeUndefined(); + fireEvent.click(button); + expect(window.localStorage.featureFlags).toBe( + '{"enable-welcome-box":true}', + ); + }); + + it('should disable the feature flag', () => { + const rendered = render( + + + , + ); + + const button = rendered.getByTestId('button-switch-feature-flag-state'); + expect(button).toBeInTheDocument(); + + expect(window.localStorage.featureFlags).toBeUndefined(); + fireEvent.click(button); + fireEvent.click(button); + expect(window.localStorage.featureFlags).toBe('{}'); + }); +}); diff --git a/plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.tsx b/plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.tsx new file mode 100644 index 0000000000..f9cf68f20c --- /dev/null +++ b/plugins/welcome/src/components/WelcomePage/ToggleFeatureFlagButton.tsx @@ -0,0 +1,49 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { FC } from 'react'; +import { Button } from '@material-ui/core'; +import { FeatureFlagState, featureFlagsApiRef, useApi } from '@backstage/core'; + +const ToggleFeatureFlagButton: FC<{}> = () => { + const featureFlagsApi = useApi(featureFlagsApiRef); + + const handleClick = () => { + const isEnabled = featureFlagsApi.get('enable-welcome-box'); + featureFlagsApi.set( + 'enable-welcome-box', + isEnabled ? FeatureFlagState.NotEnabled : FeatureFlagState.Enabled, + ); + window.location.reload(); + }; + + return ( + + ); +}; + +export default ToggleFeatureFlagButton; diff --git a/plugins/welcome/src/components/WelcomePage/WelcomePage.tsx b/plugins/welcome/src/components/WelcomePage/WelcomePage.tsx index 7d64b1470a..597ef5044b 100644 --- a/plugins/welcome/src/components/WelcomePage/WelcomePage.tsx +++ b/plugins/welcome/src/components/WelcomePage/WelcomePage.tsx @@ -23,7 +23,6 @@ import { ListItem, ListItemText, Link, - Button, } from '@material-ui/core'; import Timer from '../Timer'; import { @@ -34,13 +33,11 @@ import { pageTheme, ContentHeader, SupportButton, - featureFlagsApiRef, - useApi, } from '@backstage/core'; import ErrorButton from './ErrorButton'; +import ToggleFeatureFlagButton from './ToggleFeatureFlagButton'; const WelcomePage: FC<{}> = () => { - const featureFlagsApi = useApi(featureFlagsApiRef); const profile = { givenName: '' }; return ( @@ -127,22 +124,7 @@ const WelcomePage: FC<{}> = () => {

- +