Skip to content

Commit

Permalink
test: adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Nov 18, 2024
1 parent 39da9f1 commit 1c71d6c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { render, screen, fireEvent } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import SearchScreenDataTable from '../../../../components/SilvicultureSearch/Openings/SearchScreenDataTable';
import { columns } from '../../../../components/SilvicultureSearch/Openings/SearchScreenDataTable/headerData';
Expand All @@ -11,6 +11,7 @@ import { OpeningsSearchProvider } from '../../../../contexts/search/OpeningsSear

const handleCheckboxChange = vi.fn();
const toggleSpatial = vi.fn();
const setOpeningIds = vi.fn((openingIds: number[]) => {});
const queryClient = new QueryClient();

export const rows:any = [
Expand Down Expand Up @@ -259,6 +260,7 @@ describe('Search Screen Data table test', () => {
handleCheckboxChange={handleCheckboxChange}
toggleSpatial={toggleSpatial}
totalItems={rows.length}
setOpeningIds={setOpeningIds}
/>
</NotificationProvider>
</OpeningsSearchProvider>
Expand Down Expand Up @@ -289,6 +291,7 @@ describe('Search Screen Data table test', () => {
handleCheckboxChange={handleCheckboxChange}
toggleSpatial={toggleSpatial}
totalItems={0}
setOpeningIds={setOpeningIds}
/>
</NotificationProvider>
</OpeningsSearchProvider>
Expand All @@ -303,7 +306,7 @@ describe('Search Screen Data table test', () => {
expect(container.querySelector('.total-search-results')).toContainHTML('0');
});

it('should render the checkbox for showSPatial being true', () => {
it('should render the checkbox for showSpatial being true', () => {
render(
<BrowserRouter>
<QueryClientProvider client={queryClient}>
Expand All @@ -318,6 +321,7 @@ describe('Search Screen Data table test', () => {
handleCheckboxChange={handleCheckboxChange}
toggleSpatial={toggleSpatial}
totalItems={0}
setOpeningIds={setOpeningIds}
/>
</NotificationProvider>
</OpeningsSearchProvider>
Expand All @@ -329,6 +333,40 @@ describe('Search Screen Data table test', () => {
expect(checkbox).toBeInTheDocument();

});

it('should check the checkbox for showSpatial', () => {
render(
<BrowserRouter>
<QueryClientProvider client={queryClient}>
<PaginationProvider>
<OpeningsSearchProvider>
<NotificationProvider>
<SearchScreenDataTable
rows={rows}
headers={columns}
defaultColumns={columns}
showSpatial={true}
handleCheckboxChange={handleCheckboxChange}
toggleSpatial={toggleSpatial}
totalItems={rows.length}
setOpeningIds={setOpeningIds}
/>
</NotificationProvider>
</OpeningsSearchProvider>
</PaginationProvider>
</QueryClientProvider>
</BrowserRouter>
);
const checkboxGroup = document.querySelector('.cds--checkbox-group');
expect(checkboxGroup).toBeInTheDocument();

expect(screen.getByTestId(`checkbox-${rows[0].openingId}`)).toBeInTheDocument();
const checkbox = screen.getByTestId(`checkbox-${rows[0].openingId}`);
fireEvent.click(checkbox);
expect(checkbox).toBeChecked();
expect(setOpeningIds).toHaveBeenCalledWith([parseFloat(rows[0].openingId)]);

});


});
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ const SearchScreenDataTable: React.FC<ISearchScreenDataTable> = ({
<div className="mb-2 mx-2">
<Checkbox
id={`checkbox-label-${row.openingId}`}
data-testid={`checkbox-${row.openingId}`}
labelText=""
checked={selectedRows.includes(
row.openingId
Expand Down

0 comments on commit 1c71d6c

Please sign in to comment.