@@ -17,6 +17,7 @@ export async function testRender() {
17
17
18
18
// helpers
19
19
const { container, rerender, debug} = page
20
+ expectType < HTMLElement , typeof container > ( container )
20
21
return { container, rerender, debug}
21
22
}
22
23
@@ -35,13 +36,18 @@ export async function testPureRender() {
35
36
36
37
// helpers
37
38
const { container, rerender, debug} = page
39
+ expectType < HTMLElement , typeof container > ( container )
38
40
return { container, rerender, debug}
39
41
}
40
42
41
43
export function testRenderOptions ( ) {
42
44
const container = document . createElement ( 'div' )
43
45
const options = { container}
44
- render ( < div /> , options )
46
+ const { container : returnedContainer } = render ( < div /> , options )
47
+ // Unclear why TypeScript infers `HTMLElement` here when the the input `container` is `HTMLDivElement`
48
+ // Hopefully this breaks someday and we can switch to
49
+ // expectType<HTMLDivElement, typeof returnedContainer>(returnedContainer)
50
+ expectType < HTMLElement , typeof returnedContainer > ( returnedContainer )
45
51
}
46
52
47
53
export function testSVGRenderOptions ( ) {
@@ -50,7 +56,8 @@ export function testSVGRenderOptions() {
50
56
'svg' ,
51
57
)
52
58
const options = { container}
53
- render ( < svg /> , options )
59
+ const { container : returnedContainer } = render ( < svg /> , options )
60
+ expectType < SVGSVGElement , typeof returnedContainer > ( returnedContainer )
54
61
}
55
62
56
63
export function testFireEvent ( ) {
@@ -87,3 +94,27 @@ eslint
87
94
testing-library/no-debug: "off",
88
95
testing-library/prefer-screen-queries: "off"
89
96
*/
97
+
98
+ // https://stackoverflow.com/questions/53807517/how-to-test-if-two-types-are-exactly-the-same
99
+ type IfEquals < T , U , Yes = unknown , No = never > = ( < G > ( ) => G extends T
100
+ ? 1
101
+ : 2 ) extends < G > ( ) => G extends U ? 1 : 2
102
+ ? Yes
103
+ : No
104
+
105
+ /**
106
+ * Issues a type error if `Expected` is not identical to `Actual`.
107
+ *
108
+ * `Expected` should be declared when invoking `expectType`.
109
+ * `Actual` should almost always we be a `typeof value` statement.
110
+ *
111
+ * Source: https://github.com/mui-org/material-ui/blob/6221876a4b468a3330ffaafa8472de7613933b87/packages/material-ui-types/index.d.ts#L73-L84
112
+ *
113
+ * @example `expectType<number | string, typeof value>(value)`
114
+ * TypeScript issues a type error since `value is not assignable to never`.
115
+ * This means `typeof value` is not identical to `number | string`
116
+ * @param actual
117
+ */
118
+ declare function expectType < Expected , Actual > (
119
+ actual : IfEquals < Actual , Expected , Actual > ,
120
+ ) : void
0 commit comments