Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SILVA-449): truncating text based on parent #456

Merged
merged 9 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ca.bc.gov.restapi.results.oracle.dto;

import lombok.With;

@With
public record CodeDescriptionDto(
String code,
String description
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import ca.bc.gov.restapi.results.common.pagination.PaginatedResult;
import ca.bc.gov.restapi.results.common.pagination.PaginatedViaQuery;
import ca.bc.gov.restapi.results.common.pagination.PaginationParameters;
import ca.bc.gov.restapi.results.oracle.dto.CodeDescriptionDto;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto;
import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity;
import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity;
import ca.bc.gov.restapi.results.oracle.service.OpenCategoryCodeService;
import ca.bc.gov.restapi.results.oracle.service.OpeningService;
Expand Down Expand Up @@ -121,10 +121,10 @@ public PaginatedResult<OpeningSearchResponseDto> openingSearch(
* Get all opening categories. Optionally you can ask for the expired ones.
*
* @param includeExpired Query param to include expired categories.
* @return List of OpenCategoryCodeEntity with found categories.
* @return List of {@link CodeDescriptionDto} with found categories.
*/
@GetMapping("/categories")
public List<OpenCategoryCodeEntity> getOpeningCategories(
public List<CodeDescriptionDto> getOpeningCategories(
@RequestParam(value = "includeExpired", required = false)
Boolean includeExpired) {
boolean addExpired = Boolean.TRUE.equals(includeExpired);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.restapi.results.oracle.service;

import ca.bc.gov.restapi.results.oracle.dto.CodeDescriptionDto;
import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity;
import ca.bc.gov.restapi.results.oracle.repository.OpenCategoryCodeRepository;
import java.time.LocalDate;
Expand All @@ -21,9 +22,9 @@ public class OpenCategoryCodeService {
* Find all Opening categories. Option to include expired ones.
*
* @param includeExpired True to include expired, false otherwise.
* @return List of {@link OpenCategoryCodeEntity} with found categories.
* @return List of {@link CodeDescriptionDto} with found categories.
*/
public List<OpenCategoryCodeEntity> findAllCategories(boolean includeExpired) {
public List<CodeDescriptionDto> findAllCategories(boolean includeExpired) {
log.info("Getting all open category codes. Include expired: {}", includeExpired);

List<OpenCategoryCodeEntity> openCategoryCodes =
Expand All @@ -35,6 +36,9 @@ public List<OpenCategoryCodeEntity> findAllCategories(boolean includeExpired) {
openCategoryCodes.size(),
BooleanUtils.toString(includeExpired, "in", "ex")
);
return openCategoryCodes;
return openCategoryCodes
.stream()
.map(entity -> new CodeDescriptionDto(entity.getCode(), entity.getDescription()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
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.oracle.dto.CodeDescriptionDto;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto;
import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity;
import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity;
import ca.bc.gov.restapi.results.oracle.enums.OpeningCategoryEnum;
import ca.bc.gov.restapi.results.oracle.enums.OpeningStatusEnum;
Expand Down Expand Up @@ -145,19 +145,9 @@ void openingSearch_noRecordsFound_shouldSucceed() throws Exception {
@Test
@DisplayName("Get Opening Categories happy Path should Succeed")
void getOpeningCategories_happyPath_shouldSucceed() throws Exception {
OpenCategoryCodeEntity category = new OpenCategoryCodeEntity();
category.setCode("FTML");
category.setDescription("Free Growing");
category.setEffectiveDate(LocalDate.now().minusYears(3L));
category.setExpiryDate(LocalDate.now().plusYears(3L));
category.setUpdateTimestamp(LocalDate.now());
CodeDescriptionDto category = new CodeDescriptionDto("FTML", "Free Growing");

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String effectiveDateStr = category.getEffectiveDate().format(formatter);
String expiryDateStr = category.getExpiryDate().format(formatter);
String updateTimestampStr = category.getUpdateTimestamp().format(formatter);

List<OpenCategoryCodeEntity> openCategoryCodeEntityList = List.of(category);
List<CodeDescriptionDto> openCategoryCodeEntityList = List.of(category);

when(openCategoryCodeService.findAllCategories(false)).thenReturn(openCategoryCodeEntityList);

Expand All @@ -168,11 +158,8 @@ void getOpeningCategories_happyPath_shouldSucceed() throws Exception {
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$[0].code").value(category.getCode()))
.andExpect(jsonPath("$[0].description").value(category.getDescription()))
.andExpect(jsonPath("$[0].effectiveDate").value(effectiveDateStr))
.andExpect(jsonPath("$[0].expiryDate").value(expiryDateStr))
.andExpect(jsonPath("$[0].updateTimestamp").value(updateTimestampStr))
.andExpect(jsonPath("$[0].code").value(category.code()))
.andExpect(jsonPath("$[0].description").value(category.description()))
.andReturn();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ca.bc.gov.restapi.results.oracle.service;

import ca.bc.gov.restapi.results.extensions.AbstractTestContainerIntegrationTest;
import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity;
import java.time.LocalDate;
import ca.bc.gov.restapi.results.oracle.dto.CodeDescriptionDto;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -19,20 +18,18 @@ class OpenCategoryCodeServiceTest extends AbstractTestContainerIntegrationTest {
@DisplayName("Find all categories include expired false should succeed")
void findAllCategories_includeExpiredFalse_shouldSucceed() {

List<OpenCategoryCodeEntity> entities = openCategoryCodeService.findAllCategories(false);
List<CodeDescriptionDto> entities = openCategoryCodeService.findAllCategories(false);

Assertions.assertNotNull(entities);
Assertions.assertEquals(22, entities.size());
Assertions.assertTrue(entities.get(0).getExpiryDate().isAfter(LocalDate.now()));
}

@Test
@DisplayName("Find all categories include expired true should succeed")
void findAllCategories_includeExpiredTrue_shouldSucceed() {
List<OpenCategoryCodeEntity> entities = openCategoryCodeService.findAllCategories(true);
List<CodeDescriptionDto> entities = openCategoryCodeService.findAllCategories(true);

Assertions.assertNotNull(entities);
Assertions.assertEquals(39, entities.size());
Assertions.assertTrue(entities.get(0).getExpiryDate().isBefore(LocalDate.now()));
}
}
12 changes: 6 additions & 6 deletions frontend/src/__test__/components/FriendlyDate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ describe('FriendlyDate Component', () => {

it('displays "Today" for today\'s date', () => {
render(<FriendlyDate date="2024-01-24T06:23:12" />);
expect(screen.getByText("Today")).toBeInTheDocument();
expect(screen.getByText("in 6 hours")).toBeInTheDocument();
});

it('displays "Yesterday" for a date one day ago', () => {
render(<FriendlyDate date="2024-01-23T00:00:00" />);
expect(screen.getByText("Yesterday")).toBeInTheDocument();
expect(screen.getByText("yesterday")).toBeInTheDocument();
});

it('displays relative time within the last week', () => {
Expand All @@ -38,17 +38,17 @@ describe('FriendlyDate Component', () => {

it('displays exact date for dates older than a week', () => {
render(<FriendlyDate date="2024-01-01T00:00:00" />);
expect(screen.getByText("23 days ago")).toBeInTheDocument();
expect(screen.getByText("January 1, 2024")).toBeInTheDocument();
});

it('displays friendly date format for future dates', () => {
render(<FriendlyDate date="2024-02-22T00:00:00" />);
expect(screen.getByText("in 29 days")).toBeInTheDocument();
expect(screen.getByText("February 22, 2024")).toBeInTheDocument();
});

it('renders tooltip with full text on hover', async () => {
const {container} = render(<FriendlyDate date="2024-02-22T00:00:00" />);
expect(container.querySelector('span').getAttribute('data-tooltip')).toBe("Feb 22, 2024");
const {container} = render(<FriendlyDate date="2024-01-21T00:00:00" />);
expect(container.querySelector('span').getAttribute('data-tooltip')).toBe("January 21, 2024");
});

it('renders an empty span for null dates', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ describe("OpeningsSearchBar", () => {
</QueryClientProvider>
);

console.log(screen.debug());

// Check if an element with the class 'd-none' exists within the structure
const dNoneElement = screen.getByText('+2');
expect(dNoneElement).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react';
import { render } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import SearchScreenDataTable from '../../../../components/SilvicultureSearch/Openings/SearchScreenDataTable/index';
import { columns, rows } from '../../../../components/SilvicultureSearch/Openings/SearchScreenDataTable/testData';
import PaginationProvider from '../../../../contexts/PaginationProvider';
import { NotificationProvider } from '../../../../contexts/NotificationProvider';
import { BrowserRouter } from 'react-router-dom';
import { OpeningsSearchProvider } from '../../../../contexts/search/OpeningsSearch';

const handleCheckboxChange = vi.fn();
const toggleSpatial = vi.fn();

describe('Search Screen Data table test', () => {

it('should render the Search Screen Data table', () => {
const { getByText, container } =
render(
<BrowserRouter>
<PaginationProvider>
<OpeningsSearchProvider>
<NotificationProvider>
<SearchScreenDataTable
rows={rows}
headers={columns}
defaultColumns={columns}
showSpatial={false}
handleCheckboxChange={handleCheckboxChange}
toggleSpatial={toggleSpatial}
totalItems={rows.length}
/>
</NotificationProvider>
</OpeningsSearchProvider>
</PaginationProvider>
</BrowserRouter>
);

expect(container).toBeInTheDocument();
expect(container.querySelector('.total-search-results')).toBeInTheDocument();
expect(container.querySelector('.total-search-results')).toContainHTML('Total Search Results');

});

it('should render the Search Screen Data table with no data', () => {
const { getByText, container } =
render(
<BrowserRouter>
<PaginationProvider>
<OpeningsSearchProvider>
<NotificationProvider>
<SearchScreenDataTable
rows={[]}
headers={columns}
defaultColumns={columns}
showSpatial={false}
handleCheckboxChange={handleCheckboxChange}
toggleSpatial={toggleSpatial}
totalItems={0}
/>
</NotificationProvider>
</OpeningsSearchProvider>
</PaginationProvider>
</BrowserRouter>
);

expect(container).toBeInTheDocument();
expect(container.querySelector('.total-search-results')).toBeInTheDocument();
expect(container.querySelector('.total-search-results')).toContainHTML('Total Search Results');
expect(container.querySelector('.total-search-results')).toContainHTML('0');
});

});
40 changes: 40 additions & 0 deletions frontend/src/__test__/components/TruncatedText.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';
import TruncatedText from '../../components/TruncatedText';

// Mock Tooltip component from Carbon to ensure tests run without extra dependencies
vi.mock('@carbon/react', () => {
const Tooltip = ({ label, children }) => <span data-tooltip={label}>{children}</span>;
return { Tooltip };
});

describe('TruncatedText Component', () => {

it('renders the component with default props', () => {
render(<TruncatedText text="This is a test text that may be truncated" />);
expect(screen.getByText(/This is a test text/)).toBeInTheDocument();
});

it('truncates text based on maxLength', () => {
render(<TruncatedText text="This is a test text that should be truncated" maxLength={10} />);
expect(screen.getByText("This is a ...")).toBeInTheDocument();
});

it('does not truncate text if length is within maxLength', () => {
render(<TruncatedText text="Short text" maxLength={20} />);
expect(screen.getByText("Short text")).toBeInTheDocument();
});

it('truncates text based on parentWidth greater than 200px', () => {
const text = "This is a test text that should be truncated".repeat(10);
render(<TruncatedText text={text} parentWidth={300} />);
expect(screen.getByText("This is a test text that should be truncated...")).toBeInTheDocument(); // 300/5 = 60 chars
});

it('renders tooltip with full text on hover', async () => {
const {container} = render(<TruncatedText text="This is a test text that should be truncated" maxLength={10} />);
expect(container.querySelector('span').getAttribute('data-tooltip')).toBe("This is a test text that should be truncated");
});

});
Loading
Loading