From 915754fe1f59c9a60c7a6d8d6e7108974f54fccc Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Tue, 22 Jun 2021 14:44:00 -0400 Subject: [PATCH] adding unit test --- .../tutorial/config_agent/index.test.tsx | 208 ++++++++++++++++++ .../shared/tutorial/config_agent/index.tsx | 8 +- .../tutorial/config_agent/policy_selector.tsx | 3 +- 3 files changed, 215 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.test.tsx b/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.test.tsx index 1fec1c76430ebd..9947517d060248 100644 --- a/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.test.tsx +++ b/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.test.tsx @@ -4,3 +4,211 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ +import { fireEvent, render, screen } from '@testing-library/react'; +import { HttpStart } from 'kibana/public'; +import React from 'react'; +import TutorialAgentSecretTokenSelector from './'; + +const policyElasticAgentOnCloudAgent = { + id: 'policy-elastic-agent-on-cloud', + name: 'Elastic Cloud agent policy', + apmServerUrl: 'apm_cloud_url', + secretToken: 'apm_cloud_token', +}; + +const agents = [ + { + id: '1', + name: 'agent foo', + apmServerUrl: 'foo', + secretToken: 'foo', + }, + { + id: '2', + name: 'agent bar', + apmServerUrl: 'bar', + secretToken: 'bar', + }, +]; + +describe('TutorialAgentSecretTokenSelector', () => { + beforeAll(() => { + jest.spyOn(console, 'error').mockImplementation(() => null); + }); + + afterAll(() => { + jest.restoreAllMocks(); + }); + it('renders loading component while API is being called', () => { + const component = render( + + ); + expect(component.getByTestId('loading')).toBeInTheDocument(); + }); + it('updates commands when a different policy is selected', async () => { + const component = render( + + ); + expect( + await screen.findByText('Default Standalone configuration') + ).toBeInTheDocument(); + let commands = component.getByTestId('commands').innerHTML; + expect(commands).not.toEqual(''); + expect(commands).toMatchInlineSnapshot(` + "java -javaagent:/path/to/elastic-apm-agent-<version>.jar \\\\ + -Delastic.apm.service_name=my-application \\\\ + -Delastic.apm.server_urls=http://localhost:8200 \\\\ + -Delastic.apm.secret_token= \\\\ + -Delastic.apm.environment=production \\\\ + -Delastic.apm.application_packages=org.example \\\\ + -jar my-application.jar" + `); + + fireEvent.click(component.getByTestId('comboBoxToggleListButton')); + fireEvent.click(component.getByText('agent foo')); + commands = component.getByTestId('commands').innerHTML; + expect(commands).not.toEqual(''); + expect(commands).toMatchInlineSnapshot(` + "java -javaagent:/path/to/elastic-apm-agent-<version>.jar \\\\ + -Delastic.apm.service_name=my-application \\\\ + -Delastic.apm.server_urls=foo \\\\ + -Delastic.apm.secret_token=foo \\\\ + -Delastic.apm.environment=production \\\\ + -Delastic.apm.application_packages=org.example \\\\ + -jar my-application.jar" + `); + }); + describe('running on prem', () => { + it('selects defaul standalone by defauls', async () => { + const component = render( + + ); + expect( + await screen.findByText('Default Standalone configuration') + ).toBeInTheDocument(); + expect( + component.getByTestId('policySelector_onPrem_standalone') + ).toBeInTheDocument(); + const commands = component.getByTestId('commands').innerHTML; + expect(commands).not.toEqual(''); + expect(commands).toMatchInlineSnapshot(` + "java -javaagent:/path/to/elastic-apm-agent-<version>.jar \\\\ + -Delastic.apm.service_name=my-application \\\\ + -Delastic.apm.server_urls=http://localhost:8200 \\\\ + -Delastic.apm.secret_token= \\\\ + -Delastic.apm.environment=production \\\\ + -Delastic.apm.application_packages=org.example \\\\ + -jar my-application.jar" + `); + }); + }); + describe('running on cloud', () => { + it('selects defaul standalone by defauls', async () => { + const component = render( + + ); + expect( + await screen.findByText('Default Standalone configuration') + ).toBeInTheDocument(); + expect( + component.getByTestId('policySelector_cloud_standalone') + ).toBeInTheDocument(); + const commands = component.getByTestId('commands').innerHTML; + expect(commands).not.toEqual(''); + expect(commands).toMatchInlineSnapshot(` + "java -javaagent:/path/to/elastic-apm-agent-<version>.jar \\\\ + -Delastic.apm.service_name=my-application \\\\ + -Delastic.apm.server_urls=cloud_url \\\\ + -Delastic.apm.secret_token=cloud_token \\\\ + -Delastic.apm.environment=production \\\\ + -Delastic.apm.application_packages=org.example \\\\ + -jar my-application.jar" + `); + }); + it('selects policy elastic agent on cloud when available by default', async () => { + const component = render( + + ); + expect( + await screen.findByText('Elastic Cloud agent policy') + ).toBeInTheDocument(); + expect( + component.getByTestId('policySelector_policy-elastic-agent-on-cloud') + ).toBeInTheDocument(); + const commands = component.getByTestId('commands').innerHTML; + expect(commands).not.toEqual(''); + expect(commands).toMatchInlineSnapshot(` + "java -javaagent:/path/to/elastic-apm-agent-<version>.jar \\\\ + -Delastic.apm.service_name=my-application \\\\ + -Delastic.apm.server_urls=apm_cloud_url \\\\ + -Delastic.apm.secret_token=apm_cloud_token \\\\ + -Delastic.apm.environment=production \\\\ + -Delastic.apm.application_packages=org.example \\\\ + -jar my-application.jar" + `); + }); + }); +}); diff --git a/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.tsx b/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.tsx index e5badb81928c2b..06939decdf53ab 100644 --- a/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/index.tsx @@ -64,7 +64,9 @@ function TutorialAgentSecretTokenSelector({ setIsLoading(true); try { const response = await http.get('/api/apm/fleet/agents'); - setData(response as APIResponseType); + if (response) { + setData(response as APIResponseType); + } } catch (e) { console.error('Error while fetching fleet agents.', e); } @@ -85,7 +87,7 @@ function TutorialAgentSecretTokenSelector({ if (isLoading) { return ( - + ); @@ -107,7 +109,7 @@ function TutorialAgentSecretTokenSelector({ } : { label: GET_STARTED_WITH_FLEET_LABEL, - href: `${basePath}/app/fleet#/integrations/detail/apm-0.2.0/overview`, + href: `${basePath}/app/integrations#/detail/apm-0.2.0/overview`, }; return ( diff --git a/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/policy_selector.tsx b/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/policy_selector.tsx index 54b84ad6926c4f..ae337cd7902a9e 100644 --- a/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/policy_selector.tsx +++ b/x-pack/plugins/apm/public/components/shared/tutorial/config_agent/policy_selector.tsx @@ -40,7 +40,7 @@ export function PolicySelector({ @@ -49,6 +49,7 @@ export function PolicySelector({ } >