Skip to content

Commit

Permalink
[use-clipboard-copy_v0.1.x] Flow >= 0.203 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalduez committed Apr 6, 2023
1 parent 1ec13c5 commit 082ee2e
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// @flow

import { describe, it } from 'flow-typed-test';

import React from 'react';
import { useClipboard } from 'use-clipboard-copy';

describe('The `useClipboard` hook', () => {
it('should accept valid parameters', () => {
useClipboard();
useClipboard({ copiedTimeout: 200 });

// $FlowExpectedError[incompatible-call]
useClipboard(null);
// $FlowExpectedError[prop-missing]
// $FlowExpectedError[cannot-resolve-name]
useClipboard({ fail });
});

it('should validate the return type', () => {
const clipboard = useClipboard();

(clipboard.copied: boolean);
clipboard.copy('');
(clipboard.isSupported(): boolean);

function Test() {
const clipboard = useClipboard();
if (clipboard.target.current) {
(clipboard.target.current: HTMLElement);
}
return <input ref={clipboard.target} />;
}
});

it('should validate on default usage', () => {
function Test() {
const clipboard = useClipboard();

if (clipboard.target.current) {
(clipboard.target.current: HTMLElement);
}

return (
<>
<input ref={clipboard.target} />
<button onClick={clipboard.copy}>Copy</button>
</>
);
}
});

it('should validate on type parameter usage', () => {
function Test() {
const clipboard = useClipboard<HTMLInputElement>();

if (clipboard.target.current) {
(clipboard.target.current: HTMLInputElement);
}

return (
<>
{/* $FlowExpectedError[incompatible-type] */}
<textarea ref={clipboard.target} />
<input ref={clipboard.target} />
<button onClick={clipboard.copy}>Copy</button>
</>
);
}
});

it('should validate on imperative usage', () => {
function Test({ url }: { url: string }) {
const clipboard = useClipboard();

const handleClick = React.useCallback(() => {
clipboard.copy(url);
}, [clipboard.copy, url]);

return <button onClick={handleClick}>Copy</button>;
}
});

it('should validate on timeout and state usage', () => {
function Test({ url }: { url: string }) {
const clipboard = useClipboard({
copiedTimeout: 600,
});

return (
<>
<input ref={clipboard.target} value={url} readOnly />
<button onClick={clipboard.copy}>
{clipboard.copied ? 'Copied' : 'Copy'}
</button>
</>
);
}
});

it('should validate on callbacks usage', () => {
function Test() {
const clipboard = useClipboard({
onSuccess() {
console.log('Text was copied successfully!');
},
onError() {
console.log('Failed to copy text!');
},
});

return (
<>
<input ref={clipboard.target} />
<button onClick={clipboard.copy}>Copy</button>
</>
);
}
});

it('should validate on browser support check usage', () => {
function Test() {
const clipboard = useClipboard();

return clipboard.isSupported()
? 'yay! copy-to-clipboard is supported'
: 'meh. copy-to-clipboard is not supported';
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
declare module 'use-clipboard-copy' {
declare export type UseClipboardOptions = {|
copiedTimeout?: number,
onSuccess?: () => void,
onError?: () => void,
selectOnCopy?: boolean,
selectOnError?: boolean,
|};

declare export type ClipboardAPI<T> = {|
copied: boolean,
copy: (text?: string) => void,
isSupported: () => boolean,
target: { current: T | null, ... },
|};

declare export function useClipboard<
T: HTMLElement | HTMLInputElement | HTMLTextAreaElement = HTMLElement
>(
options?: UseClipboardOptions
): ClipboardAPI<T>;
}

0 comments on commit 082ee2e

Please sign in to comment.