import * as React from 'react'; import { render, screen, getDefaultNormalizer } from '@testing-library/react'; import { Timestamp, TimestampFormat, TimestampTooltipVariant } from '../Timestamp'; jest.mock('../../Tooltip', () => ({ Tooltip: ({ content, children, ...props }) => (
{content}
{children}
) })); xtest('Matches snapshot', () => { /** Due to how the datetime attribute is contstructed internally, dateTime must be * manually passed for this snapshot to pass on GitHub. */ const { asFragment } = render(); expect(asFragment()).toMatchSnapshot(); }); test('Renders component', () => { render(
); expect(screen.getByTestId('timestamp-container').firstChild).toBeVisible(); }); test('Renders with current date by default with default formatting', () => { render(); expect( screen.getByText(new Date().toLocaleString(), { normalizer: getDefaultNormalizer({ collapseWhitespace: false }) }) ).toBeInTheDocument(); }); test('Renders with correct datetime attribute with current date by default', () => { render(); // Because there could be a .001 ms difference in the expected and received datetime value, // we want an ISO value without the ms to expect as the datetime value. const isoDateWithoutMS = new Date().toISOString().split('.')[0]; expect( screen.getByText(new Date().toLocaleString(), { normalizer: getDefaultNormalizer({ collapseWhitespace: false }) }) ).toHaveAttribute('datetime', expect.stringMatching(isoDateWithoutMS)); }); test('Renders passed in date with default formatting', () => { render(); expect(screen.getByText('1/1/2022, 12:00:00 AM')).toBeInTheDocument(); }); test('Renders with correct datetime attribute when date is passed in', () => { const passedDate = new Date(2022, 0, 1); render(); expect(screen.getByText('1/1/2022, 12:00:00 AM')).toHaveAttribute('datetime', passedDate.toISOString()); }); test('Renders with custom formatting when dateFormat and timeFormat are passed in', () => { render( ); expect(screen.getByText('Saturday, January 1, 2022 at 12:00 AM')).toBeInTheDocument(); }); test('Renders with only date when dateFormat is passed in', () => { render(); expect(screen.getByText('Saturday, January 1, 2022')).toBeInTheDocument(); }); test('Renders with only time when timeFormat is passed in', () => { render(); expect(screen.getByText('12:00 AM')).toBeInTheDocument(); }); test('Renders with custom formatting when customFormat is passed in', () => { render( ); expect(screen.getByText('Sat, Jan 1, 22, 12 AM')).toBeInTheDocument(); }); test('Renders with custom content', () => { render(2 days ago); expect(screen.getByText('2 days ago')).toBeInTheDocument(); }); test('Renders with a custom suffix when displaySuffix is passed in', () => { render(); expect(screen.getByText('1/1/2022, 12:00:00 AM US Eastern')).toBeInTheDocument(); }); test('Renders with 12 hour time when is12Hour is passed', () => { render(); expect(screen.getByText('1/1/2022, 1:00:00 PM')).toBeInTheDocument(); }); test('Renders with 24 hour time when is12Hour is set to false', () => { render(); expect(screen.getByText('1/1/2022, 13:00:00')).toBeInTheDocument(); }); test('Renders with locale passed in', () => { render(); expect(screen.getByText('01/02/2022, 00:00:00')).toBeInTheDocument(); }); test('Renders with 12 hour time by default for US locale', () => { render(); expect(screen.getByText('2/1/2022, 1:00:00 PM')).toBeInTheDocument(); }); test('Renders with 24 hour time for US locale when is12Hour is false', () => { render(); expect(screen.getByText('2/1/2022, 13:00:00')).toBeInTheDocument(); }); test('Renders with 12 hour time for a 24 hour locale when is12Hour is passed', () => { render(); expect(screen.getByText('01/02/2022, 1:00:00 pm')).toBeInTheDocument(); }); test('Renders with pf-v5-c-timestamp by default', () => { render(); expect(screen.getByText('1/1/2022, 12:00:00 AM').parentElement).toHaveClass('pf-v5-c-timestamp'); }); test('Renders with pf-v5-c-timestamp__text by default', () => { render(); expect(screen.getByText('1/1/2022, 12:00:00 AM')).toHaveClass('pf-v5-c-timestamp__text'); }); test('Renders with custom class names', () => { render(); expect(screen.getByText('1/1/2022, 12:00:00 AM').parentElement).toHaveClass('custom-time-class'); }); test('Renders with pf-m-help-text class when tooltip is passed in with default variant', () => { render(); expect(screen.getByText('1/1/2022, 12:00:00 AM').parentElement).toHaveClass('pf-m-help-text'); }); test('Renders with pf-m-help-text class when tooltip is passed in with custom variant', () => { render(); expect(screen.getByText('1/1/2022, 12:00:00 AM').parentElement).toHaveClass('pf-m-help-text'); }); test('Renders with default tooltip content for default variant', () => { render(); expect(screen.getByText('1/1/2022, 5:00:00 AM UTC')).toBeInTheDocument(); }); test('Renders with custom tooltip suffix for default variant', () => { render( ); expect(screen.getByText('1/1/2022, 5:00:00 AM Coordinated Universal Time')).toBeInTheDocument(); }); test('Renders with custom tooltip content', () => { render(); expect(screen.getByText('Custom tooltip content')).toBeInTheDocument(); });