Skip to content

Commit d3ce72c

Browse files
committed
feat: convert config.js to typescript
1 parent b71dccd commit d3ce72c

19 files changed

+55
-26
lines changed

src/__tests__/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {configure, getConfig} from '../config'
1+
import {configure, getConfig} from '../config.ts'
22

33
describe('configuration API', () => {
44
let originalConfig

src/__tests__/element-queries.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {configure} from '../config'
1+
import {configure} from '../config.ts'
22
import {render, renderIntoDocument} from './helpers/test-utils'
33

44
beforeEach(() => {

src/__tests__/get-by-errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cases from 'jest-in-case'
22
import {screen} from '../'
3-
import {configure, getConfig} from '../config'
3+
import {configure, getConfig} from '../config.ts'
44
import {render} from './helpers/test-utils'
55

66
const originalConfig = getConfig()

src/__tests__/query-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as queryHelpers from '../query-helpers'
2-
import {configure, getConfig} from '../config'
2+
import {configure, getConfig} from '../config.ts'
33

44
const originalConfig = getConfig()
55
beforeEach(() => {

src/__tests__/role.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {configure, getConfig} from '../config'
1+
import {configure, getConfig} from '../config.ts'
22
import {getQueriesForElement} from '../get-queries-for-element'
33
import {render, renderIntoDocument} from './helpers/test-utils'
44

src/__tests__/suggestions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {configure} from '../config'
1+
import {configure} from '../config.ts'
22
import {screen, getSuggestedQuery} from '..'
33
import {renderIntoDocument, render} from './helpers/test-utils'
44

src/__tests__/wait-for.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {waitFor} from '../'
2-
import {configure, getConfig} from '../config'
2+
import {configure, getConfig} from '../config.ts'
33
import {renderIntoDocument} from './helpers/test-utils'
44

55
function deferred() {

src/config.js renamed to src/config.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
import {prettyDOM} from './pretty-dom'
2+
import {Callback} from './types'
3+
4+
export interface Config {
5+
testIdAttribute: string
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
asyncWrapper(cb: (...args: any[]) => any): Promise<any>
8+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9+
eventWrapper(cb: (...args: any[]) => any): void
10+
asyncUtilTimeout: number
11+
computedStyleSupportsPseudoElements: boolean
12+
defaultHidden: boolean
13+
showOriginalStackTrace: boolean
14+
throwSuggestions: boolean
15+
getElementError: (message: string | null, container: Element) => Error
16+
}
17+
18+
export interface ConfigFn {
19+
(existingConfig: Config): Partial<Config>
20+
}
21+
22+
interface InternalConfig {
23+
_disableExpensiveErrorDiagnostics: boolean
24+
}
225

326
// It would be cleaner for this to live inside './queries', but
427
// other parts of the code assume that all exports from
528
// './queries' are query functions.
6-
let config = {
29+
let config: Config & InternalConfig = {
730
testIdAttribute: 'data-testid',
831
asyncUtilTimeout: 1000,
932
// this is to support React's async `act` function.
@@ -36,7 +59,9 @@ let config = {
3659
}
3760

3861
export const DEFAULT_IGNORE_TAGS = 'script, style'
39-
export function runWithExpensiveErrorDiagnosticsDisabled(callback) {
62+
export function runWithExpensiveErrorDiagnosticsDisabled<T>(
63+
callback: Callback<T>,
64+
) {
4065
try {
4166
config._disableExpensiveErrorDiagnostics = true
4267
return callback()
@@ -45,7 +70,7 @@ export function runWithExpensiveErrorDiagnosticsDisabled(callback) {
4570
}
4671
}
4772

48-
export function configure(newConfig) {
73+
export function configure(newConfig: Partial<Config> | ConfigFn) {
4974
if (typeof newConfig === 'function') {
5075
// Pass the existing config out to the provided function
5176
// and accept a delta in return

src/events.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getConfig} from './config'
1+
import {getConfig} from './config.ts'
22
import {getWindowFromNode} from './helpers'
33
import {eventMap, eventAliasMap} from './event-map'
44

@@ -71,12 +71,15 @@ function createEvent(
7171
/* istanbul ignore if */
7272
if (typeof window.DataTransfer === 'function') {
7373
Object.defineProperty(event, dataTransferKey, {
74-
value: Object
75-
.getOwnPropertyNames(dataTransferValue)
76-
.reduce((acc, propName) => {
77-
Object.defineProperty(acc, propName, {value: dataTransferValue[propName]});
78-
return acc;
79-
}, new window.DataTransfer())
74+
value: Object.getOwnPropertyNames(dataTransferValue).reduce(
75+
(acc, propName) => {
76+
Object.defineProperty(acc, propName, {
77+
value: dataTransferValue[propName],
78+
})
79+
return acc
80+
},
81+
new window.DataTransfer(),
82+
),
8083
})
8184
} else {
8285
Object.defineProperty(event, dataTransferKey, {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export * from './screen'
1515
export * from './query-helpers'
1616
export {getRoles, logRoles, isInaccessible} from './role-helpers'
1717
export * from './pretty-dom'
18-
export {configure, getConfig} from './config'
18+
export {configure, getConfig} from './config.ts'
1919
export * from './suggestions'
2020

2121
export {

src/queries/all-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from '../matches'
22
export * from '../get-node-text'
33
export * from '../query-helpers'
4-
export * from '../config'
4+
export * from '../config.ts'

src/queries/label-text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getConfig} from '../config'
1+
import {getConfig} from '../config.ts'
22
import {checkContainerType} from '../helpers'
33
import {getLabels, getRealLabels, getLabelContent} from '../label-helpers'
44
import {

src/queries/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
22
import {checkContainerType} from '../helpers'
3-
import {DEFAULT_IGNORE_TAGS} from '../config'
3+
import {DEFAULT_IGNORE_TAGS} from '../config.ts'
44
import {
55
fuzzyMatches,
66
matches,

src/query-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {getSuggestedQuery} from './suggestions'
22
import {fuzzyMatches, matches, makeNormalizer} from './matches'
33
import {waitFor} from './wait-for'
4-
import {getConfig} from './config'
4+
import {getConfig} from './config.ts'
55

66
function getElementError(message, container) {
77
return getConfig().getElementError(message, container)

src/role-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {elementRoles} from 'aria-query'
22
import {computeAccessibleName} from 'dom-accessibility-api'
33
import {prettyDOM} from './pretty-dom'
4-
import {getConfig} from './config'
4+
import {getConfig} from './config.ts'
55

66
const elementRoleList = buildElementRoleList(elementRoles)
77

src/suggestions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {computeAccessibleName} from 'dom-accessibility-api'
22
import {getDefaultNormalizer} from './matches'
33
import {getNodeText} from './get-node-text'
4-
import {DEFAULT_IGNORE_TAGS, getConfig} from './config'
4+
import {DEFAULT_IGNORE_TAGS, getConfig} from './config.ts'
55
import {getImplicitAriaRoles, isInaccessible} from './role-helpers'
66
import {getLabels} from './label-helpers'
77

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Callback<T> = () => T

src/wait-for-dom-change.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
clearTimeout,
77
runWithRealTimers,
88
} from './helpers'
9-
import {getConfig} from './config'
9+
import {getConfig} from './config.ts'
1010

1111
let hasWarned = false
1212

src/wait-for.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
setTimeout,
1010
clearTimeout,
1111
} from './helpers'
12-
import {getConfig, runWithExpensiveErrorDiagnosticsDisabled} from './config'
12+
import {getConfig, runWithExpensiveErrorDiagnosticsDisabled} from './config.ts'
1313

1414
// This is so the stack trace the developer sees is one that's
1515
// closer to their code (because async stack traces are hard to follow).

0 commit comments

Comments
 (0)