Skip to content

Commit

Permalink
Merge pull request #93 from belk1ng/test-useCopyToClipboard
Browse files Browse the repository at this point in the history
test: add tests for useCopyToClipboard
  • Loading branch information
debabin authored Jun 2, 2024
2 parents e432087 + 692e143 commit 80b09a6
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/hooks/useCopyToClipboard/useCopyToClipboard.test.ts
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');
});
});

0 comments on commit 80b09a6

Please sign in to comment.