|
2 | 2 | // TODO: Statically verify we don't rely on NodeJS implicit named imports. |
3 | 3 | import lzString from 'lz-string' |
4 | 4 | import {type OptionsReceived} from 'pretty-format' |
5 | | -import {getQueriesForElement} from './get-queries-for-element' |
6 | 5 | import {getDocument} from './helpers' |
7 | 6 | import {logDOM} from './pretty-dom' |
8 | 7 | import * as queries from './queries' |
@@ -46,19 +45,20 @@ const logTestingPlaygroundURL = (element = getDocument().body) => { |
46 | 45 | return playgroundUrl |
47 | 46 | } |
48 | 47 |
|
49 | | -const initialValue = {debug, logTestingPlaygroundURL} |
| 48 | +const initialValue: Record<string, Function> = {debug, logTestingPlaygroundURL} |
50 | 49 |
|
51 | | -export const screen = |
52 | | - typeof document !== 'undefined' && document.body // eslint-disable-line @typescript-eslint/no-unnecessary-condition |
53 | | - ? getQueriesForElement(document.body, queries, initialValue) |
54 | | - : Object.keys(queries).reduce((helpers, key) => { |
55 | | - // `key` is for all intents and purposes the type of keyof `helpers`, which itself is the type of `initialValue` plus incoming properties from `queries` |
56 | | - // if `Object.keys(something)` returned Array<keyof typeof something> this explicit type assertion would not be necessary |
57 | | - // see https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript |
58 | | - helpers[key as keyof typeof initialValue] = () => { |
59 | | - throw new TypeError( |
60 | | - 'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error', |
61 | | - ) |
62 | | - } |
63 | | - return helpers |
64 | | - }, initialValue) |
| 50 | +export const screen = Object.entries(queries).reduce((helpers, [key, fn]) => { |
| 51 | + // `key` is for all intents and purposes the type of keyof `helpers`, which itself is the type of `initialValue` plus incoming properties from `queries` |
| 52 | + // if `Object.keys(something)` returned Array<keyof typeof something> this explicit type assertion would not be necessary |
| 53 | + // see https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript |
| 54 | + helpers[key] = (...args: any[]) => { |
| 55 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 56 | + if (typeof document === 'undefined' || !document.body) { |
| 57 | + throw new TypeError( |
| 58 | + 'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error', |
| 59 | + ) |
| 60 | + } |
| 61 | + return fn(document.body, ...args) |
| 62 | + } |
| 63 | + return helpers |
| 64 | +}, initialValue) |
0 commit comments