Skip to content

Commit

Permalink
OHRI-2084: Add unit tests for patient table in commons lib (#1770)
Browse files Browse the repository at this point in the history
* Add unit tests for patient table in commons lib

* Add unit tests for patient table in commons lib

* more test added

* Update patient-table.test.tsx
  • Loading branch information
lucyjemutai authored Jan 16, 2024
1 parent 4e02002 commit 1567f26
Showing 1 changed file with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import { PatientTable } from './patient-table.component';
import '@testing-library/jest-dom';

describe('PatientTable', () => {
const mockPatients = [{ name: 'John Doe', age: 35 }];

const mockColumns = [
{ key: 'name', header: 'Name', getValue: (patient) => patient.name },
{ key: 'age', header: 'Age', getValue: (patient) => patient.age },
];

const mockPagination = {
usePagination: true,
currentPage: 1,
onChange: jest.fn(),
pageSize: 10,
totalItems: 20,
};

it('renders patient table correctly with specific columns and patient', () => {
const { getByText } = render(
<PatientTable
columns={mockColumns}
isFetching={false}
isLoading={false}
mutateListDetails={() => {}}
mutateListMembers={() => {}}
pagination={mockPagination}
patients={mockPatients}
/>,
);

// Test whether the specific patient 'John Doe' and age (35) is rendered in the table
const johnDoeName = getByText('John Doe');
expect(johnDoeName).toBeInTheDocument();
const age35 = getByText('35');
expect(age35).toBeInTheDocument();
});

it('renders loading skeleton when isLoading is true', () => {
const { getByTestId } = render(
<PatientTable
columns={mockColumns}
isFetching={false}
isLoading={true}
mutateListDetails={() => {}}
mutateListMembers={() => {}}
pagination={mockPagination}
patients={[]}
/>,
);

// Test whether the loading skeleton is rendered
const skeleton = getByTestId('data-table-skeleton');
expect(skeleton).toBeInTheDocument();
});

it('renders empty state when no patients are present', () => {
const { getByText } = render(
<PatientTable
columns={mockColumns}
isFetching={false}
isLoading={false}
mutateListDetails={() => {}}
mutateListMembers={() => {}}
pagination={mockPagination}
patients={[]}
/>,
);

// Test whether the empty state message is rendered
const emptyStateMessage = getByText('There are no patients in this list');
expect(emptyStateMessage).toBeInTheDocument();
});

it('filters patients based on search term', () => {
const { getByText, getByPlaceholderText } = render(
<PatientTable
columns={mockColumns}
isFetching={false}
isLoading={false}
mutateListDetails={() => {}}
mutateListMembers={() => {}}
pagination={mockPagination}
patients={mockPatients}
/>,
);

// Search for a specific patient
const searchInput = getByPlaceholderText('Search this list');
fireEvent.change(searchInput, { target: { value: 'John' } });

// Check if the filtered patient is displayed
const johnDoeName = getByText('John Doe');
expect(johnDoeName).toBeInTheDocument();
});
});

0 comments on commit 1567f26

Please sign in to comment.