Skip to content

Commit 56b768d

Browse files
maddijoyceKent C. Dodds
authored and
Kent C. Dodds
committed
feat(TS): add typescript definitions (#37)
* Add type definitions. * Fixing dtslint issues. (Also making lint config match prettier) * Adding myself as contributor
1 parent b41b2e7 commit 56b768d

15 files changed

+246
-4
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@
200200
"contributions": [
201201
"code"
202202
]
203+
},
204+
{
205+
"login": "maddijoyce",
206+
"name": "Maddi Joyce",
207+
"avatar_url": "https://avatars2.githubusercontent.com/u/2224291?v=4",
208+
"profile": "http://www.maddijoyce.com",
209+
"contributions": [
210+
"code"
211+
]
203212
}
204213
]
205214
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -862,7 +862,7 @@ Thanks goes to these people ([emoji key][emojis]):
862862
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
863863
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
864864
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Tests") |
865-
| [<img src="https://avatars3.githubusercontent.com/u/725236?v=4" width="100px;"/><br /><sub><b>Alex Cook</b></sub>](https://github.com/alecook)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [💡](#example-alecook "Examples") | [<img src="https://avatars3.githubusercontent.com/u/10348212?v=4" width="100px;"/><br /><sub><b>Daniel Cook</b></sub>](https://github.com/dfcook)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [<img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [<img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;"/><br /><sub><b>Tim Deschryver</b></sub>](https://github.com/tdeschryver)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") |
865+
| [<img src="https://avatars3.githubusercontent.com/u/725236?v=4" width="100px;"/><br /><sub><b>Alex Cook</b></sub>](https://github.com/alecook)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [💡](#example-alecook "Examples") | [<img src="https://avatars3.githubusercontent.com/u/10348212?v=4" width="100px;"/><br /><sub><b>Daniel Cook</b></sub>](https://github.com/dfcook)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [<img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [<img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;"/><br /><sub><b>Tim Deschryver</b></sub>](https://github.com/tdeschryver)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") |
866866

867867
<!-- ALL-CONTRIBUTORS-LIST:END -->
868868

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0-semantically-released",
44
"description": "Simple and complete DOM testing utilities that encourage good testing practices.",
55
"main": "dist/index.js",
6+
"typings": "typings",
67
"keywords": [
78
"testing",
89
"ui",
@@ -27,10 +28,12 @@
2728
"test:update": "npm test -- --updateSnapshot --coverage",
2829
"validate": "kcd-scripts validate",
2930
"setup": "npm install && npm run validate -s",
30-
"precommit": "kcd-scripts precommit"
31+
"precommit": "kcd-scripts precommit",
32+
"dtslint": "dtslint typings"
3133
},
3234
"files": [
3335
"dist",
36+
"typings",
3437
"extend-expect.js"
3538
],
3639
"dependencies": {
@@ -40,6 +43,7 @@
4043
"wait-for-expect": "^0.4.0"
4144
},
4245
"devDependencies": {
46+
"dtslint": "^0.3.0",
4347
"jest-in-case": "^1.0.2",
4448
"kcd-scripts": "^0.37.0"
4549
},

src/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {
1919
const eventMap = {
2020
// Clipboard Events
2121
copy: {
22-
EventType: CompositionEvent,
22+
EventType: ClipboardEvent,
2323
defaultInit: {bubbles: true, cancelable: true},
2424
},
2525
cut: {

typings/bind-element-to-queries.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Matcher} from 'matches'
2+
import * as queries from './queries'
3+
4+
export type BoundFunction<T> = T extends (
5+
a1: any,
6+
text: infer P,
7+
options: infer Q,
8+
) => infer R
9+
? (text: P, options?: Q) => R
10+
: never
11+
export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}
12+
13+
export function bindElementToQueries(
14+
element: HTMLElement,
15+
): BoundFunctions<typeof queries>

typings/events.d.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export type EventType =
2+
| 'copy'
3+
| 'cut'
4+
| 'paste'
5+
| 'compositionEnd'
6+
| 'compositionStart'
7+
| 'compositionUpdate'
8+
| 'keyDown'
9+
| 'keyPress'
10+
| 'keyUp'
11+
| 'focus'
12+
| 'blur'
13+
| 'change'
14+
| 'input'
15+
| 'invalid'
16+
| 'submit'
17+
| 'click'
18+
| 'contextMenu'
19+
| 'dblClick'
20+
| 'drag'
21+
| 'dragEnd'
22+
| 'dragEnter'
23+
| 'dragExit'
24+
| 'dragLeave'
25+
| 'dragOver'
26+
| 'dragStart'
27+
| 'drop'
28+
| 'mouseDown'
29+
| 'mouseEnter'
30+
| 'mouseLeave'
31+
| 'mouseMove'
32+
| 'mouseOut'
33+
| 'mouseOver'
34+
| 'mouseUp'
35+
| 'select'
36+
| 'touchCancel'
37+
| 'touchEnd'
38+
| 'touchMove'
39+
| 'touchStart'
40+
| 'scroll'
41+
| 'wheel'
42+
| 'abort'
43+
| 'canPlay'
44+
| 'canPlayThrough'
45+
| 'durationChange'
46+
| 'emptied'
47+
| 'encrypted'
48+
| 'ended'
49+
| 'loadedData'
50+
| 'loadedMetadata'
51+
| 'loadStart'
52+
| 'pause'
53+
| 'play'
54+
| 'playing'
55+
| 'progress'
56+
| 'rateChange'
57+
| 'seeked'
58+
| 'seeking'
59+
| 'stalled'
60+
| 'suspend'
61+
| 'timeUpdate'
62+
| 'volumeChange'
63+
| 'waiting'
64+
| 'load'
65+
| 'error'
66+
| 'animationStart'
67+
| 'animationEnd'
68+
| 'animationIteration'
69+
| 'transitionEnd'
70+
| 'doubleClick'
71+
72+
export type FireFunction = (element: HTMLElement, event: Event) => boolean
73+
export type FireObject = {
74+
[K in EventType]: (element: HTMLElement, options?: {}) => boolean
75+
}
76+
77+
export const fireEvent: FireFunction & FireObject

typings/get-node-text.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function getNodeText(node: HTMLElement): string

typings/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TypeScript Version: 2.8
2+
import * as queries from './queries'
3+
4+
export {queries}
5+
6+
export * from './queries'
7+
export * from './wait'
8+
export * from './wait-for-element'
9+
export * from './matches'
10+
export * from './get-node-text'
11+
export * from './events'
12+
export * from './bind-element-to-queries'
13+
export * from './pretty-dom'

typings/matches.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export type MatcherFunction = (content: string, element: HTMLElement) => boolean
2+
export type Matcher = string | RegExp | MatcherFunction
3+
export interface MatcherOptions {
4+
exact?: boolean
5+
trim?: boolean
6+
collapseWhitespace?: boolean
7+
}
8+
9+
export type Match = (
10+
textToMatch: string,
11+
node: HTMLElement | null,
12+
matcher: Matcher,
13+
options?: MatcherOptions,
14+
) => boolean
15+
16+
export const fuzzyMatches: Match
17+
export const matches: Match

typings/pretty-dom.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function prettyDOM(
2+
element: HTMLElement,
3+
maxLength?: number,
4+
): string | false

typings/queries.d.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {Matcher, MatcherOptions} from './matches'
2+
3+
export interface SelectorMatcherOptions extends MatcherOptions {
4+
selector?: string
5+
}
6+
7+
export type QueryByAttribute = (
8+
container: HTMLElement,
9+
id: Matcher,
10+
options?: MatcherOptions,
11+
) => HTMLElement | null
12+
13+
export type AllByAttribute = (
14+
container: HTMLElement,
15+
id: Matcher,
16+
options?: MatcherOptions,
17+
) => [HTMLElement]
18+
19+
export type GetByAttribute = (
20+
container: HTMLElement,
21+
id: Matcher,
22+
options?: MatcherOptions,
23+
) => HTMLElement
24+
25+
export type QueryByText = (
26+
container: HTMLElement,
27+
id: Matcher,
28+
options?: SelectorMatcherOptions,
29+
) => HTMLElement | null
30+
31+
export type AllByText = (
32+
container: HTMLElement,
33+
id: Matcher,
34+
options?: SelectorMatcherOptions,
35+
) => [HTMLElement]
36+
37+
export type GetByText = (
38+
container: HTMLElement,
39+
id: Matcher,
40+
options?: SelectorMatcherOptions,
41+
) => HTMLElement
42+
43+
export const queryByPlaceholderText: QueryByAttribute
44+
export const queryAllByPlaceholderText: AllByAttribute
45+
export const getByPlaceholderText: GetByAttribute
46+
export const getAllByPlaceholderText: AllByAttribute
47+
export const queryByText: QueryByText
48+
export const queryAllByText: AllByText
49+
export const getByText: GetByText
50+
export const getAllByText: AllByText
51+
export const queryByLabelText: QueryByText
52+
export const queryAllByLabelText: AllByText
53+
export const getByLabelText: GetByText
54+
export const getAllByLabelText: AllByText
55+
export const queryByAltText: QueryByAttribute
56+
export const queryAllByAltText: AllByAttribute
57+
export const getByAltText: GetByAttribute
58+
export const getAllByAltText: AllByAttribute
59+
export const queryByTestId: QueryByAttribute
60+
export const queryAllByTestId: AllByAttribute
61+
export const getByTestId: GetByAttribute
62+
export const getAllByTestId: AllByAttribute
63+
export const queryByTitle: QueryByAttribute
64+
export const queryAllByTitle: AllByAttribute
65+
export const getByTitle: GetByAttribute
66+
export const getAllByTitle: AllByAttribute

typings/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["es6", "dom"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictFunctionTypes": true,
8+
"strictNullChecks": true,
9+
"noEmit": true,
10+
11+
"baseUrl": ".",
12+
"paths": {"dom-testing-library": ["."]}
13+
}
14+
}

typings/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": [true, "never"],
5+
"whitespace": [false]
6+
}
7+
}

typings/wait-for-element.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function waitForElement<T>(
2+
callback?: () => T,
3+
options?: {
4+
container?: HTMLElement
5+
timeout?: number
6+
mutationObserverOptions?: MutationObserverInit
7+
},
8+
): Promise<T | undefined>

typings/wait.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function wait(
2+
callback?: () => void,
3+
options?: {
4+
timeout?: number
5+
interval?: number
6+
},
7+
): Promise<void>

0 commit comments

Comments
 (0)