2023-04-11 04:51:54 -05:00
|
|
|
import { renderHook, waitFor } from '@testing-library/react';
|
2022-11-28 05:39:12 -06:00
|
|
|
|
|
|
|
import { config } from '@grafana/runtime';
|
|
|
|
|
|
|
|
import {
|
|
|
|
accountIdVariable,
|
|
|
|
dimensionVariable,
|
|
|
|
metricVariable,
|
|
|
|
namespaceVariable,
|
|
|
|
regionVariable,
|
|
|
|
setupMockedDataSource,
|
|
|
|
} from './__mocks__/CloudWatchDataSource';
|
2023-01-17 13:27:53 -06:00
|
|
|
import { setupMockedResourcesAPI } from './__mocks__/ResourcesAPI';
|
2022-11-28 05:39:12 -06:00
|
|
|
import { useAccountOptions, useDimensionKeys, useIsMonitoringAccount, useMetrics } from './hooks';
|
|
|
|
|
|
|
|
const originalFeatureToggleValue = config.featureToggles.cloudWatchCrossAccountQuerying;
|
|
|
|
|
|
|
|
describe('hooks', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
config.featureToggles.cloudWatchCrossAccountQuerying = originalFeatureToggleValue;
|
|
|
|
});
|
|
|
|
describe('useIsMonitoringAccount', () => {
|
|
|
|
it('should interpolate variables before calling api', async () => {
|
|
|
|
config.featureToggles.cloudWatchCrossAccountQuerying = true;
|
2023-01-17 13:27:53 -06:00
|
|
|
const { api } = setupMockedResourcesAPI({
|
2022-11-28 05:39:12 -06:00
|
|
|
variables: [regionVariable],
|
|
|
|
});
|
|
|
|
const isMonitoringAccountMock = jest.fn().mockResolvedValue(true);
|
|
|
|
api.isMonitoringAccount = isMonitoringAccountMock;
|
|
|
|
|
2023-04-11 04:51:54 -05:00
|
|
|
renderHook(() => useIsMonitoringAccount(api, `$${regionVariable.name}`));
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(isMonitoringAccountMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(isMonitoringAccountMock).toHaveBeenCalledWith(regionVariable.current.value);
|
|
|
|
});
|
2022-11-28 05:39:12 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('useMetricNames', () => {
|
|
|
|
it('should interpolate variables before calling api', async () => {
|
|
|
|
const { datasource } = setupMockedDataSource({
|
|
|
|
variables: [regionVariable, namespaceVariable, accountIdVariable],
|
|
|
|
});
|
|
|
|
const getMetricsMock = jest.fn().mockResolvedValue([]);
|
2023-01-17 13:27:53 -06:00
|
|
|
datasource.resources.getMetrics = getMetricsMock;
|
2022-11-28 05:39:12 -06:00
|
|
|
|
2023-04-11 04:51:54 -05:00
|
|
|
renderHook(() =>
|
2022-11-28 05:39:12 -06:00
|
|
|
useMetrics(datasource, {
|
|
|
|
namespace: `$${namespaceVariable.name}`,
|
|
|
|
region: `$${regionVariable.name}`,
|
|
|
|
accountId: `$${accountIdVariable.name}`,
|
|
|
|
})
|
|
|
|
);
|
2023-04-11 04:51:54 -05:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(getMetricsMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(getMetricsMock).toHaveBeenCalledWith({
|
|
|
|
region: regionVariable.current.value,
|
|
|
|
namespace: namespaceVariable.current.value,
|
|
|
|
accountId: accountIdVariable.current.value,
|
|
|
|
});
|
2022-11-28 05:39:12 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('useDimensionKeys', () => {
|
|
|
|
it('should interpolate variables before calling api', async () => {
|
|
|
|
const { datasource } = setupMockedDataSource({
|
|
|
|
mockGetVariableName: true,
|
|
|
|
variables: [regionVariable, namespaceVariable, accountIdVariable, metricVariable, dimensionVariable],
|
|
|
|
});
|
|
|
|
const getDimensionKeysMock = jest.fn().mockResolvedValue([]);
|
2023-01-17 13:27:53 -06:00
|
|
|
datasource.resources.getDimensionKeys = getDimensionKeysMock;
|
2022-11-28 05:39:12 -06:00
|
|
|
|
2023-04-11 04:51:54 -05:00
|
|
|
renderHook(() =>
|
2022-11-28 05:39:12 -06:00
|
|
|
useDimensionKeys(datasource, {
|
|
|
|
namespace: `$${namespaceVariable.name}`,
|
|
|
|
metricName: `$${metricVariable.name}`,
|
|
|
|
region: `$${regionVariable.name}`,
|
|
|
|
accountId: `$${accountIdVariable.name}`,
|
|
|
|
dimensionFilters: {
|
|
|
|
environment: `$${dimensionVariable.name}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2023-04-11 04:51:54 -05:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(getDimensionKeysMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(getDimensionKeysMock).toHaveBeenCalledWith({
|
|
|
|
region: regionVariable.current.value,
|
|
|
|
namespace: namespaceVariable.current.value,
|
|
|
|
metricName: metricVariable.current.value,
|
|
|
|
accountId: accountIdVariable.current.value,
|
|
|
|
dimensionFilters: {
|
|
|
|
environment: [dimensionVariable.current.value],
|
|
|
|
},
|
|
|
|
});
|
2022-11-28 05:39:12 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('useAccountOptions', () => {
|
|
|
|
it('does not call the api if the feature toggle is off', async () => {
|
|
|
|
config.featureToggles.cloudWatchCrossAccountQuerying = false;
|
2023-01-17 13:27:53 -06:00
|
|
|
const { api } = setupMockedResourcesAPI({
|
2022-11-28 05:39:12 -06:00
|
|
|
variables: [regionVariable],
|
|
|
|
});
|
|
|
|
const getAccountsMock = jest.fn().mockResolvedValue([{ id: '123', label: 'accountLabel' }]);
|
|
|
|
api.getAccounts = getAccountsMock;
|
2023-04-11 04:51:54 -05:00
|
|
|
renderHook(() => useAccountOptions(api, `$${regionVariable.name}`));
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(getAccountsMock).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
2022-11-28 05:39:12 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
it('interpolates region variables before calling the api', async () => {
|
|
|
|
config.featureToggles.cloudWatchCrossAccountQuerying = true;
|
2023-01-17 13:27:53 -06:00
|
|
|
const { api } = setupMockedResourcesAPI({
|
2022-11-28 05:39:12 -06:00
|
|
|
variables: [regionVariable],
|
|
|
|
});
|
|
|
|
const getAccountsMock = jest.fn().mockResolvedValue([{ id: '123', label: 'accountLabel' }]);
|
|
|
|
api.getAccounts = getAccountsMock;
|
2023-04-11 04:51:54 -05:00
|
|
|
renderHook(() => useAccountOptions(api, `$${regionVariable.name}`));
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(getAccountsMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(getAccountsMock).toHaveBeenCalledWith({ region: regionVariable.current.value });
|
|
|
|
});
|
2022-11-28 05:39:12 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns properly formatted account options, and template variables', async () => {
|
|
|
|
config.featureToggles.cloudWatchCrossAccountQuerying = true;
|
2023-01-17 13:27:53 -06:00
|
|
|
const { api } = setupMockedResourcesAPI({
|
2022-11-28 05:39:12 -06:00
|
|
|
variables: [regionVariable],
|
|
|
|
});
|
|
|
|
const getAccountsMock = jest.fn().mockResolvedValue([{ id: '123', label: 'accountLabel' }]);
|
|
|
|
api.getAccounts = getAccountsMock;
|
2023-04-11 04:51:54 -05:00
|
|
|
const { result } = renderHook(() => useAccountOptions(api, `$${regionVariable.name}`));
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(result.current.value).toEqual([
|
|
|
|
{ label: 'accountLabel', description: '123', value: '123' },
|
|
|
|
{ label: 'Template Variables', options: [{ label: '$region', value: '$region' }] },
|
|
|
|
]);
|
|
|
|
});
|
2022-11-28 05:39:12 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|