diff --git a/apps/explorer/src/app/components/seconds-ago/index.tsx b/apps/explorer/src/app/components/seconds-ago/index.tsx index 6d68ba550..2a0fc7639 100644 --- a/apps/explorer/src/app/components/seconds-ago/index.tsx +++ b/apps/explorer/src/app/components/seconds-ago/index.tsx @@ -4,7 +4,7 @@ interface SecondsAgoProps { date: string | undefined; } -export const SecondsAgo = ({ date }: SecondsAgoProps) => { +export const SecondsAgo = ({ date, ...props }: SecondsAgoProps) => { const [now, setNow] = useState(Date.now()); useEffect(() => { @@ -18,10 +18,16 @@ export const SecondsAgo = ({ date }: SecondsAgoProps) => { return <>Date unknown; } + console.log( + `now: ${now}, before: ${new Date( + date + ).getTime()}, date getting passed in: ${date}` + ); + const timeAgoInSeconds = Math.floor((now - new Date(date).getTime()) / 1000); return ( -
+
{timeAgoInSeconds === 1 ? '1 second' : `${timeAgoInSeconds} seconds`} ago
); diff --git a/apps/explorer/src/app/components/seconds-ago/seconds-ago.spec.tsx b/apps/explorer/src/app/components/seconds-ago/seconds-ago.spec.tsx index ac07437f5..a8cd33cb8 100644 --- a/apps/explorer/src/app/components/seconds-ago/seconds-ago.spec.tsx +++ b/apps/explorer/src/app/components/seconds-ago/seconds-ago.spec.tsx @@ -1,24 +1,37 @@ -import { render, screen } from '@testing-library/react'; - +import { render, screen, act } from '@testing-library/react'; import { SecondsAgo } from './index'; +beforeEach(() => { + jest.useFakeTimers(); +}); + +afterEach(() => { + jest.useRealTimers(); +}); + describe('Seconds ago', () => { it('should render successfully', () => { - const dateInString = Date.now().toString(); - const { baseElement } = render(); - expect(baseElement).toBeTruthy(); + const dateInString = new Date().toString(); + render(); + + expect(screen.getByTestId('test-seconds-ago')).toBeInTheDocument(); }); - it('should show the correct amount of seconds ago', async () => { - const secondsToWait = 2; + it('should show the correct amount of seconds ago', (done) => { + const secondsToWait = 10; const dateInString = new Date().toString(); - await new Promise((r) => setTimeout(r, secondsToWait * 1000)); + act(() => { + jest.advanceTimersByTime(secondsToWait * 1000); + }); - render(); + jest.runOnlyPendingTimers(); + done(); - expect( - screen.getByText(`${secondsToWait} seconds ago`) - ).toBeInTheDocument(); + render(); + + expect(screen.getByTestId('test-seconds-ago')).toHaveTextContent( + `${secondsToWait} seconds ago` + ); }); }); diff --git a/apps/explorer/src/app/components/status-message/index.tsx b/apps/explorer/src/app/components/status-message/index.tsx index 10bc441cc..ae8958270 100644 --- a/apps/explorer/src/app/components/status-message/index.tsx +++ b/apps/explorer/src/app/components/status-message/index.tsx @@ -6,7 +6,15 @@ interface StatusMessageProps { className?: string; } -export const StatusMessage = ({ children, className }: StatusMessageProps) => { +export const StatusMessage = ({ + children, + className, + ...props +}: StatusMessageProps) => { const classes = classnames('font-alpha text-h4 mb-28', className); - return

{children}

; + return ( +

+ {children} +

+ ); }; diff --git a/apps/explorer/src/app/components/status-message/status-message.spec.tsx b/apps/explorer/src/app/components/status-message/status-message.spec.tsx index 77845f645..541b10fbe 100644 --- a/apps/explorer/src/app/components/status-message/status-message.spec.tsx +++ b/apps/explorer/src/app/components/status-message/status-message.spec.tsx @@ -1,10 +1,11 @@ -import { render } from '@testing-library/react'; - +import { render, screen } from '@testing-library/react'; import { StatusMessage } from './index'; describe('Status message', () => { it('should render successfully', () => { - const { baseElement } = render(test); - expect(baseElement).toBeTruthy(); + render( + test + ); + expect(screen.getByTestId('status-message-test')).toBeInTheDocument(); }); }); diff --git a/apps/explorer/src/app/components/table/table.spec.tsx b/apps/explorer/src/app/components/table/table.spec.tsx index 14467a334..9ba2e5bd8 100644 --- a/apps/explorer/src/app/components/table/table.spec.tsx +++ b/apps/explorer/src/app/components/table/table.spec.tsx @@ -1,9 +1,9 @@ -import { render } from '@testing-library/react'; +import { render, screen } from '@testing-library/react'; import { Table, TableRow, TableHeader, TableCell } from './index'; describe('Renders all table components', () => { - const { container } = render( + render( Title @@ -12,110 +12,68 @@ describe('Renders all table components', () => {
); - const table = container.querySelector('[data-testid="test-table"]'); - const tr = container.querySelector('[data-testid="test-tr"]'); - const th = container.querySelector('[data-testid="test-th"]'); - const td = container.querySelector('[data-testid="test-td"]'); - - expect(table).toBeInTheDocument(); - expect(th).toBeInTheDocument(); - expect(tr).toBeInTheDocument(); - expect(td).toBeInTheDocument(); + expect(screen.getByTestId('test-table')).toBeInTheDocument(); + expect(screen.getByTestId('test-tr')).toBeInTheDocument(); + expect(screen.getByTestId('test-th')).toHaveTextContent('Title'); + expect(screen.getByTestId('test-td')).toHaveTextContent('Content'); }); describe('Table row', () => { it('should include classes based on custom "modifier" prop', () => { - const { baseElement: withoutModifier } = render( + render( - - Without modifier - -
- ); - - const { baseElement: withModifier } = render( - - + With modifier
); - const noModifierTr = withoutModifier.querySelector('tr'); - const modifierTr = withModifier.querySelector('tr'); - const classNameToCheck = 'border-white-40'; - - expect(noModifierTr && !noModifierTr.classList.contains(classNameToCheck)); - expect(modifierTr && modifierTr.classList.contains(classNameToCheck)); + expect(screen.getByTestId('modifier-test')).toHaveClass('border-white-40'); }); }); describe('Table header', () => { it('should accept props i.e. scope="row"', () => { - const { baseElement } = render( + render( - Test + + Test +
); - const th = baseElement.querySelector('th'); - - expect(th && th.hasAttribute('scope')); + expect(screen.getByTestId('props-test')).toHaveAttribute('scope'); }); it('should include custom class based on scope="row"', () => { - const { baseElement: withoutScope } = render( + render( - Without scope attribute + + With scope attribute +
); - const { baseElement: withScope } = render( - - - With scope attribute - -
- ); - - const withoutScopeTr = withoutScope.querySelector('tr'); - const withScopeTr = withScope.querySelector('tr'); - const classNameToCheck = 'text-left'; - - expect( - withoutScopeTr && !withoutScopeTr.classList.contains(classNameToCheck) - ); - expect(withScopeTr && withScopeTr.classList.contains(classNameToCheck)); + expect(screen.getByTestId('scope-class-test')).toHaveClass('text-left'); }); }); describe('Table cell', () => { it('should include class based on custom "modifier" prop', () => { - const { baseElement: withoutModifier } = render( + render( - Without modifier + + With modifier +
); - const { baseElement: withModifier } = render( - - - With modifier - -
- ); - - const noModifierTd = withoutModifier.querySelector('td'); - const modifierTd = withModifier.querySelector('td'); - const classNameToCheck = 'py-4'; - - expect(noModifierTd && !noModifierTd.classList.contains(classNameToCheck)); - expect(modifierTd && modifierTd.classList.contains(classNameToCheck)); + expect(screen.getByTestId('modifier-class-test')).toHaveClass('py-4'); }); }); diff --git a/apps/explorer/src/app/components/truncate/truncate.spec.tsx b/apps/explorer/src/app/components/truncate/truncate.spec.tsx index 875ab8317..85677d8be 100644 --- a/apps/explorer/src/app/components/truncate/truncate.spec.tsx +++ b/apps/explorer/src/app/components/truncate/truncate.spec.tsx @@ -3,11 +3,11 @@ import { TruncateInline } from './truncate'; describe('Truncate', () => { it('should render successfully', () => { - const { baseElement } = render( - + render( + ); - expect(baseElement).toBeTruthy(); + expect(screen.getByTestId('truncate-test')).toBeInTheDocument(); }); it('it truncates as expected', () => { diff --git a/apps/explorer/src/app/components/truncate/truncate.tsx b/apps/explorer/src/app/components/truncate/truncate.tsx index 44cf2e2ba..c478c8fe4 100644 --- a/apps/explorer/src/app/components/truncate/truncate.tsx +++ b/apps/explorer/src/app/components/truncate/truncate.tsx @@ -22,6 +22,7 @@ export function TruncateInline({ children, startChars, endChars, + ...props }: TruncateInlineProps) { if (text === null) { return ; @@ -31,6 +32,7 @@ export function TruncateInline({ const wrapperProps = { title: text, className, + ...props, }; if (children !== undefined) {