-
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.
chore: vitest unit test integration for web
- Loading branch information
Showing
17 changed files
with
1,161 additions
and
6 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
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,9 @@ | ||
import { render } from '@testing-library/react' | ||
import { Button } from '../Button' | ||
|
||
describe('<Button /> tests', () => { | ||
it('should match snapshot', () => { | ||
const { baseElement } = render(<Button>button</Button>) | ||
expect(baseElement).toMatchSnapshot() | ||
}) | ||
}) |
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,22 @@ | ||
import { render } from '@testing-library/react' | ||
import { Dialog } from '../Dialog' | ||
|
||
describe('<Dialog /> tests', () => { | ||
it('should match snapshot for isOpen false', () => { | ||
const { baseElement } = render( | ||
<Dialog isOpen={false} onClose={() => {}}> | ||
hello world | ||
</Dialog>, | ||
) | ||
expect(baseElement).toMatchSnapshot() | ||
}) | ||
|
||
it('should match snapshot for isOpen true', () => { | ||
const { baseElement } = render( | ||
<Dialog isOpen={true} onClose={() => {}}> | ||
hello world | ||
</Dialog>, | ||
) | ||
expect(baseElement).toMatchSnapshot() | ||
}) | ||
}) |
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,17 @@ | ||
import { timeout } from '@/utils/testUtils' | ||
import { render } from '@testing-library/react' | ||
import { Toast } from '../Toast' | ||
|
||
describe('<Toast / > tests', () => { | ||
it('should show toast only for 3 sec', async () => { | ||
const onOnOpenSpy = vi.fn() | ||
const { baseElement } = render( | ||
<Toast open={true} onOpenChange={onOnOpenSpy}> | ||
This is a toast! | ||
</Toast>, | ||
) | ||
expect(baseElement).toMatchSnapshot('toast visible') | ||
await timeout(3500) | ||
expect(onOnOpenSpy).toBeCalledWith(false) | ||
}) | ||
}) |
13 changes: 13 additions & 0 deletions
13
web/src/components/tests/__snapshots__/Button.test.tsx.snap
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,13 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`<Button /> tests > should match snapshot 1`] = ` | ||
<body> | ||
<div> | ||
<button | ||
class="rounded-lg border-2 border-black disabled:bg-gray-300 bg-black text-white h-10 px-3" | ||
> | ||
button | ||
</button> | ||
</div> | ||
</body> | ||
`; |
43 changes: 43 additions & 0 deletions
43
web/src/components/tests/__snapshots__/Dialog.test.tsx.snap
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,43 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`<Dialog /> tests > should match snapshot for isOpen false 1`] = ` | ||
<body> | ||
<div /> | ||
</body> | ||
`; | ||
|
||
exports[`<Dialog /> tests > should match snapshot for isOpen false 2`] = ` | ||
<body> | ||
<div /> | ||
<div | ||
aria-label="dialog_overlay" | ||
class="fixed top-0 flex h-screen w-screen items-center justify-center bg-black bg-opacity-80" | ||
> | ||
<div | ||
aria-modal="true" | ||
class="border-3 rounded bg-white p-6 shadow-md" | ||
role="dialog" | ||
> | ||
hello world | ||
</div> | ||
</div> | ||
</body> | ||
`; | ||
|
||
exports[`<Dialog /> tests > should match snapshot for isOpen true 1`] = ` | ||
<body> | ||
<div /> | ||
<div | ||
aria-label="dialog_overlay" | ||
class="fixed top-0 flex h-screen w-screen items-center justify-center bg-black bg-opacity-80" | ||
> | ||
<div | ||
aria-modal="true" | ||
class="border-3 rounded bg-white p-6 shadow-md" | ||
role="dialog" | ||
> | ||
hello world | ||
</div> | ||
</div> | ||
</body> | ||
`; |
52 changes: 52 additions & 0 deletions
52
web/src/components/tests/__snapshots__/Toast.test.tsx.snap
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,52 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`<Toast / > tests > should show toast only for 3 sec > toast hidden 1`] = ` | ||
<body> | ||
<div /> | ||
<div | ||
class="fixed flex text-white justify-between gap-4 md:min-w-[250px] shadow-lg border rounded p-3 bottom-6 z-10 transition-all bg-blue-800 right-6" | ||
> | ||
This is a toast! | ||
<button | ||
aria-label="close toast" | ||
class="inline-flex h-5 w-5 items-center justify-center rounded-full text-white hover:bg-gray-100" | ||
> | ||
x | ||
</button> | ||
</div> | ||
</body> | ||
`; | ||
exports[`<Toast / > tests > should show toast only for 3 sec > toast visible 1`] = ` | ||
<body> | ||
<div /> | ||
<div | ||
class="fixed flex text-white justify-between gap-4 md:min-w-[250px] shadow-lg border rounded p-3 bottom-6 z-10 transition-all bg-blue-800 right-6" | ||
> | ||
This is a toast! | ||
<button | ||
aria-label="close toast" | ||
class="inline-flex h-5 w-5 items-center justify-center rounded-full text-white hover:bg-gray-100" | ||
> | ||
x | ||
</button> | ||
</div> | ||
</body> | ||
`; | ||
exports[`<Toast / > tests > should show toast only for 3 sec 1`] = ` | ||
<body> | ||
<div /> | ||
<div | ||
class="fixed flex text-white justify-between gap-4 md:min-w-[250px] shadow-lg border rounded p-3 bottom-6 z-10 transition-all bg-blue-800 right-6" | ||
> | ||
This is a toast! | ||
<button | ||
aria-label="close toast" | ||
class="inline-flex h-5 w-5 items-center justify-center rounded-full text-white hover:bg-gray-100" | ||
> | ||
x | ||
</button> | ||
</div> | ||
</body> | ||
`; |
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,71 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { useLocation, useNavigate } from 'react-router-dom' | ||
import { Mock } from 'vitest' | ||
import { useAuthState } from '../useAuth' | ||
import { useAuthRedirect } from '../useAuthRedirect' | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
useLocation: vi.fn(), | ||
useNavigate: vi.fn(), | ||
})) | ||
|
||
vi.mock('../useAuth', () => ({ | ||
useAuthState: vi.fn(), | ||
})) | ||
|
||
describe('useAuthRedirect', () => { | ||
it('should navigate to /chat if authenticated and on the root path', () => { | ||
const mockNavigate = vi.fn() | ||
;(useLocation as Mock).mockReturnValue({ pathname: '/' }) | ||
;(useNavigate as Mock).mockReturnValue(mockNavigate) | ||
;(useAuthState as Mock).mockReturnValue(true) | ||
|
||
renderHook(() => useAuthRedirect()) | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith('/chat', { replace: true }) | ||
}) | ||
|
||
it('should navigate to /login if not authenticated and on the root path', () => { | ||
const mockNavigate = vi.fn() | ||
;(useLocation as Mock).mockReturnValue({ pathname: '/' }) | ||
;(useNavigate as Mock).mockReturnValue(mockNavigate) | ||
;(useAuthState as Mock).mockReturnValue(false) | ||
|
||
renderHook(() => useAuthRedirect()) | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith('/login', { replace: true }) | ||
}) | ||
|
||
it('should navigate to / if authenticated and on a guest route', () => { | ||
const mockNavigate = vi.fn() | ||
;(useLocation as Mock).mockReturnValue({ pathname: '/login' }) | ||
;(useNavigate as Mock).mockReturnValue(mockNavigate) | ||
;(useAuthState as Mock).mockReturnValue(true) | ||
|
||
renderHook(() => useAuthRedirect()) | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true }) | ||
}) | ||
|
||
it('should navigate to /login if not authenticated and on a protected route', () => { | ||
const mockNavigate = vi.fn() | ||
;(useLocation as Mock).mockReturnValue({ pathname: '/chat' }) | ||
;(useNavigate as Mock).mockReturnValue(mockNavigate) | ||
;(useAuthState as Mock).mockReturnValue(false) | ||
|
||
renderHook(() => useAuthRedirect()) | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith('/login', { replace: true }) | ||
}) | ||
|
||
it('should not navigate if the conditions are not met', () => { | ||
const mockNavigate = vi.fn() | ||
;(useLocation as Mock).mockReturnValue({ pathname: '/chat' }) | ||
;(useNavigate as Mock).mockReturnValue(mockNavigate) | ||
;(useAuthState as Mock).mockReturnValue(true) | ||
|
||
renderHook(() => useAuthRedirect()) | ||
|
||
expect(mockNavigate).not.toHaveBeenCalled() | ||
}) | ||
}) |
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,35 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { useAutoFocus } from '../useAutoFocus' | ||
|
||
describe('useAutoFocus', () => { | ||
it('should focus the element on mount', () => { | ||
const focusMock = vi.fn() | ||
|
||
const ref = { | ||
current: { focus: focusMock }, | ||
} as unknown as React.RefObject<HTMLInputElement> | ||
|
||
renderHook(() => useAutoFocus(ref)) | ||
|
||
expect(focusMock).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should focus the element on dependency change', () => { | ||
const focusMock = vi.fn() | ||
|
||
const ref = { | ||
current: { focus: focusMock }, | ||
} as unknown as React.RefObject<HTMLInputElement> | ||
|
||
const { rerender } = renderHook(({ deps }) => useAutoFocus(ref, deps), { | ||
initialProps: { deps: [1] }, | ||
}) | ||
|
||
expect(focusMock).toHaveBeenCalledTimes(1) | ||
|
||
rerender({ deps: [2] }) | ||
|
||
expect(focusMock).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |
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,60 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { Mock } from 'vitest' | ||
import { useClickOutside } from '../useClickOutside' | ||
|
||
describe('useClickOutside', () => { | ||
let ref: React.RefObject<HTMLDivElement> | ||
let callback: Mock | ||
|
||
beforeEach(() => { | ||
callback = vi.fn() | ||
ref = { | ||
current: document.createElement('div'), | ||
} as React.RefObject<HTMLDivElement> | ||
}) | ||
|
||
it('should call callback when clicking outside the element', () => { | ||
document.body.appendChild(ref.current!) | ||
|
||
renderHook(() => useClickOutside(ref, callback)) | ||
|
||
// Simulate clicking outside the element | ||
document.body.click() | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should not call callback when clicking inside the element', () => { | ||
document.body.appendChild(ref.current!) | ||
|
||
renderHook(() => useClickOutside(ref, callback)) | ||
|
||
// Simulate clicking inside the element | ||
ref.current!.click() | ||
|
||
expect(callback).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should clean up event listeners on unmount', () => { | ||
const addEventListenerSpy = vi.spyOn(document, 'addEventListener') | ||
const removeEventListenerSpy = vi.spyOn(document, 'removeEventListener') | ||
|
||
const { unmount } = renderHook(() => useClickOutside(ref, callback)) | ||
|
||
expect(addEventListenerSpy).toHaveBeenCalledWith( | ||
'mousedown', | ||
expect.any(Function), | ||
) | ||
|
||
unmount() | ||
|
||
expect(removeEventListenerSpy).toHaveBeenCalledWith( | ||
'mousedown', | ||
expect.any(Function), | ||
) | ||
|
||
// Clean up | ||
addEventListenerSpy.mockRestore() | ||
removeEventListenerSpy.mockRestore() | ||
}) | ||
}) |
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 @@ | ||
import '@testing-library/jest-dom/vitest' |
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,2 @@ | ||
export const timeout = (timeout: number) => | ||
new Promise(res => setTimeout(res, timeout)) |
Oops, something went wrong.