Skip to content

Commit

Permalink
adding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jun 22, 2021
1 parent 585dcb9 commit 915754f
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<TutorialAgentSecretTokenSelector
variantId="java"
http={
({
get: jest.fn(),
} as unknown) as HttpStart
}
basePath="http://localhost:5601"
isCloudEnabled
/>
);
expect(component.getByTestId('loading')).toBeInTheDocument();
});
it('updates commands when a different policy is selected', async () => {
const component = render(
<TutorialAgentSecretTokenSelector
variantId="java"
http={
({
get: jest.fn().mockReturnValue({
cloudStandaloneSetup: undefined,
agents,
}),
} as unknown) as HttpStart
}
basePath="http://localhost:5601"
isCloudEnabled={false}
/>
);
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-&lt;version&gt;.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-&lt;version&gt;.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(
<TutorialAgentSecretTokenSelector
variantId="java"
http={
({
get: jest.fn().mockReturnValue({
cloudStandaloneSetup: undefined,
agents,
}),
} as unknown) as HttpStart
}
basePath="http://localhost:5601"
isCloudEnabled={false}
/>
);
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-&lt;version&gt;.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(
<TutorialAgentSecretTokenSelector
variantId="java"
http={
({
get: jest.fn().mockReturnValue({
cloudStandaloneSetup: {
apmServerUrl: 'cloud_url',
secretToken: 'cloud_token',
},
agents,
}),
} as unknown) as HttpStart
}
basePath="http://localhost:5601"
isCloudEnabled
/>
);
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-&lt;version&gt;.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(
<TutorialAgentSecretTokenSelector
variantId="java"
http={
({
get: jest.fn().mockReturnValue({
cloudStandaloneSetup: {
apmServerUrl: 'cloud_url',
secretToken: 'cloud_token',
},
agents: [...agents, policyElasticAgentOnCloudAgent],
}),
} as unknown) as HttpStart
}
basePath="http://localhost:5601"
isCloudEnabled
/>
);
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-&lt;version&gt;.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"
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -85,7 +87,7 @@ function TutorialAgentSecretTokenSelector({

if (isLoading) {
return (
<CentralizedContainer>
<CentralizedContainer data-test-subj="loading">
<EuiLoadingSpinner />
</CentralizedContainer>
);
Expand All @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function PolicySelector({
<EuiFormRow
label={i18n.translate(
'xpack.apm.tutorial.agent_config.choosePolicyLabel',
{ defaultMessage: 'Choose a policy' }
{ defaultMessage: 'Choose policy' }
)}
labelAppend={
<EuiText size="xs">
Expand All @@ -49,6 +49,7 @@ export function PolicySelector({
}
>
<EuiComboBox
data-test-subj={`policySelector_${selectedOption?.key}`}
isClearable={false}
singleSelection={{ asPlainText: true }}
options={options}
Expand Down

0 comments on commit 915754f

Please sign in to comment.