diff --git a/src/defs.spec.ts b/src/defs.spec.ts deleted file mode 100644 index 19b1576..0000000 --- a/src/defs.spec.ts +++ /dev/null @@ -1,177 +0,0 @@ -import { Component, PureComponent } from "react"; -import type { ClassNames, ClassNamesProperty, ClassHash } from "./defs"; - -type Props = ClassNames> -function Functional(_: ClassNames>) { return null } -class ClassComponent extends Component>> {} -class ClassPureComponent extends PureComponent>> {} - -describe("ClassNames", () => { - it("", () => { - const suites: Record> = { - "className only": {className: ""}, - //@ts-expect-error Property 'className' is missing - "empty object" - : {}, - "classnames only": { - //@ts-expect-error Object literal may only specify known properties, but 'classnames' does not exist - classnames: {} - }, - "className and classnames": { - className: "", - //@ts-expect-error Object literal may only specify known properties, but 'classnames' does not exist - classnames: {} - } - } - expect(suites).toBeInstanceOf(Object) - }) - - it("<{class1, class2}>", () => { - const suites: Record>> = { - "omitted": { - //@ts-expect-error ReactRelated - classnames: { - class1: undefined - } - }, - "classnames only": { - classnames: {class1: undefined, class2: undefined} - }, - "className only": { - //@ts-expect-error Object literal may only specify known properties, but 'className' does not exist - className: "" - }, - //@ts-expect-error Property 'classnames' is missing - "empty object" - : {}, - "className and classnames": { - //@ts-expect-error Object literal may only specify known properties, but 'className' does not exist - className: "", - classnames: {class1: undefined, class2: undefined} - } - } - expect(suites).toBeInstanceOf(Object) - }) - - it("", () => { - const suites: Record>> = { - "className and classnames": { - className: "", - classnames: {class1: undefined, class2: undefined} - }, - //@ts-expect-error Property 'className' is missing - "classnames only": { - classnames: {class1: undefined, class2: undefined} - }, - //@ts-expect-error Property 'classnames' is missing - "className only": { - className: "" - } - } - expect(suites).toBeInstanceOf(Object) - }) - - it("nothing to pick", () => { - type NoClassNames = ClassNames - const suites: Record> = { - "nothing": {classnames: {}} - } - expect(suites).toBeInstanceOf(Object) - }) -}) - -describe("ClassNames from", () => { - it("manually merge", () => { - type AppClassNames = ( - ClassNamesProperty<{App: ClassHash}> - & ClassNames - & ClassNames - & ClassNames - & ClassNames - )["classnames"]; - - const suites: Record = { - "exact": { - App: undefined, - component: undefined, - comp0: undefined, - functional: undefined, - props: undefined, - pureComponent: undefined - }, - "redundant": { - //@ts-expect-error Object literal may only specify known properties, and 'redundant' does not exist - redundant: undefined, - App: undefined, - component: undefined, - functional: undefined, - props: undefined, - pureComponent: undefined, - }, - //@ts-expect-error Property 'App' is missing - "missed App": { - component: undefined, - functional: undefined, - props: undefined, - pureComponent: undefined, - }, - //@ts-expect-error Property 'component' is missing - "missed component": { - App: undefined, - functional: undefined, - props: undefined, - pureComponent: undefined, - }, - //@ts-expect-error Property 'pureComponent' is missing - "missed pureComponent": { - App: undefined, - component: undefined, - functional: undefined, - props: undefined, - }, - //@ts-expect-error Property 'functional' is missing - "missed functional": { - App: undefined, - component: undefined, - props: undefined, - pureComponent: undefined, - }, - //@ts-expect-error Property 'props' is missing - "missed props": { - App: undefined, - component: undefined, - functional: undefined, - pureComponent: undefined, - } - } - - expect(suites).toBeInstanceOf(Object) - }) - - it("multiple apply", () => { - type AppClassNames = ClassNames< - true, - ClassNamesProperty<{App: ClassHash}>, - typeof ClassComponent, - typeof ClassPureComponent, - typeof Functional, - Props - >["classnames"]; - - const suites: Record = { - "exact": { - App: undefined, - component: undefined, - comp0: undefined, - functional: undefined, - props: undefined, - pureComponent: undefined - } - } - - expect(suites).toBeInstanceOf(Object) - }) -}) diff --git a/src/defs.test.ts b/src/defs.test.ts index 7cab7cf..8c66ed0 100644 --- a/src/defs.test.ts +++ b/src/defs.test.ts @@ -1,15 +1,203 @@ import { Component, - // ReactElement + PureComponent } from "react" import type { ClassNamed, ClassHash, ReactRelated, GetProps, - ClassNamesProperty + ClassNamesProperty, + ClassNames } from "./defs" +type Props = ClassNames> +function Functional(_: ClassNames>) { return null } +class ClassComponent extends Component>> {} +class ClassPureComponent extends PureComponent>> {} + +describe("ClassNames", () => { + describe("direct", () => { + it("", () => { + const suites: Record> = { + "className only": {className: ""}, + //@ts-expect-error Property 'className' is missing + "empty object" + : {}, + "classnames only": { + //@ts-expect-error Object literal may only specify known properties, but 'classnames' does not exist + classnames: {} + }, + "className and classnames": { + className: "", + //@ts-expect-error Object literal may only specify known properties, but 'classnames' does not exist + classnames: {} + } + } + expect(suites).toBeInstanceOf(Object) + }) + + it("<{class1, class2}>", () => { + const suites: Record> = { + "omitted": { + //@ts-expect-error ReactRelated + classnames: { + class1: undefined + } + }, + "classnames only": { + classnames: {class1: undefined, class2: undefined} + }, + "className only": { + //@ts-expect-error Object literal may only specify known properties, but 'className' does not exist + className: "" + }, + //@ts-expect-error Property 'classnames' is missing + "empty object" + : {}, + "className and classnames": { + //@ts-expect-error Object literal may only specify known properties, but 'className' does not exist + className: "", + classnames: {class1: undefined, class2: undefined} + } + } + expect(suites).toBeInstanceOf(Object) + }) + + it("", () => { + const suites: Record> = { + "className and classnames": { + className: "", + classnames: {class1: undefined, class2: undefined} + }, + //@ts-expect-error Property 'className' is missing + "classnames only": { + classnames: {class1: undefined, class2: undefined} + }, + //@ts-expect-error Property 'classnames' is missing + "className only": { + className: "" + } + } + expect(suites).toBeInstanceOf(Object) + }) + + it("nothing to pick", () => { + type NoClassNames = ClassNames + const suite1: Record> = { + "nothing": {classnames: {class1: ""}} + } + const suite2: Record> = { + "nothing": {classnames: {class1: ""}} + } + expect({suite1, suite2}).toBeInstanceOf(Object) + }) + }) + + describe("from", () => { + it("manually merge", () => { + type AppClassNames = ( + ClassNamesProperty<{App: ClassHash}> + & ClassNames + & ClassNames + & ClassNames + & ClassNames + ); + + const suites: Record = { + "exact": { + App: undefined, + component: undefined, + comp0: undefined, + functional: undefined, + props: undefined, + pureComponent: undefined + }, + "redundant": { + //@ts-expect-error Object literal may only specify known properties, and 'redundant' does not exist + redundant: undefined, + App: undefined, + component: undefined, + functional: undefined, + props: undefined, + pureComponent: undefined, + }, + //@ts-expect-error Property 'App' is missing + "missed App": { + component: undefined, + functional: undefined, + props: undefined, + pureComponent: undefined, + }, + //@ts-expect-error Property 'component' is missing + "missed component": { + App: undefined, + functional: undefined, + props: undefined, + pureComponent: undefined, + }, + //@ts-expect-error Property 'pureComponent' is missing + "missed pureComponent": { + App: undefined, + component: undefined, + functional: undefined, + props: undefined, + }, + //@ts-expect-error Property 'functional' is missing + "missed functional": { + App: undefined, + component: undefined, + props: undefined, + pureComponent: undefined, + }, + //@ts-expect-error Property 'props' is missing + "missed props": { + App: undefined, + component: undefined, + functional: undefined, + pureComponent: undefined, + } + } + + expect(suites).toBeInstanceOf(Object) + }) + + it("multiple apply", () => { + type AppClassNames = ClassNames< + true, + ClassNamesProperty<{App: ClassHash}>, + typeof ClassComponent, + typeof ClassPureComponent, + typeof Functional, + Props + >; + + const suites: Record = { + "exact": { + App: undefined, + component: undefined, + comp0: undefined, + functional: undefined, + props: undefined, + pureComponent: undefined + } + } + + expect(suites).toBeInstanceOf(Object) + }) + }) +}) + describe("ClassNamesProperty", () => { it("Free declaration", () => { type Props = ClassNamesProperty<{ @@ -38,6 +226,7 @@ describe("ClassNamesProperty", () => { } expect(suites).toBeInstanceOf(Object) }) + it("Module based", () => { type CssModule = { App: ClassHash @@ -47,7 +236,7 @@ describe("ClassNamesProperty", () => { type Props = ClassNamesProperty type PropsWithWrong = ClassNamesProperty { }) }) -it.todo("ClassNamesFrom") describe("ReactRelated", () => { type Getter = GetProps