Skip to content

Commit

Permalink
[8.9] [Synthetics] Call enable Synthetics from Getting started and Ad…
Browse files Browse the repository at this point in the history
…d monitor flow (#160360) (#160662)

# Backport

This will backport the following commits from `main` to `8.9`:
- [[Synthetics] Call enable Synthetics from Getting started and Add
monitor flow (#160360)](#160360)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2023-06-27T16:18:19Z","message":"[Synthetics]
Call enable Synthetics from Getting started and Add monitor flow
(#160360)","sha":"58b3c3298f5c024045c0fd1642503857aab82277","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:fix","Team:uptime","v8.9.0","v8.10.0"],"number":160360,"url":"#160360
Call enable Synthetics from Getting started and Add monitor flow
(#160360)","sha":"58b3c3298f5c024045c0fd1642503857aab82277"}},"sourceBranch":"main","suggestedTargetBranches":["8.9"],"targetPullRequestStates":[{"branch":"8.9","label":"v8.9.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"#160360
Call enable Synthetics from Getting started and Add monitor flow
(#160360)","sha":"58b3c3298f5c024045c0fd1642503857aab82277"}}]}]
BACKPORT-->

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
  • Loading branch information
kibanamachine and shahzad31 committed Jun 27, 2023
1 parent 2f5b730 commit a5058ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
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

0 comments on commit a5058ba

Please sign in to comment.