diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningEndpointTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningEndpointTest.java deleted file mode 100644 index f0e4b06a..00000000 --- a/backend/src/test/java/ca/bc/gov/restapi/results/oracle/endpoint/OpeningEndpointTest.java +++ /dev/null @@ -1,96 +0,0 @@ -package ca.bc.gov.restapi.results.oracle.endpoint; - -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import ca.bc.gov.restapi.results.common.pagination.PaginatedResult; -import ca.bc.gov.restapi.results.common.pagination.PaginationParameters; -import ca.bc.gov.restapi.results.oracle.dto.RecentOpeningDto; -import ca.bc.gov.restapi.results.oracle.enums.OpeningCategoryEnum; -import ca.bc.gov.restapi.results.oracle.enums.OpeningStatusEnum; -import ca.bc.gov.restapi.results.oracle.service.OpeningService; -import java.math.BigDecimal; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.web.servlet.MockMvc; - -@WebMvcTest(OpeningEndpoint.class) -@WithMockUser(roles = "user_read") -class OpeningEndpointTest { - - @Autowired private MockMvc mockMvc; - - @MockBean private OpeningService openingService; - - @Test - @DisplayName("Request a list of recent openings for the home screen") - void getRecentOpenings_fetchPaginated_shouldSucceed() throws Exception { - PaginatedResult paginatedResult = new PaginatedResult<>(); - paginatedResult.setPageIndex(0); - paginatedResult.setPerPage(5); - paginatedResult.setTotalPages(1); - paginatedResult.setHasNextPage(false); - - LocalDate now = LocalDate.now(); - LocalDateTime entryLastYear = LocalDateTime.now().minusYears(1L); - LocalDateTime updateLastMonth = entryLastYear.plusMonths(11L); - - RecentOpeningDto recentOpeningDto = - new RecentOpeningDto( - 114207L, - "TFL47", - "12T", - "47/12S", - "12-69", - new BigDecimal("12.9"), - OpeningStatusEnum.APP, - OpeningCategoryEnum.FTML, - now, - entryLastYear, - updateLastMonth); - paginatedResult.setData(List.of(recentOpeningDto)); - - PaginationParameters params = new PaginationParameters(0, 5); - - - mockMvc - .perform( - get("/api/openings/recent-openings") - .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType("application/json")) - .andExpect(header().exists("x-opening-source")) - .andExpect(jsonPath("$.pageIndex").value("0")) - .andExpect(jsonPath("$.perPage").value("5")) - .andExpect(jsonPath("$.totalPages").value("1")) - .andExpect(jsonPath("$.hasNextPage").value("false")) - .andExpect(jsonPath("$.data[0].openingId").value("114207")) - .andExpect(jsonPath("$.data[0].fileId").value("TFL47")) - .andExpect(jsonPath("$.data[0].cuttingPermit").value("12T")) - .andExpect(jsonPath("$.data[0].timberMark").value("47/12S")) - .andExpect(jsonPath("$.data[0].cutBlock").value("12-69")) - .andExpect(jsonPath("$.data[0].grossAreaHa").value("12.9")) - .andExpect(jsonPath("$.data[0].status.code").value(OpeningStatusEnum.APP.getCode())) - .andExpect( - jsonPath("$.data[0].status.description").value(OpeningStatusEnum.APP.getDescription())) - .andExpect(jsonPath("$.data[0].category.code").value(OpeningCategoryEnum.FTML.getCode())) - .andExpect( - jsonPath("$.data[0].category.description") - .value(OpeningCategoryEnum.FTML.getDescription())) - .andExpect(jsonPath("$.data[0].disturbanceStart").value(now.toString())) - .andReturn(); - } -} diff --git a/frontend/src/__test__/components/Dashboard/Opening/RecentOpeningsDataTable.test.tsx b/frontend/src/__test__/components/Dashboard/Opening/RecentOpeningsDataTable.test.tsx new file mode 100644 index 00000000..d4b10571 --- /dev/null +++ b/frontend/src/__test__/components/Dashboard/Opening/RecentOpeningsDataTable.test.tsx @@ -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( + + + + + + + + ); + // 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( + + + + + + + + ); + 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(); + }); +}); \ No newline at end of file diff --git a/frontend/src/__test__/utils/DateUtils.test.ts b/frontend/src/__test__/utils/DateUtils.test.ts new file mode 100644 index 00000000..f5e8acd3 --- /dev/null +++ b/frontend/src/__test__/utils/DateUtils.test.ts @@ -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(''); + }); +}); diff --git a/frontend/src/utils/DateUtils.ts b/frontend/src/utils/DateUtils.ts index 44b66e3d..a5b416ce 100644 --- a/frontend/src/utils/DateUtils.ts +++ b/frontend/src/utils/DateUtils.ts @@ -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}`; -};