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

[Synthetics] Call enable Synthetics from Getting started and Add monitor flow #160360

Merged
merged 7 commits into from Jun 27, 2023
Expand Up @@ -13,6 +13,7 @@ import * as privateLocationsHooks from '../settings/private_locations/hooks/use_
import * as settingsHooks from '../../contexts/synthetics_settings_context';
import { SyntheticsSettingsContextValues } from '../../contexts/synthetics_settings_context';
import { fireEvent } from '@testing-library/react';
import { kibanaService } from '../../../../utils/kibana_service';

describe('GettingStartedPage', () => {
beforeEach(() => {
Expand Down Expand Up @@ -163,4 +164,23 @@ describe('GettingStartedPage', () => {
await findByText(/You do not have sufficient permissions to perform this action./)
).toBeInTheDocument();
});

it('should call enablement API and redirect to monitors', function () {
render(<GettingStartedPage />, {
state: {
syntheticsEnablement: {
loading: false,
enablement: {
canEnable: false,
isEnabled: false,
},
},
},
});

// page is loaded
expect(kibanaService.core.application.navigateToApp).toHaveBeenCalledWith('synthetics', {
path: '/monitors',
});
});
});
Expand Up @@ -20,7 +20,7 @@ import { useHistory } from 'react-router-dom';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import styled from 'styled-components';
import { useBreadcrumbs, useLocations } from '../../hooks';
import { useBreadcrumbs, useEnablement, useLocations } from '../../hooks';
import { usePrivateLocationsAPI } from '../settings/private_locations/hooks/use_locations_api';
import { LoadingState } from '../monitors_page/overview/overview/monitor_detail_flyout';
import {
Expand All @@ -40,6 +40,8 @@ export const GettingStartedPage = () => {
const dispatch = useDispatch();
const history = useHistory();

useEnablement();

useEffect(() => {
dispatch(getServiceLocations());
dispatch(getAgentPoliciesAction.get());
Expand Down
Expand Up @@ -18,13 +18,21 @@ import React from 'react';
import { act, fireEvent, waitFor } from '@testing-library/react';
import { syntheticsTestSubjects } from '../../../../../common/constants/data_test_subjects';
import { apiService } from '../../../../utils/api_service';
import * as reduxHooks from 'react-redux';

describe('SimpleMonitorForm', () => {
const apiSpy = jest.spyOn(apiService, 'post');
const dispatchSpy = jest.spyOn(reduxHooks, 'useDispatch');

it('renders', async () => {
render(<SimpleMonitorForm />);
expect(screen.getByText(WEBSITE_URL_LABEL)).toBeInTheDocument();
expect(screen.getByText(WEBSITE_URL_HELP_TEXT)).toBeInTheDocument();

// calls enabled API
await waitFor(async () => {
expect(dispatchSpy).toHaveBeenCalledTimes(3);
});
});

it('do not show validation error on touch', async () => {
Expand Down
Expand Up @@ -9,6 +9,7 @@ import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useTrackPageview } from '@kbn/observability-shared-plugin/public';

import { useEnablement } from '../../hooks';
import { getServiceLocations, selectServiceLocationsState } from '../../state';
import { ServiceAllowedWrapper } from '../common/wrappers/service_allowed_wrapper';

Expand All @@ -25,6 +26,9 @@ export const MonitorAddPage = () => {
const { space } = useKibanaSpace();
useTrackPageview({ app: 'synthetics', path: 'add-monitor', delay: 15000 });
useMonitorAddEditBreadcrumbs();

useEnablement();

const dispatch = useDispatch();
useEffect(() => {
dispatch(getServiceLocations());
Expand Down
Expand Up @@ -5,13 +5,16 @@
* 2.0.
*/

import { useKibana } from '@kbn/kibana-react-plugin/public';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getSyntheticsEnablement, selectSyntheticsEnablement } from '../state';

export function useEnablement() {
const dispatch = useDispatch();

const { application } = useKibana().services;

const { loading, error, enablement } = useSelector(selectSyntheticsEnablement);

useEffect(() => {
Expand All @@ -20,6 +23,14 @@ export function useEnablement() {
}
}, [dispatch, enablement, error, loading]);

useEffect(() => {
if (!enablement?.canEnable && !enablement?.isEnabled && !loading && enablement) {
application?.navigateToApp('synthetics', {
path: '/monitors',
});
}
}, [application, enablement, loading]);

return {
enablement: {
areApiKeysEnabled: enablement?.areApiKeysEnabled,
Expand Down