From 060b83db55c965c329c79b8e67562eaac5da4bfd Mon Sep 17 00:00:00 2001 From: Maddi Joyce Date: Tue, 8 May 2018 15:41:34 +1000 Subject: [PATCH 1/3] Add type definitions. --- package.json | 6 ++- src/events.js | 2 +- typings/bind-element-to-queries.d.ts | 15 ++++++ typings/events.d.ts | 77 ++++++++++++++++++++++++++++ typings/get-node-text.d.ts | 1 + typings/index.d.ts | 12 +++++ typings/matches.d.ts | 17 ++++++ typings/pretty-dom.d.ts | 4 ++ typings/queries.d.ts | 66 ++++++++++++++++++++++++ typings/tsconfig.json | 14 +++++ typings/tslint.json | 3 ++ typings/wait-for-element.d.ts | 8 +++ typings/wait.d.ts | 7 +++ 13 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 typings/bind-element-to-queries.d.ts create mode 100644 typings/events.d.ts create mode 100644 typings/get-node-text.d.ts create mode 100644 typings/index.d.ts create mode 100644 typings/matches.d.ts create mode 100644 typings/pretty-dom.d.ts create mode 100644 typings/queries.d.ts create mode 100644 typings/tsconfig.json create mode 100644 typings/tslint.json create mode 100644 typings/wait-for-element.d.ts create mode 100644 typings/wait.d.ts diff --git a/package.json b/package.json index b3867852..2d8e0333 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { @@ -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" }, diff --git a/src/events.js b/src/events.js index ad5a8e33..c9adc34e 100644 --- a/src/events.js +++ b/src/events.js @@ -19,7 +19,7 @@ const { const eventMap = { // Clipboard Events copy: { - EventType: CompositionEvent, + EventType: ClipboardEvent, defaultInit: {bubbles: true, cancelable: true}, }, cut: { diff --git a/typings/bind-element-to-queries.d.ts b/typings/bind-element-to-queries.d.ts new file mode 100644 index 00000000..afe69b72 --- /dev/null +++ b/typings/bind-element-to-queries.d.ts @@ -0,0 +1,15 @@ +import {Matcher} from 'matches' +import * as queries from './queries' + +type BoundFunction = T extends ( + a1: any, + text: infer P, + options: infer Q, +) => infer R + ? (text: P, options?: Q) => R + : never +type BoundFunctions = {[P in keyof T]: BoundFunction} + +export const bindElementToQueries: ( + element: HTMLElement, +) => BoundFunctions diff --git a/typings/events.d.ts b/typings/events.d.ts new file mode 100644 index 00000000..59574b2a --- /dev/null +++ b/typings/events.d.ts @@ -0,0 +1,77 @@ +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' + +type FireFunction = (element: HTMLElement, event: Event) => boolean +type FireObject = { + [K in EventType]: (element: HTMLElement, options?: {}) => boolean +} + +export const fireEvent: FireFunction & FireObject diff --git a/typings/get-node-text.d.ts b/typings/get-node-text.d.ts new file mode 100644 index 00000000..4c45b691 --- /dev/null +++ b/typings/get-node-text.d.ts @@ -0,0 +1 @@ +export const getNodeText: (node: HTMLElement) => string diff --git a/typings/index.d.ts b/typings/index.d.ts new file mode 100644 index 00000000..de3d1d28 --- /dev/null +++ b/typings/index.d.ts @@ -0,0 +1,12 @@ +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' diff --git a/typings/matches.d.ts b/typings/matches.d.ts new file mode 100644 index 00000000..af89af82 --- /dev/null +++ b/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 diff --git a/typings/pretty-dom.d.ts b/typings/pretty-dom.d.ts new file mode 100644 index 00000000..00d2dc70 --- /dev/null +++ b/typings/pretty-dom.d.ts @@ -0,0 +1,4 @@ +export const prettyDOM: ( + element: HTMLElement, + maxLength?: number, +) => string | false diff --git a/typings/queries.d.ts b/typings/queries.d.ts new file mode 100644 index 00000000..2d26bd3f --- /dev/null +++ b/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 diff --git a/typings/tsconfig.json b/typings/tsconfig.json new file mode 100644 index 00000000..81e4f72b --- /dev/null +++ b/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": ["."]} + } +} diff --git a/typings/tslint.json b/typings/tslint.json new file mode 100644 index 00000000..6490e53e --- /dev/null +++ b/typings/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dtslint.json" +} diff --git a/typings/wait-for-element.d.ts b/typings/wait-for-element.d.ts new file mode 100644 index 00000000..ad5b14b7 --- /dev/null +++ b/typings/wait-for-element.d.ts @@ -0,0 +1,8 @@ +export function waitForElement( + callback?: () => T, + options?: { + container?: HTMLElement + timeout?: number + mutationObserverOptions?: MutationObserverInit + }, +): Promise diff --git a/typings/wait.d.ts b/typings/wait.d.ts new file mode 100644 index 00000000..cb102637 --- /dev/null +++ b/typings/wait.d.ts @@ -0,0 +1,7 @@ +export function wait( + callback?: () => void, + options?: { + timeout?: number + interval?: number + }, +): Promise From f9537bdfe788c5f7b7c2bfaa6c597e642a7fe146 Mon Sep 17 00:00:00 2001 From: Maddi Joyce Date: Tue, 8 May 2018 16:04:31 +1000 Subject: [PATCH 2/3] Fixing dtslint issues. (Also making lint config match prettier) --- typings/bind-element-to-queries.d.ts | 8 ++++---- typings/events.d.ts | 6 +++--- typings/get-node-text.d.ts | 2 +- typings/index.d.ts | 1 + typings/pretty-dom.d.ts | 4 ++-- typings/tslint.json | 6 +++++- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/typings/bind-element-to-queries.d.ts b/typings/bind-element-to-queries.d.ts index afe69b72..432b638c 100644 --- a/typings/bind-element-to-queries.d.ts +++ b/typings/bind-element-to-queries.d.ts @@ -1,15 +1,15 @@ import {Matcher} from 'matches' import * as queries from './queries' -type BoundFunction = T extends ( +export type BoundFunction = T extends ( a1: any, text: infer P, options: infer Q, ) => infer R ? (text: P, options?: Q) => R : never -type BoundFunctions = {[P in keyof T]: BoundFunction} +export type BoundFunctions = {[P in keyof T]: BoundFunction} -export const bindElementToQueries: ( +export function bindElementToQueries( element: HTMLElement, -) => BoundFunctions +): BoundFunctions diff --git a/typings/events.d.ts b/typings/events.d.ts index 59574b2a..a11bd3c7 100644 --- a/typings/events.d.ts +++ b/typings/events.d.ts @@ -1,4 +1,4 @@ -type EventType = +export type EventType = | 'copy' | 'cut' | 'paste' @@ -69,8 +69,8 @@ type EventType = | 'transitionEnd' | 'doubleClick' -type FireFunction = (element: HTMLElement, event: Event) => boolean -type FireObject = { +export type FireFunction = (element: HTMLElement, event: Event) => boolean +export type FireObject = { [K in EventType]: (element: HTMLElement, options?: {}) => boolean } diff --git a/typings/get-node-text.d.ts b/typings/get-node-text.d.ts index 4c45b691..3f01b629 100644 --- a/typings/get-node-text.d.ts +++ b/typings/get-node-text.d.ts @@ -1 +1 @@ -export const getNodeText: (node: HTMLElement) => string +export function getNodeText(node: HTMLElement): string diff --git a/typings/index.d.ts b/typings/index.d.ts index de3d1d28..44f58402 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,3 +1,4 @@ +// TypeScript Version: 2.8 import * as queries from './queries' export {queries} diff --git a/typings/pretty-dom.d.ts b/typings/pretty-dom.d.ts index 00d2dc70..23738431 100644 --- a/typings/pretty-dom.d.ts +++ b/typings/pretty-dom.d.ts @@ -1,4 +1,4 @@ -export const prettyDOM: ( +export function prettyDOM( element: HTMLElement, maxLength?: number, -) => string | false +): string | false diff --git a/typings/tslint.json b/typings/tslint.json index 6490e53e..286a004f 100644 --- a/typings/tslint.json +++ b/typings/tslint.json @@ -1,3 +1,7 @@ { - "extends": "dtslint/dtslint.json" + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": [true, "never"], + "whitespace": [false] + } } From 6921685392bb3db1099e31c784f946701fd6c7d7 Mon Sep 17 00:00:00 2001 From: Maddi Joyce Date: Tue, 8 May 2018 16:07:58 +1000 Subject: [PATCH 3/3] Adding myself as contributor --- .all-contributorsrc | 9 +++++++++ README.md | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 5d5a66b6..d984844d 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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" + ] } ] } diff --git a/README.md b/README.md index 59650138..ace845d5 100644 --- a/README.md +++ b/README.md @@ -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] @@ -841,7 +841,7 @@ Thanks goes to these people ([emoji key][emojis]): | [
Kent C. Dodds](https://kentcdodds.com)
[πŸ’»](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") | [
Ryan Castner](http://audiolion.github.io)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [
Daniel Sandiego](https://www.dnlsandiego.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [
PaweΕ‚ MikoΕ‚ajczyk](https://github.com/Miklet)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [
Alejandro ÑÑñez Ortiz](http://co.linkedin.com/in/alejandronanez/)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [
Matt Parrish](https://github.com/pbomb)
[πŸ›](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") | [
Justin Hall](https://github.com/wKovacs64)
[πŸ“¦](#platform-wKovacs64 "Packaging/porting to new platform") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [
Anto Aravinth](https://github.com/antoaravinth)
[πŸ’»](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") | [
Jonah Moses](https://github.com/JonahMoses)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [
Łukasz Gandecki](http://team.thebrain.pro)
[πŸ’»](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") | [
Ivan Babak](https://sompylasar.github.io)
[πŸ›](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") | [
Jesse Day](https://github.com/jday3)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [
Ernesto GarcΓ­a](http://gnapse.github.io)
[πŸ’¬](#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") | [
Josef Maxx Blake](http://jomaxx.com)
[πŸ’»](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") | -| [
Alex Cook](https://github.com/alecook)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [πŸ’‘](#example-alecook "Examples") | [
Daniel Cook](https://github.com/dfcook)
[πŸ’»](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") | [
Thomas Chia](https://github.com/thchia)
[πŸ›](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [
Tim Deschryver](https://github.com/tdeschryver)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [
Alex Krolick](https://alexkrolick.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | +| [
Alex Cook](https://github.com/alecook)
[πŸ“–](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [πŸ’‘](#example-alecook "Examples") | [
Daniel Cook](https://github.com/dfcook)
[πŸ’»](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") | [
Thomas Chia](https://github.com/thchia)
[πŸ›](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [
Tim Deschryver](https://github.com/tdeschryver)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [
Alex Krolick](https://alexkrolick.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [
Maddi Joyce](http://www.maddijoyce.com)
[πŸ’»](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") |