-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from belk1ng/test-useCopyToClipboard
test: add tests for useCopyToClipboard
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/hooks/useCopyToClipboard/useCopyToClipboard.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import { useCopyToClipboard } from './useCopyToClipboard'; | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('Should be defined', () => { | ||
const { result } = renderHook(() => useCopyToClipboard()); | ||
|
||
expect(result.current.value).toBeNull(); | ||
expect(typeof result.current.copy).toBe('function'); | ||
}); | ||
|
||
describe('Should copy to clipboard using navigator', () => { | ||
beforeAll(() => { | ||
Object.assign(navigator, { | ||
clipboard: { | ||
writeText: vi.fn() | ||
} | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
Object.assign(navigator, { | ||
clipboard: {} | ||
}); | ||
}); | ||
|
||
it('Should copy value to clipboard', async () => { | ||
const copyText = 'Some string content'; | ||
const { result } = renderHook(() => useCopyToClipboard()); | ||
|
||
await act(async () => { | ||
await result.current.copy(copyText); | ||
}); | ||
|
||
const { | ||
clipboard: { writeText } | ||
} = navigator; | ||
|
||
expect(result.current.value).toBe(copyText); | ||
expect(writeText).toHaveBeenLastCalledWith(copyText); | ||
expect(writeText).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it('Should copy last value to clipboard', async () => { | ||
const copyText = 'Lorem ipsum dolor sit amet'; | ||
const { result } = renderHook(() => useCopyToClipboard()); | ||
|
||
await act(async () => { | ||
await result.current.copy('Some string'); | ||
await result.current.copy(copyText); | ||
}); | ||
|
||
const { | ||
clipboard: { writeText } | ||
} = navigator; | ||
|
||
expect(result.current.value).toBe(copyText); | ||
expect(writeText).toHaveBeenCalledTimes(2); | ||
expect(writeText).toHaveBeenLastCalledWith(copyText); | ||
}); | ||
}); | ||
|
||
describe('Should copy to clipboard using legacy way', () => { | ||
beforeAll(() => { | ||
Object.assign(document, { | ||
execCommand: vi.fn() | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
Object.assign(document, { | ||
execCommand: undefined | ||
}); | ||
}); | ||
|
||
it('Should copy value to clipboard using legacy way', async () => { | ||
const copyText = 'Some string content'; | ||
const { result } = renderHook(() => useCopyToClipboard()); | ||
|
||
await act(async () => { | ||
await result.current.copy(copyText); | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const { execCommand } = document; | ||
|
||
expect(result.current.value).toBe(copyText); | ||
expect(execCommand).toHaveBeenCalledOnce(); | ||
expect(execCommand).toHaveBeenLastCalledWith('copy'); | ||
}); | ||
|
||
it('Should copy last value to clipboard using legacy way', async () => { | ||
const copyText = 'Lorem ipsum dolor sit amet'; | ||
const { result } = renderHook(() => useCopyToClipboard()); | ||
|
||
await act(async () => { | ||
await result.current.copy('Some string'); | ||
await result.current.copy(copyText); | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const { execCommand } = document; | ||
|
||
expect(result.current.value).toBe(copyText); | ||
expect(execCommand).toHaveBeenCalledTimes(2); | ||
expect(execCommand).toHaveBeenLastCalledWith('copy'); | ||
}); | ||
}); |