Skip to content

Commit 066a41f

Browse files
riotrahRayat Rahmaneps1lon
authored
chore: Convert screen.js to TypeScript (#1002)
Co-authored-by: Rayat Rahman <rayat@adaptilab.com> Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
1 parent 992f8b5 commit 066a41f

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
},
5151
"devDependencies": {
5252
"@testing-library/jest-dom": "^5.11.6",
53+
"@types/lz-string": "^1.3.34",
5354
"jest-in-case": "^1.0.2",
5455
"jest-serializer-ansi": "^1.0.3",
5556
"jest-watch-select-projects": "^2.0.0",

src/screen.js renamed to src/screen.ts

Lines changed: 19 additions & 8 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?: Array<Element | HTMLDocument> | Element | HTMLDocument,
24+
maxLength?: number,
25+
options?: OptionsReceived,
26+
): void =>
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
@@ -38,11 +45,15 @@ const logTestingPlaygroundURL = (element = getDocument().body) => {
3845
}
3946

4047
const initialValue = {debug, logTestingPlaygroundURL}
48+
4149
export const screen =
42-
typeof document !== 'undefined' && document.body
50+
typeof document !== 'undefined' && document.body // eslint-disable-line @typescript-eslint/no-unnecessary-condition
4351
? getQueriesForElement(document.body, queries, initialValue)
4452
: Object.keys(queries).reduce((helpers, key) => {
45-
helpers[key] = () => {
53+
// `key` is for all intents and purposes the type of keyof `helpers`, which itself is the type of `initialValue` plus incoming properties from `queries`
54+
// if `Object.keys(something)` returned Array<keyof typeof something> this explicit type assertion would not be necessary
55+
// see https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript
56+
helpers[key as keyof typeof initialValue] = () => {
4657
throw new TypeError(
4758
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error',
4859
)

0 commit comments

Comments
 (0)