From c15e90eb1c402ec34b254e21b5e1b558fef480e5 Mon Sep 17 00:00:00 2001 From: Harrison Lambeth Date: Fri, 24 Apr 2020 21:58:03 -0700 Subject: [PATCH] Add tests for types (only works in 3.9.0) --- src/__tests__/types.tsx | 239 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 src/__tests__/types.tsx diff --git a/src/__tests__/types.tsx b/src/__tests__/types.tsx new file mode 100644 index 000000000..cfa48c8f2 --- /dev/null +++ b/src/__tests__/types.tsx @@ -0,0 +1,239 @@ +/** @jsx createElement */ +import { createElement, Component, FunctionComponent, Context, GeneratorComponent, Child } from ".."; + +describe("types", () => { + + type MyProps = { + message: string; + }; + let elem: any; + + test("Primitive Components", () => { + + const MyString: Component = 'Hello'; + const MyNumber: Component = 10; + const MyBoolean: Component = true; + const MyNull: Component = null; + const MyUndefined: Component = undefined; + const MyElement: Component = ; + + // @ts-expect-error + const NotComponent: Component = {}; + + // @ts-expect-error + const StringsDontHaveProps: Component = 'Hello'; + + }); + + test ("Components", () => { + const MyFunctionComponent: Component = function(this, props) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncFunctionComponent: Component = async function (this, props) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + const MyGeneratorComponent: Component = function* (this, initialProps) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncGeneratorComponent: Component = async function* (this, initialProps) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for await (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + }); + + test("Function Components", () => { + const MyFunctionComponent: FunctionComponent = function(this, props) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncFunctionComponent: FunctionComponent = async function (this, props) { + const ctx: Context = this; + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + // @ts-expect-error + const MyGeneratorComponent: FunctionComponent = function* (this, props) { + yield
; + } + // @ts-expect-error + const MyAsyncGeneratorComponent: FunctionComponent = async function* (this, props) { + yield
; + } + }) + + test ("Generator Components", () => { + const MyGeneratorComponent: GeneratorComponent = function* (this, initialProps) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + const MyAsyncGeneratorComponent: GeneratorComponent = async function* (this, initialProps) { + const ctx: Context = this; + let message: string = initialProps.message; + // @ts-expect-error + let unexpected = initialProps.unexpected; + + for await (const newProps of this) { + let newMessage: string = initialProps.message; + // @ts-expect-error + let newUnexpected = newProps.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + // TODO: add ts-expect-error at some point in the future, I guess? + // This will not pass because the function is infered as any, and any matches Iterator. + // Hopefully a later typescript version fixes this :( + const MyFunctionComponent: GeneratorComponent = function(this, props) { + return
; + } + // @ts-expect-error + const MyAsyncFunctionComponent: GeneratorComponent = async function(this, props) { + return
; + } + }); + + test ("Loose Typings", () => { + function MyFunctionComponent(props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + async function MyAsyncFunctionComponent(props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + function* MyGeneratorComponent(this: Context, props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + for (props of this) { + let newMessage: string = props.message; + // @ts-expect-error + let newUnexpected = props.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + + async function* MyAsyncGeneratorComponent(this: Context, props: MyProps) { + let message: string = props.message; + // @ts-expect-error + let unexpected = props.unexpected; + + for await (props of this) { + let newMessage: string = props.message; + // @ts-expect-error + let newUnexpected = props.unexpected; + yield
; + } + + return
; + } + // @ts-expect-error + elem = ; + elem = ; + }); + +}); \ No newline at end of file