Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenil27 committed Sep 26, 2024
1 parent d728b4c commit ef2b12b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2023 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { render, screen } from '@testing-library/react';
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { act } from 'react-test-renderer';
import { KPI_LIST } from '../../pages/KPIPage/KPIMock.mock';
import KPIChart from './KPIChart';

jest.mock('../../rest/KpiAPI', () => ({
getListKPIs: jest
.fn()
.mockImplementation(() => Promise.resolve({ data: KPI_LIST })),
}));

describe('Test KPIChart Component', () => {
const mockProps = {
chartFilter: {
startTs: 1234567890,
endTs: 1234567899,
},
kpiList: KPI_LIST,
isKpiLoading: false,
viewKPIPermission: true,
createKPIPermission: true,
};

it('Should render KPIChart component', async () => {
await act(async () => {
render(<KPIChart {...mockProps} />, {
wrapper: MemoryRouter,
});
});

const kpiCard = screen.getByTestId('kpi-card');

expect(kpiCard).toBeInTheDocument();
});

it('Should render EmptyGraphPlaceholder when no data is available', async () => {
await act(async () => {
render(<KPIChart {...mockProps} kpiList={[]} />, {
wrapper: MemoryRouter,
});
});

const emptyPlaceholder = screen.getByText(
'message.no-kpi-available-add-new-one'
);

expect(emptyPlaceholder).toBeInTheDocument();
});

it('Should render "Add KPI" button when no KPIs exist and user has create permission', async () => {
await act(async () => {
render(<KPIChart {...mockProps} kpiList={[]} />, {
wrapper: MemoryRouter,
});
});

const addButton = screen.getByText('label.add-entity');

expect(addButton).toBeInTheDocument();
});

it('Should not render "Add KPI" button when no create permission', async () => {
await act(async () => {
render(
<KPIChart {...mockProps} createKPIPermission={false} kpiList={[]} />,
{
wrapper: MemoryRouter,
}
);
});

const addButton = screen.queryByText('label.add-entity');

expect(addButton).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ describe('Add KPI page', () => {
expect(submitButton).toBeInTheDocument();
});

it('should show validation error when description is empty', async () => {
render(<AddKPIPage />, { wrapper: MemoryRouter });

const submitButton = await screen.findByTestId('submit-btn');

await act(async () => {
fireEvent.click(submitButton);
});

const validationMessages = await screen.findAllByText(
'label.field-required'
);
// we have start date and end date field with the same label, hence we have 3 validation messages
// and description is the last field in the form
const lastValidationMessage =
validationMessages[validationMessages.length - 1];

expect(lastValidationMessage).toBeInTheDocument();
});

it.skip('Should render the proper metric input based on metric type', async () => {
render(<AddKPIPage />, { wrapper: MemoryRouter });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* limitations under the License.
*/

import { KpiTargetType } from '../../generated/dataInsight/kpi/kpi';

export const KPI_CHARTS = [
{
id: 'd2f093d4-0ca8-42b8-8721-1c2a59951b59',
Expand Down Expand Up @@ -307,7 +309,7 @@ export const KPI_LIST = [
displayName: 'Description KPI',
fullyQualifiedName: 'description-kpi',
description: '',
metricType: 'PERCENTAGE',
metricType: KpiTargetType.Percentage,
dataInsightChart: {
id: '7dc794d3-1881-408c-92fc-6182aa453bc8',
type: 'dataInsightChart',
Expand Down Expand Up @@ -349,6 +351,7 @@ export const KPI_LIST = [
previousVersion: 0.1,
},
deleted: false,
targetValue: 58,
},
];

Expand Down

0 comments on commit ef2b12b

Please sign in to comment.