1
|
|
|
import { Component, PureComponent } from "react"; |
2
|
|
|
import type { ClassNames } from "./defs"; |
3
|
|
|
|
4
|
|
|
type Props = ClassNames<true, "props"> |
5
|
|
|
function Functional(_: ClassNames<"functional">) { return null } |
6
|
|
|
class ClassComponent extends Component<ClassNames<"component"|"comp0">> {} |
7
|
|
|
class ClassPureComponent extends PureComponent<ClassNames<"pureComponent">> {} |
8
|
|
|
|
9
|
|
|
describe("ClassNames", () => { |
10
|
|
|
it("<true>", () => { |
11
|
|
|
const suites: Record<string, ClassNames<true>> = { |
12
|
|
|
"className only": {className: ""}, |
13
|
|
|
//@ts-expect-error Property 'className' is missing |
14
|
|
|
"empty object" |
15
|
|
|
: {}, |
16
|
|
|
"classNames only": { |
17
|
|
|
//@ts-expect-error Object literal may only specify known properties, but 'classNames' does not exist |
18
|
|
|
classNames: {} |
19
|
|
|
}, |
20
|
|
|
"className and classNames": { |
21
|
|
|
className: "", |
22
|
|
|
//@ts-expect-error Object literal may only specify known properties, but 'classNames' does not exist |
23
|
|
|
classNames: {} |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
expect(suites).toBeInstanceOf(Object) |
27
|
|
|
}) |
28
|
|
|
|
29
|
|
|
it("<'class1'|'class2'>", () => { |
30
|
|
|
const suites: Record<string, ClassNames<"class1"|"class2">> = { |
31
|
|
|
"omitted": { |
32
|
|
|
//@ts-expect-error ReactRelated |
33
|
|
|
classNames: { |
34
|
|
|
class1: undefined |
35
|
|
|
} |
36
|
|
|
}, |
37
|
|
|
"classNames only": { |
38
|
|
|
classNames: {class1: undefined, class2: undefined} |
39
|
|
|
}, |
40
|
|
|
"className only": { |
41
|
|
|
//@ts-expect-error Object literal may only specify known properties, but 'className' does not exist |
42
|
|
|
className: "" |
43
|
|
|
}, |
44
|
|
|
//@ts-expect-error Property 'classNames' is missing |
45
|
|
|
"empty object" |
46
|
|
|
: {}, |
47
|
|
|
"className and classNames": { |
48
|
|
|
//@ts-expect-error Object literal may only specify known properties, but 'className' does not exist |
49
|
|
|
className: "", |
50
|
|
|
classNames: {class1: undefined, class2: undefined} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
expect(suites).toBeInstanceOf(Object) |
54
|
|
|
}) |
55
|
|
|
|
56
|
|
|
it("<true, 'class1'|'class2'>", () => { |
57
|
|
|
const suites: Record<string, ClassNames<true, "class1"|"class2">> = { |
58
|
|
|
"className and classNames": { |
59
|
|
|
className: "", |
60
|
|
|
classNames: {class1: undefined, class2: undefined} |
61
|
|
|
}, |
62
|
|
|
//@ts-expect-error Property 'className' is missing |
63
|
|
|
"classNames only": { |
64
|
|
|
classNames: {class1: undefined, class2: undefined} |
65
|
|
|
}, |
66
|
|
|
//@ts-expect-error Property 'classNames' is missing |
67
|
|
|
"className only": { |
68
|
|
|
className: "" |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
expect(suites).toBeInstanceOf(Object) |
72
|
|
|
}) |
73
|
|
|
}) |
74
|
|
|
describe("Miss-use", () => { |
75
|
|
|
it("<'class1', true>", () => { |
76
|
|
|
const suite1 |
77
|
|
|
//@ts-expect-error Type 'boolean' does not satisfy the constraint 'never' |
78
|
|
|
: ClassNames<"class1", true> |
79
|
|
|
= {classNames: {"class1": undefined}} |
80
|
|
|
|
81
|
|
|
expect(suite1).toBeInstanceOf(Object) |
82
|
|
|
}) |
83
|
|
|
|
84
|
|
|
it("<true, true>", () => { |
85
|
|
|
const suite1 |
86
|
|
|
: ClassNames<true, |
87
|
|
|
//@ts-expect-error Type 'boolean' does not satisfy the constraint 'never' |
88
|
|
|
true |
89
|
|
|
> = { |
90
|
|
|
className: ""} |
91
|
|
|
|
92
|
|
|
expect(suite1).toBeInstanceOf(Object) |
93
|
|
|
}) |
94
|
|
|
|
95
|
|
|
it("<'class1', 'class2'>", () => { |
96
|
|
|
const suite1 |
97
|
|
|
: ClassNames<"class1", |
98
|
|
|
//@ts-expect-error Type 'string' does not satisfy the constraint 'never' |
99
|
|
|
"class2" |
100
|
|
|
> |
101
|
|
|
= {classNames: {"class1": undefined, class2: undefined}} |
102
|
|
|
|
103
|
|
|
expect(suite1).toBeInstanceOf(Object) |
104
|
|
|
}) |
105
|
|
|
|
106
|
|
|
}) |
107
|
|
|
|
108
|
|
|
describe("ClassNamesFrom", () => { |
109
|
|
|
|
110
|
|
|
it("manually merge", () => { |
111
|
|
|
type AppClassNames = ( |
112
|
|
|
ClassNames<"App"> |
113
|
|
|
& ClassNames<typeof ClassComponent> |
114
|
|
|
& ClassNames<typeof ClassPureComponent> |
115
|
|
|
& ClassNames<typeof Functional> |
116
|
|
|
& ClassNames<Props> |
117
|
|
|
)["classNames"]; |
118
|
|
|
|
119
|
|
|
const suites: Record<string, AppClassNames> = { |
120
|
|
|
"exact": { |
121
|
|
|
App: undefined, |
122
|
|
|
component: undefined, |
123
|
|
|
comp0: undefined, |
124
|
|
|
functional: undefined, |
125
|
|
|
props: undefined, |
126
|
|
|
pureComponent: undefined |
127
|
|
|
}, |
128
|
|
|
"redundant": { |
129
|
|
|
//@ts-expect-error Object literal may only specify known properties, and 'redundant' does not exist |
130
|
|
|
redundant: undefined, |
131
|
|
|
App: undefined, |
132
|
|
|
component: undefined, |
133
|
|
|
functional: undefined, |
134
|
|
|
props: undefined, |
135
|
|
|
pureComponent: undefined, |
136
|
|
|
}, |
137
|
|
|
//@ts-expect-error Property 'App' is missing |
138
|
|
|
"missed App": { |
139
|
|
|
component: undefined, |
140
|
|
|
functional: undefined, |
141
|
|
|
props: undefined, |
142
|
|
|
pureComponent: undefined, |
143
|
|
|
}, |
144
|
|
|
//@ts-expect-error Property 'component' is missing |
145
|
|
|
"missed component": { |
146
|
|
|
App: undefined, |
147
|
|
|
functional: undefined, |
148
|
|
|
props: undefined, |
149
|
|
|
pureComponent: undefined, |
150
|
|
|
}, |
151
|
|
|
//@ts-expect-error Property 'pureComponent' is missing |
152
|
|
|
"missed pureComponent": { |
153
|
|
|
App: undefined, |
154
|
|
|
component: undefined, |
155
|
|
|
functional: undefined, |
156
|
|
|
props: undefined, |
157
|
|
|
}, |
158
|
|
|
//@ts-expect-error Property 'functional' is missing |
159
|
|
|
"missed functional": { |
160
|
|
|
App: undefined, |
161
|
|
|
component: undefined, |
162
|
|
|
props: undefined, |
163
|
|
|
pureComponent: undefined, |
164
|
|
|
}, |
165
|
|
|
//@ts-expect-error Property 'props' is missing |
166
|
|
|
"missed props": { |
167
|
|
|
App: undefined, |
168
|
|
|
component: undefined, |
169
|
|
|
functional: undefined, |
170
|
|
|
pureComponent: undefined, |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
expect(suites).toBeInstanceOf(Object) |
175
|
|
|
}) |
176
|
|
|
|
177
|
|
|
it("multiple apply", () => { |
178
|
|
|
type AppClassNames = ClassNames< |
179
|
|
|
true, |
180
|
|
|
"App", |
181
|
|
|
typeof ClassComponent, |
182
|
|
|
typeof ClassPureComponent, |
183
|
|
|
typeof Functional, |
184
|
|
|
Props |
185
|
|
|
>["classNames"]; |
186
|
|
|
|
187
|
|
|
const suites: Record<string, AppClassNames> = { |
188
|
|
|
"exact": { |
189
|
|
|
App: undefined, |
190
|
|
|
component: undefined, |
191
|
|
|
comp0: undefined, |
192
|
|
|
functional: undefined, |
193
|
|
|
props: undefined, |
194
|
|
|
pureComponent: undefined |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
expect(suites).toBeInstanceOf(Object) |
199
|
|
|
}) |
200
|
|
|
}) |
201
|
|
|
|