Skip to content

Commit d34f235

Browse files
author
Rayat Rahman
committed
chore(types): Typescript migrate src/screen.js -> src/screen.ts (#494)
Also update test snapshots
1 parent ff829dc commit d34f235

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/__tests__/get-user-code-frame.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ test('it returns only user code frame when code frames from node_modules are fir
3939
const userTrace = getUserCodeFrame(stack)
4040

4141
expect(userTrace).toMatchInlineSnapshot(`
42-
/sample-error/error-example.js:7:14
43-
5 | document.createTextNode('Hello world')
44-
6 | )
45-
> 7 | screen.debug()
46-
| ^
47-
48-
`)
42+
"/sample-error/error-example.js:7:14
43+
5 | document.createTextNode('Hello world')
44+
6 | )
45+
> 7 | screen.debug()
46+
| ^
47+
"
48+
`)
4949
})
5050

5151
test('it returns only user code frame when node code frames are present afterwards', () => {
@@ -59,13 +59,13 @@ test('it returns only user code frame when node code frames are present afterwar
5959
const userTrace = getUserCodeFrame()
6060

6161
expect(userTrace).toMatchInlineSnapshot(`
62-
/sample-error/error-example.js:7:14
63-
5 | document.createTextNode('Hello world')
64-
6 | )
65-
> 7 | screen.debug()
66-
| ^
67-
68-
`)
62+
"/sample-error/error-example.js:7:14
63+
5 | document.createTextNode('Hello world')
64+
6 | )
65+
> 7 | screen.debug()
66+
| ^
67+
"
68+
`)
6969
})
7070

7171
test("it returns empty string if file from code frame can't be read", () => {
+23-9
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)