-
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.
- Loading branch information
Showing
9 changed files
with
177 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ biome.jsonc | |
components.json | ||
tsconfig.tsbuildinfo | ||
*.test.* | ||
*.spec.* | ||
*.spec.* | ||
vitest.config.ts |
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,32 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import type { RefObject } from 'react'; | ||
import { useEventListener } from './use-event-listener'; | ||
import { vi } from 'vitest'; | ||
|
||
describe('useEventListener', () => { | ||
it('should call handler when the event is triggered', () => { | ||
const handler = vi.fn(); | ||
const event = new Event('click'); | ||
const ref: RefObject<HTMLDivElement> = { current: document.createElement('div') }; | ||
|
||
renderHook(() => useEventListener('click', handler, ref)); | ||
|
||
ref.current?.dispatchEvent(event); | ||
|
||
expect(handler).toHaveBeenCalledWith(event); | ||
}); | ||
|
||
it('should not call handler after unmount', () => { | ||
const handler = vi.fn(); | ||
const event = new Event('click'); | ||
const ref: RefObject<HTMLDivElement> = { current: document.createElement('div') }; | ||
|
||
const { unmount } = renderHook(() => useEventListener('click', handler, ref)); | ||
|
||
unmount(); | ||
|
||
ref.current?.dispatchEvent(event); | ||
|
||
expect(handler).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,74 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useRef } from 'react'; | ||
import { useEventListener } from './use-event-listener'; | ||
import { vi } from 'vitest'; | ||
|
||
describe('useEventListener', () => { | ||
it('adds event listener to window when no element is provided', () => { | ||
const handler = vi.fn(); | ||
const { unmount } = renderHook(() => useEventListener('click', handler)); | ||
|
||
act(() => { | ||
window.dispatchEvent(new Event('click')); | ||
}); | ||
|
||
expect(handler).toHaveBeenCalled(); | ||
|
||
unmount(); | ||
}); | ||
|
||
it('adds event listener to provided element', () => { | ||
const handler = vi.fn(); | ||
const { result, unmount } = renderHook(() => { | ||
const ref = useRef<HTMLDivElement>(document.createElement('div')); | ||
useEventListener('click', handler, ref); | ||
return ref; | ||
}); | ||
|
||
act(() => { | ||
result.current?.current?.dispatchEvent(new Event('click')); | ||
}); | ||
|
||
expect(handler).toHaveBeenCalled(); | ||
|
||
unmount(); | ||
}); | ||
|
||
it('removes event listener when component unmounts', () => { | ||
const handler = vi.fn(); | ||
const { unmount } = renderHook(() => useEventListener('click', handler)); | ||
|
||
unmount(); | ||
|
||
act(() => { | ||
window.dispatchEvent(new Event('click')); | ||
}); | ||
|
||
expect(handler).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('updates event listener when handler changes', () => { | ||
const handler1 = vi.fn(); | ||
const handler2 = vi.fn(); | ||
const { rerender, unmount } = renderHook( | ||
({ handler }) => useEventListener('click', handler), | ||
{ initialProps: { handler: handler1 } } | ||
); | ||
|
||
act(() => { | ||
window.dispatchEvent(new Event('click')); | ||
}); | ||
|
||
expect(handler1).toHaveBeenCalled(); | ||
|
||
rerender({ handler: handler2 }); | ||
|
||
act(() => { | ||
window.dispatchEvent(new Event('click')); | ||
}); | ||
|
||
expect(handler2).toHaveBeenCalled(); | ||
|
||
unmount(); | ||
}); | ||
}); |
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,12 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useMounted } from './use-mounted'; | ||
|
||
describe('useMounted', () => { | ||
it('should return true after the component has been mounted', () => { | ||
const { result, waitForNextUpdate } = renderHook(() => useMounted()); | ||
|
||
waitForNextUpdate().then(() => { | ||
expect(result.current).toBe(true); | ||
}); | ||
}); | ||
}); |
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,25 @@ | ||
import { parseJson } from './parse-json'; | ||
|
||
describe('parseJson', () => { | ||
it('should return an object when a valid JSON string is passed', () => { | ||
const jsonString = '{"key":"value"}'; | ||
expect(parseJson(jsonString)).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('should return undefined when an invalid JSON string is passed', () => { | ||
const invalidJsonString = '{key:value}'; | ||
expect(parseJson(invalidJsonString)).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined when "undefined" is passed', () => { | ||
expect(parseJson("undefined")).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined when null is passed', () => { | ||
expect(parseJson(null)).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined when an empty string is passed', () => { | ||
expect(parseJson("")).toBeUndefined(); | ||
}); | ||
}); |
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,14 @@ | ||
/// <reference types="vitest" /> | ||
|
||
import react from "@vitejs/plugin-react-swc"; | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
plugins: [react()], | ||
test: { | ||
environment: "happy-dom", | ||
globals: true, | ||
restoreMocks: true, | ||
include: ["**/*.test.?(c|m)[jt]s?(x)"], | ||
}, | ||
}); |