Skip to content

Commit

Permalink
feat(TS): add typescript definitions (#37)
Browse files Browse the repository at this point in the history
* Add type definitions.

* Fixing dtslint issues. (Also making lint config match prettier)

* Adding myself as contributor
  • Loading branch information
maddijoyce authored and Kent C. Dodds committed May 8, 2018
1 parent b41b2e7 commit 56b768d
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Expand Up @@ -200,6 +200,15 @@
"contributions": [
"code"
]
},
{
"login": "maddijoyce",
"name": "Maddi Joyce",
"avatar_url": "https://avatars2.githubusercontent.com/u/2224291?v=4",
"profile": "http://www.maddijoyce.com",
"contributions": [
"code"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -16,7 +16,7 @@
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]

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

Expand Down Expand Up @@ -862,7 +862,7 @@ Thanks goes to these people ([emoji key][emojis]):
| [<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") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<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") |
| [<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://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") |

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

Expand Down
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0-semantically-released",
"description": "Simple and complete DOM testing utilities that encourage good testing practices.",
"main": "dist/index.js",
"typings": "typings",
"keywords": [
"testing",
"ui",
Expand All @@ -27,10 +28,12 @@
"test:update": "npm test -- --updateSnapshot --coverage",
"validate": "kcd-scripts validate",
"setup": "npm install && npm run validate -s",
"precommit": "kcd-scripts precommit"
"precommit": "kcd-scripts precommit",
"dtslint": "dtslint typings"
},
"files": [
"dist",
"typings",
"extend-expect.js"
],
"dependencies": {
Expand All @@ -40,6 +43,7 @@
"wait-for-expect": "^0.4.0"
},
"devDependencies": {
"dtslint": "^0.3.0",
"jest-in-case": "^1.0.2",
"kcd-scripts": "^0.37.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/events.js
Expand Up @@ -19,7 +19,7 @@ const {
const eventMap = {
// Clipboard Events
copy: {
EventType: CompositionEvent,
EventType: ClipboardEvent,
defaultInit: {bubbles: true, cancelable: true},
},
cut: {
Expand Down
15 changes: 15 additions & 0 deletions typings/bind-element-to-queries.d.ts
@@ -0,0 +1,15 @@
import {Matcher} from 'matches'
import * as queries from './queries'

export type BoundFunction<T> = T extends (
a1: any,
text: infer P,
options: infer Q,
) => infer R
? (text: P, options?: Q) => R
: never
export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}

export function bindElementToQueries(
element: HTMLElement,
): BoundFunctions<typeof queries>
77 changes: 77 additions & 0 deletions typings/events.d.ts
@@ -0,0 +1,77 @@
export type EventType =
| 'copy'
| 'cut'
| 'paste'
| 'compositionEnd'
| 'compositionStart'
| 'compositionUpdate'
| 'keyDown'
| 'keyPress'
| 'keyUp'
| 'focus'
| 'blur'
| 'change'
| 'input'
| 'invalid'
| 'submit'
| 'click'
| 'contextMenu'
| 'dblClick'
| 'drag'
| 'dragEnd'
| 'dragEnter'
| 'dragExit'
| 'dragLeave'
| 'dragOver'
| 'dragStart'
| 'drop'
| 'mouseDown'
| 'mouseEnter'
| 'mouseLeave'
| 'mouseMove'
| 'mouseOut'
| 'mouseOver'
| 'mouseUp'
| 'select'
| 'touchCancel'
| 'touchEnd'
| 'touchMove'
| 'touchStart'
| 'scroll'
| 'wheel'
| 'abort'
| 'canPlay'
| 'canPlayThrough'
| 'durationChange'
| 'emptied'
| 'encrypted'
| 'ended'
| 'loadedData'
| 'loadedMetadata'
| 'loadStart'
| 'pause'
| 'play'
| 'playing'
| 'progress'
| 'rateChange'
| 'seeked'
| 'seeking'
| 'stalled'
| 'suspend'
| 'timeUpdate'
| 'volumeChange'
| 'waiting'
| 'load'
| 'error'
| 'animationStart'
| 'animationEnd'
| 'animationIteration'
| 'transitionEnd'
| 'doubleClick'

export type FireFunction = (element: HTMLElement, event: Event) => boolean
export type FireObject = {
[K in EventType]: (element: HTMLElement, options?: {}) => boolean
}

export const fireEvent: FireFunction & FireObject
1 change: 1 addition & 0 deletions typings/get-node-text.d.ts
@@ -0,0 +1 @@
export function getNodeText(node: HTMLElement): string
13 changes: 13 additions & 0 deletions typings/index.d.ts
@@ -0,0 +1,13 @@
// TypeScript Version: 2.8
import * as queries from './queries'

export {queries}

export * from './queries'
export * from './wait'
export * from './wait-for-element'
export * from './matches'
export * from './get-node-text'
export * from './events'
export * from './bind-element-to-queries'
export * from './pretty-dom'
17 changes: 17 additions & 0 deletions typings/matches.d.ts
@@ -0,0 +1,17 @@
export type MatcherFunction = (content: string, element: HTMLElement) => boolean
export type Matcher = string | RegExp | MatcherFunction
export interface MatcherOptions {
exact?: boolean
trim?: boolean
collapseWhitespace?: boolean
}

export type Match = (
textToMatch: string,
node: HTMLElement | null,
matcher: Matcher,
options?: MatcherOptions,
) => boolean

export const fuzzyMatches: Match
export const matches: Match
4 changes: 4 additions & 0 deletions typings/pretty-dom.d.ts
@@ -0,0 +1,4 @@
export function prettyDOM(
element: HTMLElement,
maxLength?: number,
): string | false
66 changes: 66 additions & 0 deletions typings/queries.d.ts
@@ -0,0 +1,66 @@
import {Matcher, MatcherOptions} from './matches'

export interface SelectorMatcherOptions extends MatcherOptions {
selector?: string
}

export type QueryByAttribute = (
container: HTMLElement,
id: Matcher,
options?: MatcherOptions,
) => HTMLElement | null

export type AllByAttribute = (
container: HTMLElement,
id: Matcher,
options?: MatcherOptions,
) => [HTMLElement]

export type GetByAttribute = (
container: HTMLElement,
id: Matcher,
options?: MatcherOptions,
) => HTMLElement

export type QueryByText = (
container: HTMLElement,
id: Matcher,
options?: SelectorMatcherOptions,
) => HTMLElement | null

export type AllByText = (
container: HTMLElement,
id: Matcher,
options?: SelectorMatcherOptions,
) => [HTMLElement]

export type GetByText = (
container: HTMLElement,
id: Matcher,
options?: SelectorMatcherOptions,
) => HTMLElement

export const queryByPlaceholderText: QueryByAttribute
export const queryAllByPlaceholderText: AllByAttribute
export const getByPlaceholderText: GetByAttribute
export const getAllByPlaceholderText: AllByAttribute
export const queryByText: QueryByText
export const queryAllByText: AllByText
export const getByText: GetByText
export const getAllByText: AllByText
export const queryByLabelText: QueryByText
export const queryAllByLabelText: AllByText
export const getByLabelText: GetByText
export const getAllByLabelText: AllByText
export const queryByAltText: QueryByAttribute
export const queryAllByAltText: AllByAttribute
export const getByAltText: GetByAttribute
export const getAllByAltText: AllByAttribute
export const queryByTestId: QueryByAttribute
export const queryAllByTestId: AllByAttribute
export const getByTestId: GetByAttribute
export const getAllByTestId: AllByAttribute
export const queryByTitle: QueryByAttribute
export const queryAllByTitle: AllByAttribute
export const getByTitle: GetByAttribute
export const getAllByTitle: AllByAttribute
14 changes: 14 additions & 0 deletions typings/tsconfig.json
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"noEmit": true,

"baseUrl": ".",
"paths": {"dom-testing-library": ["."]}
}
}
7 changes: 7 additions & 0 deletions typings/tslint.json
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": [true, "never"],
"whitespace": [false]
}
}
8 changes: 8 additions & 0 deletions typings/wait-for-element.d.ts
@@ -0,0 +1,8 @@
export function waitForElement<T>(
callback?: () => T,
options?: {
container?: HTMLElement
timeout?: number
mutationObserverOptions?: MutationObserverInit
},
): Promise<T | undefined>
7 changes: 7 additions & 0 deletions typings/wait.d.ts
@@ -0,0 +1,7 @@
export function wait(
callback?: () => void,
options?: {
timeout?: number
interval?: number
},
): Promise<void>

0 comments on commit 56b768d

Please sign in to comment.