Skip to content

Commit eaedbde

Browse files
committed
Add function-based overload to configure()
This commit prepares the way for configure() to be used other than just for `testIdAttribute`: it exposes the whole config internally and upgrades the configure() function so that it can take a function that. I've introduced explicit tests for this more complex configuration API and a new documentation section for it. The documentation for the function-based configure() API could be improved with a real example once there is a real key for which it would be useful.
1 parent a9c61a1 commit eaedbde

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ when a real user uses it.
104104
- [`within` and `getQueriesForElement` APIs](#within-and-getqueriesforelement-apis)
105105
- [Debugging](#debugging)
106106
- [`prettyDOM`](#prettydom)
107+
- [Configuration](#configuration)
107108
- [Implementations](#implementations)
108109
- [Using Without Jest](#using-without-jest)
109110
- [FAQ](#faq)
@@ -941,6 +942,19 @@ console.log(prettyDOM(div))
941942

942943
This function is what also powers [the automatic debugging output described above](#debugging).
943944

945+
## Configuration
946+
947+
The library can be configured via the `configure` function, which accepts:
948+
949+
- a plain JS object; this will be merged into the existing configuration. e.g. `configure({testIdAttribute: 'not-data-testid'})`
950+
- a function; the function will be given the existing configuration, and should return a plain JS object which will be merged as above, e.g.
951+
`configure(existingConfig => ({something: [...existingConfig.something, 'extra value for the something array']}))`
952+
953+
Configuration options:
954+
955+
`testIdAttribute`
956+
: The attribute used by `getByTestId` and related queries. Defaults to `data-testid`. See [`getByTestId`](#getbytestid).
957+
944958
## Implementations
945959

946960
This library was not built to be used on its own. The original implementation
@@ -1129,6 +1143,7 @@ Thanks goes to these people ([emoji key][emojis]):
11291143
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [<img src="https://avatars1.githubusercontent.com/u/186971?v=4" width="100px;"/><br /><sub><b>Neil Kistner</b></sub>](https://neilkistner.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [<img src="https://avatars3.githubusercontent.com/u/1448597?v=4" width="100px;"/><br /><sub><b>Ben Chauvette</b></sub>](http://bdchauvette.net/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [<img src="https://avatars2.githubusercontent.com/u/777527?v=4" width="100px;"/><br /><sub><b>Jeff Baumgardt</b></sub>](https://github.com/JeffBaumgardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/4658208?v=4" width="100px;"/><br /><sub><b>Matan Kushner</b></sub>](http://matchai.me)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Documentation") [🤔](#ideas-matchai "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Tests") | [<img src="https://avatars2.githubusercontent.com/u/5779538?v=4" width="100px;"/><br /><sub><b>Alex Wendte</b></sub>](http://www.wendtedesigns.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Tests") | [<img src="https://avatars0.githubusercontent.com/u/2196208?v=4" width="100px;"/><br /><sub><b>Tamas Fodor</b></sub>](https://github.com/ruffle1986)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=ruffle1986 "Documentation") |
11301144
| [<img src="https://avatars3.githubusercontent.com/u/14793495?v=4" width="100px;"/><br /><sub><b>Benjamin Eckardt</b></sub>](https://github.com/BenjaminEckardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=BenjaminEckardt "Code") | [<img src="https://avatars3.githubusercontent.com/u/205752?v=4" width="100px;"/><br /><sub><b>Ryan Campbell</b></sub>](https://github.com/campbellr)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=campbellr "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/1335519?v=4" width="100px;"/><br /><sub><b>Taylor Briggs</b></sub>](https://taylor-briggs.com)<br />[⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=TaylorBriggs "Tests") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jgoz "Code") | [<img src="https://avatars2.githubusercontent.com/u/3382565?v=4" width="100px;"/><br /><sub><b>C. T. Lin</b></sub>](https://github.com/chentsulin)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=chentsulin "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/5312329?v=4" width="100px;"/><br /><sub><b>Terrence Wong</b></sub>](http://terrencewwong.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=terrencewwong "Code") | [<img src="https://avatars0.githubusercontent.com/u/12230408?v=4" width="100px;"/><br /><sub><b>Soo Jae Hwang</b></sub>](https://www.ossfinder.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=misoguy "Code") |
11311145
| [<img src="https://avatars0.githubusercontent.com/u/19773?v=4" width="100px;"/><br /><sub><b>Royston Shufflebotham</b></sub>](https://github.com/RoystonS)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3ARoystonS "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Tests") |
1146+
11321147
<!-- ALL-CONTRIBUTORS-LIST:END -->
11331148

11341149
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/config.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {configure, getConfig} from '../config'
2+
3+
describe('configuration API', () => {
4+
let originalConfig
5+
beforeEach(() => {
6+
// Grab the existing configuration so we can restore
7+
// it at the end of the test
8+
configure(existingConfig => {
9+
originalConfig = existingConfig
10+
// Don't change the existing config
11+
return {}
12+
})
13+
})
14+
afterEach(() => {
15+
configure(originalConfig)
16+
})
17+
18+
beforeEach(() => {
19+
configure({other: 123})
20+
})
21+
22+
describe('getConfig', () => {
23+
test('returns existing configuration', () => {
24+
const conf = getConfig()
25+
expect(conf.testIdAttribute).toEqual('data-testid')
26+
})
27+
})
28+
29+
describe('configure', () => {
30+
test('merges a delta rather than replacing the whole config', () => {
31+
configure({newKey: 123})
32+
const conf = getConfig()
33+
expect(conf).toMatchObject({
34+
newKey: 123,
35+
testIdAttribute: 'data-testid',
36+
})
37+
})
38+
39+
test('overrides existing values', () => {
40+
configure({testIdAttribute: 'new-id'})
41+
const conf = getConfig()
42+
expect(conf.testIdAttribute).toEqual('new-id')
43+
})
44+
45+
test('passes existing config out to config function', () => {
46+
// Create a new config key based on the value of an existing one
47+
configure(existingConfig => ({
48+
newKey: existingConfig.testIdAttribute + '-derived',
49+
}))
50+
const conf = getConfig()
51+
52+
// The new value should be there, and existing values should be
53+
// untouched
54+
expect(conf).toMatchObject({
55+
testIdAttribute: 'data-testid',
56+
newKey: 'data-testid-derived',
57+
})
58+
})
59+
})
60+
})

src/config.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ let config = {
55
testIdAttribute: 'data-testid',
66
}
77

8-
export function configure(configDelta) {
8+
export function configure(newConfig) {
9+
if (typeof newConfig === 'function') {
10+
// Pass the existing config out to the provided function
11+
// and accept a delta in return
12+
newConfig = newConfig(config)
13+
}
14+
15+
// Merge the incoming config delta
916
config = {
1017
...config,
11-
...configDelta,
18+
...newConfig,
1219
}
1320
}
14-
export function getTestIdAttribute() {
15-
return config.testIdAttribute
21+
22+
export function getConfig() {
23+
return config
1624
}

src/queries.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
queryAllByAttribute,
77
queryByAttribute,
88
} from './query-helpers'
9-
import {getTestIdAttribute} from './config'
9+
import {getConfig} from './config'
1010

1111
// Here are the queries for the library.
1212
// The queries here should only be things that are accessible to both users who are using a screen reader
@@ -149,6 +149,10 @@ function queryBySelectText(...args) {
149149
return firstResultOrNull(queryAllBySelectText, ...args)
150150
}
151151

152+
function getTestIdAttribute() {
153+
return getConfig().testIdAttribute
154+
}
155+
152156
const queryByPlaceholderText = queryByAttribute.bind(null, 'placeholder')
153157
const queryAllByPlaceholderText = queryAllByAttribute.bind(null, 'placeholder')
154158
const queryByTestId = (...args) =>

typings/config.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ export interface IConfig {
22
testIdAttribute: string
33
}
44

5-
export function configure(configDelta: Partial<IConfig>): void
5+
export interface IConfigFn {
6+
(existingConfig: IConfig): Partial<IConfig>
7+
}
8+
9+
export function configure(configDelta: Partial<IConfig> | IConfigFn): void

0 commit comments

Comments
 (0)