Skip to content

Commit

Permalink
added test for the recentOpeningData table
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzgrewal committed Nov 5, 2024
1 parent b540db6 commit 7b27b18
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 104 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// src/__test__/components/SilvicultureSearch/Openings/OpeningsSearchBar.test.tsx

import React from "react";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import OpeningsSearchBar from "../../../../components/SilvicultureSearch/Openings/OpeningsSearchBar";
import { vi } from "vitest";
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { OpeningsSearchProvider } from "../../../../contexts/search/OpeningsSearch";
import { Pagination } from "@carbon/react";
import {Provider } from "react-redux";
import PaginationProvider from "../../../../contexts/PaginationProvider";
import RecentOpeningsDataTable from "../../../../components/Dashboard/Opening/RecentOpeningsDataTable";
import { MemoryRouter } from "react-router-dom";

describe("OpeningsSearchBar", () => {
// Create a new QueryClient instance for each test
const queryClient = new QueryClient();
const handleCheckboxChange = vi.fn()
const setLoadId = vi.fn()
const toggleSpatial = vi.fn()
const showSpatial = false
const data = { data: [], perPage: 0, totalPages: 0 }
const headers = []

it("renders the blank table when data array is empty", () => {
render(
<MemoryRouter>
<QueryClientProvider client={queryClient}>
<PaginationProvider>
<RecentOpeningsDataTable
rows={data?.data || []}
headers={headers}
defaultColumns={headers}
handleCheckboxChange={handleCheckboxChange}
setOpeningId={setLoadId}
toggleSpatial={toggleSpatial}
showSpatial={showSpatial}
totalItems={(data?.perPage ?? 0) * (data?.totalPages ?? 0)}
/>
</PaginationProvider>
</QueryClientProvider>
</MemoryRouter>
);
// Check if the search input field is present with the correct placeholder text
const searchInput = screen.getByText(/Total Search Results: 0/i);
expect(searchInput).toBeInTheDocument();
});

it("renders the table with data", () => {
const data = { data: [{
"openingId": 114203,
"forestFileId": "TFL47",
"categoryCode": "FTML",
"categoryDescription": null,
"statusCode": "Active",
"statusDescription": "Active",
"cuttingPermitId": "12T",
"cutBlockId": "12-44A",
"orgUnitName": "DCC - Cariboo chilcotin natural resources",
"updateTimestamp": "2021-12-08"
}], perPage: 1, totalPages: 1 }
const headers = [{
key: 'openingId',
header: 'Opening Id',
selected: false
},
{
key: 'forestFileId',
header: 'File Id',
selected: false
},
{
key: 'cuttingPermit',
header: 'Cutting permit',
selected: false
},
{
key: 'timberMark',
header: 'Timber mark',
selected: false
},
{
key: 'cutBlock',
header: 'Cut block',
selected: false
},]
render(
<MemoryRouter>
<QueryClientProvider client={queryClient}>
<PaginationProvider>
<RecentOpeningsDataTable
rows={data?.data || []}
headers={headers}
defaultColumns={headers}
handleCheckboxChange={handleCheckboxChange}
setOpeningId={setLoadId}
toggleSpatial={toggleSpatial}
showSpatial={showSpatial}
totalItems={(data?.perPage ?? 0) * (data?.totalPages ?? 0)}
/>
</PaginationProvider>
</QueryClientProvider>
</MemoryRouter>
);
console.log(screen.debug())
// Check if the search input field is present with the correct placeholder text
const searchInput = screen.getByText(/Total Search Results: 1/i);
expect(searchInput).toBeInTheDocument();
});
});
38 changes: 38 additions & 0 deletions frontend/src/__test__/utils/DateUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, it, expect } from 'vitest';
import { formatDate, dateStringToISO } from '../../utils/DateUtils';

describe('formatDate', () => {
it('should format a valid date string to "Month Day, Year"', () => {
const result = formatDate('2023-11-05');
expect(result).toBe('November 5, 2023');
});

it('should return "--" for an empty string', () => {
const result = formatDate('');
expect(result).toBe('--');
});

it('should return "--" for a null value', () => {
// @ts-ignore
const result = formatDate(null);
expect(result).toBe('--');
});
});

describe('dateStringToISO', () => {
it('should convert a date string to ISO format', () => {
const result = dateStringToISO('2023-11-05');
expect(result).toBe('2023-11-05T00:00:00.000Z');
});

it('should return an empty string for an invalid date', () => {
const result = dateStringToISO('');
expect(result).toBe('');
});

it('should return an empty string for a null value', () => {
// @ts-ignore
const result = dateStringToISO(null);
expect(result).toBe('');
});
});
8 changes: 0 additions & 8 deletions frontend/src/utils/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,3 @@ export const dateStringToISO = (date: string): string => {
}
return '';
};

export const formatDateToString = (dateToFormat: Date) => {
if (!dateToFormat) return null;
const year = dateToFormat.getFullYear();
const month = String(dateToFormat.getMonth() + 1).padStart(2, "0");
const day = String(dateToFormat.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
};

0 comments on commit 7b27b18

Please sign in to comment.