generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/SILVA-449
- Loading branch information
Showing
8 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
import FriendlyDate from '../../components/FriendlyDate'; | ||
|
||
// 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 }; | ||
}); | ||
|
||
// Mock `Date.now` for consistent testing | ||
beforeAll(() => { | ||
vi.useFakeTimers(); | ||
vi.setSystemTime(new Date('2024-01-24T00:00:00')); // Set a fixed date for testing | ||
}); | ||
|
||
afterAll(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
describe('FriendlyDate Component', () => { | ||
|
||
it('displays "Today" for today\'s date', () => { | ||
render(<FriendlyDate date="2024-01-24T06:23:12" />); | ||
expect(screen.getByText("Today")).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays "Yesterday" for a date one day ago', () => { | ||
render(<FriendlyDate date="2024-01-23T00:00:00" />); | ||
expect(screen.getByText("Yesterday")).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays relative time within the last week', () => { | ||
render(<FriendlyDate date="2024-01-21T00:00:00" />); | ||
expect(screen.getByText("3 days ago")).toBeInTheDocument(); | ||
}); | ||
|
||
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(); | ||
}); | ||
|
||
it('displays friendly date format for future dates', () => { | ||
render(<FriendlyDate date="2024-02-22T00:00:00" />); | ||
expect(screen.getByText("in 29 days")).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"); | ||
}); | ||
|
||
it('renders an empty span for null dates', () => { | ||
const {getByTestId} = render(<FriendlyDate date={null} />); | ||
expect(getByTestId("friendly-date")).toBeInTheDocument(); | ||
|
||
}); | ||
|
||
it('renders an empty span for undefined dates', () => { | ||
const {getByTestId} = render(<FriendlyDate date={undefined} />); | ||
expect(getByTestId("friendly-date")).toBeInTheDocument(); | ||
|
||
}); | ||
|
||
it('renders an empty span for invalid', () => { | ||
const {getByTestId} = render(<FriendlyDate date="invalid-date" />); | ||
expect(getByTestId("friendly-date")).toBeInTheDocument(); | ||
|
||
}); | ||
|
||
it('renders an empty span for empty', () => { | ||
const {getByTestId} = render(<FriendlyDate date="" />); | ||
expect(getByTestId("friendly-date")).toBeInTheDocument(); | ||
|
||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import { formatDistanceToNow, format, parseISO, isToday, isYesterday } from 'date-fns'; | ||
import { Tooltip } from '@carbon/react'; | ||
|
||
interface FriendlyDateProps { | ||
date: string | null | undefined; // The date string in ISO format | ||
} | ||
|
||
const FriendlyDate: React.FC<FriendlyDateProps> = ({ date }) => { | ||
|
||
if(!date) return <span data-testid="friendly-date"></span>; | ||
|
||
try{ | ||
const parsedDate = parseISO(date); | ||
const cleanDate = format(parsedDate, "MMM dd, yyyy"); | ||
|
||
const formattedDate = isToday(parsedDate) | ||
? "Today" | ||
: isYesterday(parsedDate) | ||
? "Yesterday" | ||
: formatDistanceToNow(parsedDate, { addSuffix: true }); | ||
|
||
return <Tooltip | ||
label={cleanDate} | ||
align="bottom-left" | ||
autoAlign> | ||
<span>{formattedDate}</span> | ||
</Tooltip>; | ||
} catch(e){ | ||
return <span data-testid="friendly-date"></span>; | ||
} | ||
}; | ||
|
||
export default FriendlyDate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters