1
1
import { compressToEncodedURIComponent } from 'lz-string'
2
- import * as queries from './queries '
2
+ import type { OptionsReceived } from 'pretty-format '
3
3
import { getQueriesForElement } from './get-queries-for-element'
4
- import { logDOM } from './pretty-dom'
5
4
import { getDocument } from './helpers'
5
+ import { logDOM } from './pretty-dom'
6
+ import * as queries from './queries'
6
7
7
- function unindent ( string ) {
8
+ function unindent ( string : string ) {
8
9
// remove white spaces first, to save a few bytes.
9
10
// testing-playground will reformat on load any ways.
10
11
return string . replace ( / [ \t ] * [ \n ] [ \t ] * / g, '\n' )
11
12
}
12
13
13
- function encode ( value ) {
14
+ function encode ( value : string ) {
14
15
return compressToEncodedURIComponent ( unindent ( value ) )
15
16
}
16
17
17
- function getPlaygroundUrl ( markup ) {
18
+ function getPlaygroundUrl ( markup : string ) {
18
19
return `https://testing-playground.com/#markup=${ encode ( markup ) } `
19
20
}
20
21
21
- const debug = ( element , maxLength , options ) =>
22
+ const debug = (
23
+ element : ( Element | HTMLDocument ) [ ] ,
24
+ maxLength ?: number ,
25
+ options ?: OptionsReceived ,
26
+ ) =>
22
27
Array . isArray ( element )
23
28
? element . forEach ( el => logDOM ( el , maxLength , options ) )
24
29
: logDOM ( element , maxLength , options )
25
30
26
31
const logTestingPlaygroundURL = ( element = getDocument ( ) . body ) => {
32
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27
33
if ( ! element || ! ( 'innerHTML' in element ) ) {
28
34
console . log ( `The element you're providing isn't a valid DOM element.` )
29
35
return
30
36
}
37
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
31
38
if ( ! element . innerHTML ) {
32
39
console . log ( `The provided element doesn't have any children.` )
33
40
return
@@ -37,15 +44,22 @@ const logTestingPlaygroundURL = (element = getDocument().body) => {
37
44
)
38
45
}
39
46
40
- const initialValue = { debug, logTestingPlaygroundURL}
47
+ const initialValue : {
48
+ [ key in keyof typeof queries | 'debug' | 'logTestingPlaygroundURL' ] ?: Function
49
+ } = { debug, logTestingPlaygroundURL}
50
+
41
51
export const screen =
42
- typeof document !== 'undefined' && document . body
52
+ typeof document !== 'undefined' && document . body // eslint-disable-line @typescript-eslint/no-unnecessary-condition
43
53
? getQueriesForElement ( document . body , queries , initialValue )
44
- : Object . keys ( queries ) . reduce ( ( helpers , key ) => {
54
+ : typedKeysOf ( queries ) . reduce < typeof initialValue > ( ( helpers , key ) => {
45
55
helpers [ key ] = ( ) => {
46
56
throw new TypeError (
47
57
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error' ,
48
58
)
49
59
}
50
60
return helpers
51
61
} , initialValue )
62
+
63
+ function typedKeysOf < O extends Object > ( o : O ) {
64
+ return Object . keys ( o ) as Array < keyof O >
65
+ }
0 commit comments