Skip to content

Commit cca31e9

Browse files
committed
adding unit test
1 parent 1188e7f commit cca31e9

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { fetchData, hasData } from './observability_dashboard';
8+
import * as createCallApmApi from './createCallApmApi';
9+
10+
describe('Observability dashboard data', () => {
11+
const callApmApiMock = jest.spyOn(createCallApmApi, 'callApmApi');
12+
afterEach(() => {
13+
callApmApiMock.mockClear();
14+
});
15+
describe('hasData', () => {
16+
it('returns false when no data is available', async () => {
17+
callApmApiMock.mockImplementation(() => Promise.resolve(false));
18+
const response = await hasData();
19+
expect(response).toBeFalsy();
20+
});
21+
it('returns true when data is available', async () => {
22+
callApmApiMock.mockImplementation(() => Promise.resolve(true));
23+
const response = await hasData();
24+
expect(response).toBeTruthy();
25+
});
26+
});
27+
28+
describe('fetchData', () => {
29+
it('returns APM data with series and stats', async () => {
30+
callApmApiMock.mockImplementation(() =>
31+
Promise.resolve({
32+
serviceCount: 10,
33+
transactionCoordinates: [
34+
{ x: 1, y: 1 },
35+
{ x: 2, y: 2 },
36+
{ x: 3, y: 3 },
37+
],
38+
})
39+
);
40+
const response = await fetchData({
41+
startTime: '1',
42+
endTime: '2',
43+
bucketSize: '3',
44+
});
45+
expect(response).toEqual({
46+
title: 'APM',
47+
appLink: '/app/apm',
48+
stats: {
49+
services: {
50+
label: 'Services',
51+
value: 10,
52+
},
53+
transactions: {
54+
label: 'Transactions',
55+
value: 6,
56+
},
57+
},
58+
series: {
59+
transactions: {
60+
label: 'Transactions',
61+
coordinates: [
62+
{ x: 1, y: 1 },
63+
{ x: 2, y: 2 },
64+
{ x: 3, y: 3 },
65+
],
66+
color: 'euiColorVis1',
67+
},
68+
},
69+
});
70+
});
71+
it('returns empty transaction coordinates', async () => {
72+
callApmApiMock.mockImplementation(() =>
73+
Promise.resolve({
74+
serviceCount: 0,
75+
transactionCoordinates: [],
76+
})
77+
);
78+
const response = await fetchData({
79+
startTime: '1',
80+
endTime: '2',
81+
bucketSize: '3',
82+
});
83+
expect(response).toEqual({
84+
title: 'APM',
85+
appLink: '/app/apm',
86+
stats: {
87+
services: {
88+
label: 'Services',
89+
value: 0,
90+
},
91+
transactions: {
92+
label: 'Transactions',
93+
value: 0,
94+
},
95+
},
96+
series: {
97+
transactions: {
98+
label: 'Transactions',
99+
coordinates: [],
100+
color: 'euiColorVis1',
101+
},
102+
},
103+
});
104+
});
105+
});
106+
});

0 commit comments

Comments
 (0)