From 903eaaf06d849a1cbc0bdd48017acaba59f7a07a Mon Sep 17 00:00:00 2001 From: John Gozde Date: Tue, 21 Jan 2020 07:42:30 -0700 Subject: [PATCH] Add @testing-library/jest-dom (#37792) * Add @testing-library/jest-dom * Fix global test * Update jest-dom typings for (future) v5 - Remove module typings - Sync global typings with latest version Apply suggestions from code review Co-Authored-By: Sebastian Silbermann Co-authored-by: Sebastian Silbermann --- types/testing-library__jest-dom/index.d.ts | 36 ++++++++++ .../testing-library__jest-dom-global-tests.ts | 65 +++++++++++++++++++ types/testing-library__jest-dom/tsconfig.json | 23 +++++++ types/testing-library__jest-dom/tslint.json | 3 + 4 files changed, 127 insertions(+) create mode 100644 types/testing-library__jest-dom/index.d.ts create mode 100644 types/testing-library__jest-dom/test/testing-library__jest-dom-global-tests.ts create mode 100644 types/testing-library__jest-dom/tsconfig.json create mode 100644 types/testing-library__jest-dom/tslint.json diff --git a/types/testing-library__jest-dom/index.d.ts b/types/testing-library__jest-dom/index.d.ts new file mode 100644 index 0000000000..a6db284e9a --- /dev/null +++ b/types/testing-library__jest-dom/index.d.ts @@ -0,0 +1,36 @@ +// Type definitions for @testing-library/jest-dom 5.0 +// Project: https://github.com/testing-library/jest-dom +// Definitions by: Ernesto GarcĂ­a +// John Gozde +// Seth Macpherson +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +/// + +declare namespace jest { + interface Matchers { + /** + * @deprecated + */ + toBeInTheDOM(container?: HTMLElement | SVGElement): R; + toBeInTheDocument(): R; + toBeVisible(): R; + toBeEmpty(): R; + toBeDisabled(): R; + toBeEnabled(): R; + toBeInvalid(): R; + toBeRequired(): R; + toBeValid(): R; + toContainElement(element: HTMLElement | SVGElement | null): R; + toContainHTML(htmlText: string): R; + toHaveAttribute(attr: string, value?: unknown): R; + toHaveClass(...classNames: string[]): R; + toHaveFocus(): R; + toHaveFormValues(expectedValues: Record): R; + toHaveStyle(css: string): R; + toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R; + toHaveValue(value?: string | string[] | number): R; + toBeChecked(): R; + } +} diff --git a/types/testing-library__jest-dom/test/testing-library__jest-dom-global-tests.ts b/types/testing-library__jest-dom/test/testing-library__jest-dom-global-tests.ts new file mode 100644 index 0000000000..8dc0e3c137 --- /dev/null +++ b/types/testing-library__jest-dom/test/testing-library__jest-dom-global-tests.ts @@ -0,0 +1,65 @@ +const element: HTMLElement = document.body; + +expect(element).toBeInTheDOM(); +expect(element).toBeInTheDOM(document.body); +expect(element).toBeInTheDocument(); +expect(element).toBeVisible(); +expect(element).toBeEmpty(); +expect(element).toBeDisabled(); +expect(element).toBeEnabled(); +expect(element).toBeInvalid(); +expect(element).toBeRequired(); +expect(element).toBeValid(); +expect(element).toContainElement(document.body); +expect(element).toContainElement(null); +expect(element).toContainHTML('body'); +expect(element).toHaveAttribute('attr'); +expect(element).toHaveAttribute('attr', true); +expect(element).toHaveAttribute('attr', 'yes'); +expect(element).toHaveClass(); +expect(element).toHaveClass('cls1'); +expect(element).toHaveClass('cls1', 'cls2', 'cls3', 'cls4'); +expect(element).toHaveFocus(); +expect(element).toHaveFormValues({ foo: 'bar', baz: 1 }); +expect(element).toHaveStyle('display: block'); +expect(element).toHaveTextContent('Text'); +expect(element).toHaveTextContent(/Text/); +expect(element).toHaveTextContent('Text', { normalizeWhitespace: true }); +expect(element).toHaveTextContent(/Text/, { normalizeWhitespace: true }); +expect(element).toHaveValue(); +expect(element).toHaveValue('str'); +expect(element).toHaveValue(['str1', 'str2']); +expect(element).toHaveValue(1); +expect(element).toBeChecked(); + +expect(element).not.toBeInTheDOM(); +expect(element).not.toBeInTheDOM(document.body); +expect(element).not.toBeInTheDocument(); +expect(element).not.toBeVisible(); +expect(element).not.toBeEmpty(); +expect(element).not.toBeDisabled(); +expect(element).not.toBeEnabled(); +expect(element).not.toBeInvalid(); +expect(element).not.toBeRequired(); +expect(element).not.toBeValid(); +expect(element).not.toContainElement(document.body); +expect(element).not.toContainElement(null); +expect(element).not.toContainHTML('body'); +expect(element).not.toHaveAttribute('attr'); +expect(element).not.toHaveAttribute('attr', true); +expect(element).not.toHaveAttribute('attr', 'yes'); +expect(element).not.toHaveClass(); +expect(element).not.toHaveClass('cls1'); +expect(element).not.toHaveClass('cls1', 'cls2', 'cls3', 'cls4'); +expect(element).not.toHaveFocus(); +expect(element).not.toHaveFormValues({ foo: 'bar', baz: 1 }); +expect(element).not.toHaveStyle('display: block'); +expect(element).not.toHaveTextContent('Text'); +expect(element).not.toHaveTextContent(/Text/); +expect(element).not.toHaveTextContent('Text', { normalizeWhitespace: true }); +expect(element).not.toHaveTextContent(/Text/, { normalizeWhitespace: true }); +expect(element).not.toHaveValue(); +expect(element).not.toHaveValue('str'); +expect(element).not.toHaveValue(['str1', 'str2']); +expect(element).not.toHaveValue(1); +expect(element).not.toBeChecked(); diff --git a/types/testing-library__jest-dom/tsconfig.json b/types/testing-library__jest-dom/tsconfig.json new file mode 100644 index 0000000000..788d5893ab --- /dev/null +++ b/types/testing-library__jest-dom/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6", "dom"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "target": "es6", + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@testing-library/jest-dom": ["testing-library__jest-dom"] + } + }, + "files": [ + "index.d.ts", + "test/testing-library__jest-dom-global-tests.ts" + ] +} diff --git a/types/testing-library__jest-dom/tslint.json b/types/testing-library__jest-dom/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/testing-library__jest-dom/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}