diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /preact/compat/test/ts | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'preact/compat/test/ts')
-rw-r--r-- | preact/compat/test/ts/forward-ref.tsx | 26 | ||||
-rw-r--r-- | preact/compat/test/ts/lazy.tsx | 17 | ||||
-rw-r--r-- | preact/compat/test/ts/memo.tsx | 56 | ||||
-rw-r--r-- | preact/compat/test/ts/react-default.tsx | 6 | ||||
-rw-r--r-- | preact/compat/test/ts/react-star.tsx | 7 | ||||
-rw-r--r-- | preact/compat/test/ts/scheduler.ts | 19 | ||||
-rw-r--r-- | preact/compat/test/ts/suspense.tsx | 52 | ||||
-rw-r--r-- | preact/compat/test/ts/tsconfig.json | 20 | ||||
-rw-r--r-- | preact/compat/test/ts/utils.ts | 4 |
9 files changed, 207 insertions, 0 deletions
diff --git a/preact/compat/test/ts/forward-ref.tsx b/preact/compat/test/ts/forward-ref.tsx new file mode 100644 index 0000000..9a920cb --- /dev/null +++ b/preact/compat/test/ts/forward-ref.tsx @@ -0,0 +1,26 @@ +import React from '../../src'; + +const MyInput: React.ForwardFn<{ id: string }, { focus(): void }> = ( + props, + ref +) => { + const inputRef = React.useRef<HTMLInputElement>(null); + + React.useImperativeHandle(ref, () => ({ + focus: () => { + if (inputRef.current) { + inputRef.current.focus(); + } + } + })); + + return <input {...props} ref={inputRef} />; +}; + +export const foo = React.forwardRef(MyInput); + +export const Bar = React.forwardRef<HTMLDivElement, { children: any }>( + (props, ref) => { + return <div ref={ref}>{props.children}</div>; + } +); diff --git a/preact/compat/test/ts/lazy.tsx b/preact/compat/test/ts/lazy.tsx new file mode 100644 index 0000000..b3797c1 --- /dev/null +++ b/preact/compat/test/ts/lazy.tsx @@ -0,0 +1,17 @@ +import * as React from '../../src'; + +export interface LazyProps { + isProp: boolean; +} + +interface LazyState { + forState: string; +} +export default class IsLazyComponent extends React.Component< + LazyProps, + LazyState +> { + render({ isProp }: LazyProps) { + return <div>{isProp ? 'Super Lazy TRUE' : 'Super Lazy FALSE'}</div>; + } +} diff --git a/preact/compat/test/ts/memo.tsx b/preact/compat/test/ts/memo.tsx new file mode 100644 index 0000000..4e89e26 --- /dev/null +++ b/preact/compat/test/ts/memo.tsx @@ -0,0 +1,56 @@ +import * as React from '../../src'; +import { expectType } from './utils'; + +interface MemoProps { + required: string; + optional?: string; + defaulted: string; +} + +interface MemoPropsExceptDefaults { + required: string; + optional?: string; +} + +const ComponentExceptDefaults = () => <div></div>; + +const ReadonlyBaseComponent = (props: Readonly<MemoProps>) => ( + <div>{props.required + props.optional + props.defaulted}</div> +); +ReadonlyBaseComponent.defaultProps = { defaulted: '' }; + +const BaseComponent = (props: MemoProps) => ( + <div>{props.required + props.optional + props.defaulted}</div> +); +BaseComponent.defaultProps = { defaulted: '' }; + +// memo for readonly component with default comparison +const MemoedReadonlyComponent = React.memo(ReadonlyBaseComponent); +expectType<React.FunctionComponent<MemoProps>>(MemoedReadonlyComponent); +export const memoedReadonlyComponent = ( + <MemoedReadonlyComponent required="hi" /> +); + +// memo for non-readonly component with default comparison +const MemoedComponent = React.memo(BaseComponent); +expectType<React.FunctionComponent<MemoProps>>(MemoedComponent); +export const memoedComponent = <MemoedComponent required="hi" />; + +// memo with custom comparison +const CustomMemoedComponent = React.memo(BaseComponent, (a, b) => { + expectType<MemoProps>(a); + expectType<MemoProps>(b); + return a.required === b.required; +}); +expectType<React.FunctionComponent<MemoProps>>(CustomMemoedComponent); +export const customMemoedComponent = <CustomMemoedComponent required="hi" />; + +const MemoedComponentExceptDefaults = React.memo<MemoPropsExceptDefaults>( + ComponentExceptDefaults +); +expectType<React.FunctionComponent<MemoPropsExceptDefaults>>( + MemoedComponentExceptDefaults +); +export const memoedComponentExceptDefaults = ( + <MemoedComponentExceptDefaults required="hi" /> +); diff --git a/preact/compat/test/ts/react-default.tsx b/preact/compat/test/ts/react-default.tsx new file mode 100644 index 0000000..f46c0b4 --- /dev/null +++ b/preact/compat/test/ts/react-default.tsx @@ -0,0 +1,6 @@ +import React from '../../src'; +class ReactIsh extends React.Component { + render() { + return <div>Text</div>; + } +} diff --git a/preact/compat/test/ts/react-star.tsx b/preact/compat/test/ts/react-star.tsx new file mode 100644 index 0000000..da82690 --- /dev/null +++ b/preact/compat/test/ts/react-star.tsx @@ -0,0 +1,7 @@ +// import React from '../../src'; +import * as React from '../../src'; +class ReactIsh extends React.Component { + render() { + return <div>Text</div>; + } +} diff --git a/preact/compat/test/ts/scheduler.ts b/preact/compat/test/ts/scheduler.ts new file mode 100644 index 0000000..999e652 --- /dev/null +++ b/preact/compat/test/ts/scheduler.ts @@ -0,0 +1,19 @@ +import { + unstable_runWithPriority, + unstable_NormalPriority, + unstable_LowPriority, + unstable_IdlePriority, + unstable_UserBlockingPriority, + unstable_ImmediatePriority, + unstable_now +} from '../../src'; + +const noop = () => null; +unstable_runWithPriority(unstable_IdlePriority, noop); +unstable_runWithPriority(unstable_LowPriority, noop); +unstable_runWithPriority(unstable_NormalPriority, noop); +unstable_runWithPriority(unstable_UserBlockingPriority, noop); +unstable_runWithPriority(unstable_ImmediatePriority, noop); + +if (typeof unstable_now() === 'number') { +} diff --git a/preact/compat/test/ts/suspense.tsx b/preact/compat/test/ts/suspense.tsx new file mode 100644 index 0000000..916dd6b --- /dev/null +++ b/preact/compat/test/ts/suspense.tsx @@ -0,0 +1,52 @@ +import * as React from '../../src'; + +interface LazyProps { + isProp: boolean; +} + +const IsLazyFunctional = (props: LazyProps) => ( + <div>{props.isProp ? 'Super Lazy TRUE' : 'Super Lazy FALSE'}</div> +); + +const FallBack = () => <div>Still working...</div>; +/** + * Have to mock dynamic import as import() throws a syntax error in the test runner + */ +const componentPromise = new Promise<{ default: typeof IsLazyFunctional }>( + resolve => { + setTimeout(() => { + resolve({ default: IsLazyFunctional }); + }, 800); + } +); + +/** + * For usage with import: + * const IsLazyComp = lazy(() => import('./lazy')); + */ +const IsLazyFunc = React.lazy(() => componentPromise); + +// Suspense using lazy component +class SuspensefulFunc extends React.Component { + render() { + return ( + <React.Suspense fallback={<FallBack />}> + <IsLazyFunc isProp={false} /> + </React.Suspense> + ); + } +} + +//SuspenseList using lazy components +function SuspenseListTester(props: any) { + return ( + <React.SuspenseList revealOrder="together"> + <React.Suspense fallback={<FallBack />}> + <IsLazyFunc isProp={false} /> + </React.Suspense> + <React.Suspense fallback={<FallBack />}> + <IsLazyFunc isProp={false} /> + </React.Suspense> + </React.SuspenseList> + ); +} diff --git a/preact/compat/test/ts/tsconfig.json b/preact/compat/test/ts/tsconfig.json new file mode 100644 index 0000000..742a039 --- /dev/null +++ b/preact/compat/test/ts/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "es6", + "moduleResolution": "node", + "lib": [ + "es6", + "dom", + ], + "strict": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react", + "noEmit": true, + "allowSyntheticDefaultImports": true + }, + "include": [ + "./**/*.ts", + "./**/*.tsx" + ] +} diff --git a/preact/compat/test/ts/utils.ts b/preact/compat/test/ts/utils.ts new file mode 100644 index 0000000..16ec22d --- /dev/null +++ b/preact/compat/test/ts/utils.ts @@ -0,0 +1,4 @@ +/** + * Assert the parameter is of a specific type. + */ +export const expectType = <T>(_: T): void => undefined; |