Skip to content

Commit 4105877

Browse files
author
Rayat Rahman
committed
chore(TS): Typescript migrate src/screen.js -> src/screen.ts (testing-library#494)
1 parent ff829dc commit 4105877

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
import {compressToEncodedURIComponent} from 'lz-string'
2-
import * as queries from './queries'
2+
import type {OptionsReceived} from 'pretty-format'
33
import {getQueriesForElement} from './get-queries-for-element'
4-
import {logDOM} from './pretty-dom'
54
import {getDocument} from './helpers'
5+
import {logDOM} from './pretty-dom'
6+
import * as queries from './queries'
67

7-
function unindent(string) {
8+
function unindent(string: string) {
89
// remove white spaces first, to save a few bytes.
910
// testing-playground will reformat on load any ways.
1011
return string.replace(/[ \t]*[\n][ \t]*/g, '\n')
1112
}
1213

13-
function encode(value) {
14+
function encode(value: string) {
1415
return compressToEncodedURIComponent(unindent(value))
1516
}
1617

17-
function getPlaygroundUrl(markup) {
18+
function getPlaygroundUrl(markup: string) {
1819
return `https://testing-playground.com/#markup=${encode(markup)}`
1920
}
2021

21-
const debug = (element, maxLength, options) =>
22+
const debug = (
23+
element: (Element | HTMLDocument)[],
24+
maxLength?: number,
25+
options?: OptionsReceived,
26+
) =>
2227
Array.isArray(element)
2328
? element.forEach(el => logDOM(el, maxLength, options))
2429
: logDOM(element, maxLength, options)
2530

2631
const logTestingPlaygroundURL = (element = getDocument().body) => {
32+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
2733
if (!element || !('innerHTML' in element)) {
2834
console.log(`The element you're providing isn't a valid DOM element.`)
2935
return
3036
}
37+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
3138
if (!element.innerHTML) {
3239
console.log(`The provided element doesn't have any children.`)
3340
return
@@ -37,15 +44,22 @@ const logTestingPlaygroundURL = (element = getDocument().body) => {
3744
)
3845
}
3946

40-
const initialValue = {debug, logTestingPlaygroundURL}
47+
const initialValue: {
48+
[key in keyof typeof queries | 'debug' | 'logTestingPlaygroundURL']?: Function
49+
} = {debug, logTestingPlaygroundURL}
50+
4151
export const screen =
42-
typeof document !== 'undefined' && document.body
52+
typeof document !== 'undefined' && document.body // eslint-disable-line @typescript-eslint/no-unnecessary-condition
4353
? getQueriesForElement(document.body, queries, initialValue)
44-
: Object.keys(queries).reduce((helpers, key) => {
54+
: typedKeysOf(queries).reduce<typeof initialValue>((helpers, key) => {
4555
helpers[key] = () => {
4656
throw new TypeError(
4757
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error',
4858
)
4959
}
5060
return helpers
5161
}, initialValue)
62+
63+
function typedKeysOf<O extends Object>(o: O) {
64+
return Object.keys(o) as Array<keyof O>
65+
}

0 commit comments

Comments
 (0)