From 83ca62b10877b52371bf4e105affee38c465a303 Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Sat, 16 Sep 2023 12:35:28 +0800 Subject: [PATCH] first round of unit tests --- .../ExpandableSection.test.tsx | 14 ++ .../PseudoActionButton.test.tsx | 43 ++++++ .../PseudoActionButton/PseudoActionButton.tsx | 17 ++- .../AvatarWithIdentifier.test.tsx | 62 +++++++++ .../AvatarWithIdentifier.tsx | 16 +-- .../QuestionTooltip/QuestionTooltip.test.tsx | 19 +++ .../QuestionTooltip/QuestionTooltip.tsx | 2 +- .../hooks/useOwnershipWarning.test.tsx | 127 ++++++++++++++++++ .../hooks/useOwnershipWarning.tsx | 11 +- .../ContractSection/ContractSection.test.tsx | 29 ++++ .../ContractSection/ContractSection.tsx | 13 +- .../components/ExpiryPanel.test.tsx | 14 ++ .../ExpirySection/components/ExpiryPanel.tsx | 12 +- .../ExpirySection/hooks/useExpiryActions.tsx | 6 +- .../ExpirySection/hooks/useExpiryDetails.ts | 7 +- .../RolesSection/hooks/useRoleActions.tsx | 4 +- .../useRoles/utils/getAvailableRoles.test.ts | 2 +- .../components/SearchViewResult.tsx | 7 +- src/utils/date.test.ts | 21 +++ src/utils/date.ts | 1 + src/utils/name.test.ts | 33 +++++ src/utils/name.ts | 5 + 22 files changed, 429 insertions(+), 36 deletions(-) create mode 100644 src/components/@atoms/ExpandableSection/ExpandableSection.test.tsx create mode 100644 src/components/@atoms/PseudoActionButton/PseudoActionButton.test.tsx create mode 100644 src/components/@molecules/AvatarWithIdentifier/AvatarWithIdentifier.test.tsx create mode 100644 src/components/@molecules/QuestionTooltip/QuestionTooltip.test.tsx create mode 100644 src/components/pages/profile/[name]/tabs/OwnershipTab/hooks/useOwnershipWarning.test.tsx create mode 100644 src/components/pages/profile/[name]/tabs/OwnershipTab/sections/ContractSection/ContractSection.test.tsx create mode 100644 src/components/pages/profile/[name]/tabs/OwnershipTab/sections/ExpirySection/components/ExpiryPanel.test.tsx create mode 100644 src/utils/date.test.ts create mode 100644 src/utils/name.test.ts diff --git a/src/components/@atoms/ExpandableSection/ExpandableSection.test.tsx b/src/components/@atoms/ExpandableSection/ExpandableSection.test.tsx new file mode 100644 index 000000000..bd4dd4910 --- /dev/null +++ b/src/components/@atoms/ExpandableSection/ExpandableSection.test.tsx @@ -0,0 +1,14 @@ +import { render, screen, userEvent} from '@app/test-utils' +import { ExpandableSection } from './ExpandableSection' + +describe('ExpandableSection', () => { + it('should expand and close when header is clicked', async () => { + render(CONTENT) + const header = screen.getByRole('button', { name: /Test/i }) + expect(screen.getByText('CONTENT')).not.toBeVisible() + await userEvent.click(header) + expect(screen.getByText('CONTENT')).toBeVisible() + await userEvent.click(header) + expect(screen.getByText('CONTENT')).not.toBeVisible() + }) +}) \ No newline at end of file diff --git a/src/components/@atoms/PseudoActionButton/PseudoActionButton.test.tsx b/src/components/@atoms/PseudoActionButton/PseudoActionButton.test.tsx new file mode 100644 index 000000000..c54104f37 --- /dev/null +++ b/src/components/@atoms/PseudoActionButton/PseudoActionButton.test.tsx @@ -0,0 +1,43 @@ +import { act, render, screen, userEvent, waitFor} from '@app/test-utils' +import { PseudoActionButton } from './PseudoActionButton' + +beforeAll(() => { + jest.useFakeTimers() +}) + +afterAll(() => { + jest.useRealTimers() +}) + +describe('PseudoActionButton', () => { + it('should show loading state when clicked and reset after timeout has been run', async () => { + const testIcon =
+ render(Test) + const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime}) + const button = screen.getByRole('button', { name: /Test/i }) + const icon = screen.getByTestId('icon') + expect(icon).toBeVisible() + await user.click(button) + expect(icon).not.toBeVisible() + act(() => { + jest.runAllTimers() + }) + await waitFor(() => { + expect(screen.getByTestId('icon')).toBeVisible() + }) + }) + + it('should maintain loading state if loading is set to true', async () => { + const testIcon =
+ render(Test) + const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime}) + const button = screen.getByRole('button', { name: /Test/i }) + expect(screen.queryByTestId('icon')).toEqual(null) + await user.click(button) + expect(screen.queryByTestId('icon')).toEqual(null) + act(() => { + jest.runAllTimers() + }) + expect(screen.queryByTestId('icon')).toEqual(null) + }) +}) \ No newline at end of file diff --git a/src/components/@atoms/PseudoActionButton/PseudoActionButton.tsx b/src/components/@atoms/PseudoActionButton/PseudoActionButton.tsx index 267bd52c6..beb66c1ba 100644 --- a/src/components/@atoms/PseudoActionButton/PseudoActionButton.tsx +++ b/src/components/@atoms/PseudoActionButton/PseudoActionButton.tsx @@ -1,20 +1,27 @@ -import { ComponentProps, useEffect, useState } from 'react' +import { ComponentProps, useEffect, useRef, useState } from 'react' import { Button } from '@ensdomains/thorin' -type Props = ComponentProps +type Props = { timeout?: number } & ComponentProps -export const PseudoActionButton = ({ loading, onClick, ...props }: Props) => { +export const PseudoActionButton = ({ loading, onClick, timeout = 3000, ...props }: Props) => { const [pseudoLoading, setPseudoLoading] = useState(false) + + const timerRef = useRef | null>(null) useEffect(() => { - if (pseudoLoading) setTimeout(() => setPseudoLoading(false), 5000) - }, [pseudoLoading, setPseudoLoading]) + return () => { + if (timerRef.current) clearTimeout(timerRef.current) + } + }, []) + return (