jest-clipboard
jest-clipboard provides a easy way to test code against the clipboard API.
Usage
import {readTextFromClipboard} from "./clipboard";
const thisFunctionWritesToClipboard = async () => {
await navigator.clipboard.writeText('text from clipboard');
};
const thisFunctionReadContentFromClipboard = async (): Promise<ClipboardItems> => {
return navigator.clipboard.read();
}
describe('Clipboard', () => {
beforeEach(() => {
setUpClipboard();
});
afterEach(() => {
tearDownClipboard();
});
it('should use text from clipboard', async () => {
await writeTextToClipboard('I want this to be in the transfer area');
expect(global.navigator.clipboard.writeText).toHaveBeenCalledWith('text from clipboard');
expect(await readTextFromClipboard()).toEqual('I want this to be in the transfer area');
});
});
Projects using jest-clipboard
Further reading