From 15cfb4475f9a9cf6d7b72478c34938edc2d412f6 Mon Sep 17 00:00:00 2001 From: Matej Snuderl Date: Thu, 7 May 2020 16:24:35 +0200 Subject: [PATCH 1/8] feat: native typescript support --- .gitignore | 1 + .vscode/settings.json | 3 + package.json | 15 ++- src/__tests__/helpers/document.js | 8 -- src/__tests__/helpers/document.ts | 15 +++ src/__tests__/helpers/test-utils.js | 11 -- src/__tests__/helpers/test-utils.ts | 20 ++++ .../{to-be-checked.js => to-be-checked.ts} | 0 .../{to-be-disabled.js => to-be-disabled.ts} | 0 .../{to-be-empty.js => to-be-empty.ts} | 0 ...e-document.js => to-be-in-the-document.ts} | 0 ...o-be-in-the-dom.js => to-be-in-the-dom.ts} | 1 + .../{to-be-invalid.js => to-be-invalid.ts} | 2 +- .../{to-be-required.js => to-be-required.ts} | 0 .../{to-be-visible.js => to-be-visible.ts} | 56 +++++----- ...ntain-element.js => to-contain-element.ts} | 3 + ...{to-contain-html.js => to-contain-html.ts} | 5 + ...have-attribute.js => to-have-attribute.ts} | 1 + .../{to-have-class.js => to-have-class.ts} | 0 ...play-value.js => to-have-display-value.ts} | 52 ++++----- .../{to-have-focus.js => to-have-focus.ts} | 2 +- ...-form-values.js => to-have-form-values.ts} | 65 +++++++++-- .../{to-have-style.js => to-have-style.ts} | 1 + ...ext-content.js => to-have-text-content.ts} | 0 .../{to-have-value.js => to-have-value.ts} | 24 +++-- src/__tests__/{utils.js => utils.ts} | 9 ++ src/extend-expect.js | 3 - src/extend-expect.ts | 11 ++ src/{index.js => index.ts} | 0 src/{matchers.js => matchers.ts} | 0 src/{to-be-checked.js => to-be-checked.ts} | 21 ++-- src/{to-be-disabled.js => to-be-disabled.ts} | 37 +++++-- src/{to-be-empty.js => to-be-empty.ts} | 6 +- ...e-document.js => to-be-in-the-document.ts} | 13 ++- ...o-be-in-the-dom.js => to-be-in-the-dom.ts} | 15 ++- src/{to-be-invalid.js => to-be-invalid.ts} | 20 ++-- src/{to-be-required.js => to-be-required.ts} | 20 ++-- src/{to-be-visible.js => to-be-visible.ts} | 29 +++-- ...ntain-element.js => to-contain-element.ts} | 13 ++- ...{to-contain-html.js => to-contain-html.ts} | 11 +- ...have-attribute.js => to-have-attribute.ts} | 21 ++-- src/{to-have-class.js => to-have-class.ts} | 27 ++--- src/to-have-display-value.js | 44 -------- src/to-have-display-value.ts | 58 ++++++++++ src/to-have-focus.js | 20 ---- src/to-have-focus.ts | 27 +++++ ...-form-values.js => to-have-form-values.ts} | 33 ++++-- src/{to-have-style.js => to-have-style.ts} | 45 +++++--- src/to-have-text-content.js | 36 ------- src/to-have-text-content.ts | 45 ++++++++ src/{to-have-value.js => to-have-value.ts} | 18 ++-- src/types.ts | 4 + src/{utils.js => utils.ts} | 101 ++++++++++++------ tests/jest.config.dom.js | 4 + tests/jest.config.node.js | 4 + tests/setup-env.d.ts | 1 + tests/{setup-env.js => setup-env.ts} | 0 tsconfig.build.json | 7 ++ tsconfig.json | 10 ++ 59 files changed, 663 insertions(+), 335 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 src/__tests__/helpers/document.js create mode 100644 src/__tests__/helpers/document.ts delete mode 100644 src/__tests__/helpers/test-utils.js create mode 100644 src/__tests__/helpers/test-utils.ts rename src/__tests__/{to-be-checked.js => to-be-checked.ts} (100%) rename src/__tests__/{to-be-disabled.js => to-be-disabled.ts} (100%) rename src/__tests__/{to-be-empty.js => to-be-empty.ts} (100%) rename src/__tests__/{to-be-in-the-document.js => to-be-in-the-document.ts} (100%) rename src/__tests__/{to-be-in-the-dom.js => to-be-in-the-dom.ts} (99%) rename src/__tests__/{to-be-invalid.js => to-be-invalid.ts} (98%) rename src/__tests__/{to-be-required.js => to-be-required.ts} (100%) rename src/__tests__/{to-be-visible.js => to-be-visible.ts} (75%) rename src/__tests__/{to-contain-element.js => to-contain-element.ts} (97%) rename src/__tests__/{to-contain-html.js => to-contain-html.ts} (97%) rename src/__tests__/{to-have-attribute.js => to-have-attribute.ts} (99%) rename src/__tests__/{to-have-class.js => to-have-class.ts} (100%) rename src/__tests__/{to-have-display-value.js => to-have-display-value.ts} (66%) rename src/__tests__/{to-have-focus.js => to-have-focus.ts} (89%) rename src/__tests__/{to-have-form-values.js => to-have-form-values.ts} (89%) rename src/__tests__/{to-have-style.js => to-have-style.ts} (99%) rename src/__tests__/{to-have-text-content.js => to-have-text-content.ts} (100%) rename src/__tests__/{to-have-value.js => to-have-value.ts} (92%) rename src/__tests__/{utils.js => utils.ts} (93%) delete mode 100644 src/extend-expect.js create mode 100644 src/extend-expect.ts rename src/{index.js => index.ts} (100%) rename src/{matchers.js => matchers.ts} (100%) rename src/{to-be-checked.js => to-be-checked.ts} (67%) rename src/{to-be-disabled.js => to-be-disabled.ts} (64%) rename src/{to-be-empty.js => to-be-empty.ts} (60%) rename src/{to-be-in-the-document.js => to-be-in-the-document.ts} (61%) rename src/{to-be-in-the-dom.js => to-be-in-the-dom.ts} (63%) rename src/{to-be-invalid.js => to-be-invalid.ts} (66%) rename src/{to-be-required.js => to-be-required.ts} (76%) rename src/{to-be-visible.js => to-be-visible.ts} (57%) rename src/{to-contain-element.js => to-contain-element.ts} (63%) rename src/{to-contain-html.js => to-contain-html.ts} (52%) rename src/{to-have-attribute.js => to-have-attribute.ts} (67%) rename src/{to-have-class.js => to-have-class.ts} (75%) delete mode 100644 src/to-have-display-value.js create mode 100644 src/to-have-display-value.ts delete mode 100644 src/to-have-focus.js create mode 100644 src/to-have-focus.ts rename src/{to-have-form-values.js => to-have-form-values.ts} (71%) rename src/{to-have-style.js => to-have-style.ts} (58%) delete mode 100644 src/to-have-text-content.js create mode 100644 src/to-have-text-content.ts rename src/{to-have-value.js => to-have-value.ts} (70%) create mode 100644 src/types.ts rename src/{utils.js => utils.ts} (63%) create mode 100644 tests/setup-env.d.ts rename tests/{setup-env.js => setup-env.ts} (100%) create mode 100644 tsconfig.build.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 8e0c70cb..8486e171 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ dist # when working with contributors package-lock.json yarn.lock +yarn-error.log diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..25fa6215 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} diff --git a/package.json b/package.json index a56c0825..2e597bf1 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,16 @@ "version": "0.0.0-semantically-released", "description": "Custom jest matchers to test the state of the DOM", "main": "dist/index.js", + "types": "dist/index.d.ts", "engines": { "node": ">=8", "npm": ">=6", "yarn": ">=1" }, "scripts": { - "build": "kcd-scripts build", + "build": "npm run build:source && npm run build:types", + "build:source": "kcd-scripts build", + "build:types": "tsc -p tsconfig.build.json --emitDeclarationOnly", "format": "kcd-scripts format", "lint": "kcd-scripts lint", "setup": "npm install && npm run validate -s", @@ -32,8 +35,7 @@ "license": "MIT", "dependencies": { "@babel/runtime": "^7.9.2", - "@types/testing-library__jest-dom": "^5.0.2", - "chalk": "^3.0.0", + "chalk": "^4.0.0", "css": "^2.2.4", "css.escape": "^1.5.1", "jest-diff": "^25.1.0", @@ -42,11 +44,16 @@ "redent": "^3.0.0" }, "devDependencies": { + "@types/css": "^0.0.31", + "@types/jsdom": "^16.2.1", + "@types/lodash": "^4.14.150", "jest-environment-jsdom-sixteen": "^1.0.3", "jest-watch-select-projects": "^2.0.0", "jsdom": "^16.2.1", "kcd-scripts": "^5.6.0", - "pretty-format": "^25.1.0" + "pretty-format": "^25.1.0", + "ts-jest": "^25.5.0", + "typescript": "^3.8.3" }, "eslintConfig": { "extends": "./node_modules/kcd-scripts/eslint.js", diff --git a/src/__tests__/helpers/document.js b/src/__tests__/helpers/document.js deleted file mode 100644 index 97c2b221..00000000 --- a/src/__tests__/helpers/document.js +++ /dev/null @@ -1,8 +0,0 @@ -if (global.document) { - module.exports = global.document -} else { - const {JSDOM} = require('jsdom') - const {window} = new JSDOM() - - module.exports = window.document -} diff --git a/src/__tests__/helpers/document.ts b/src/__tests__/helpers/document.ts new file mode 100644 index 00000000..aa6e58a2 --- /dev/null +++ b/src/__tests__/helpers/document.ts @@ -0,0 +1,15 @@ +let document: Document + +const globalWithDocument = global as { + document?: Document +} + +if (globalWithDocument.document) { + document = globalWithDocument.document +} else { + const {JSDOM} = require('jsdom') + const {window} = new JSDOM() + document = window.document +} + +export default document diff --git a/src/__tests__/helpers/test-utils.js b/src/__tests__/helpers/test-utils.js deleted file mode 100644 index 3efabeac..00000000 --- a/src/__tests__/helpers/test-utils.js +++ /dev/null @@ -1,11 +0,0 @@ -import document from './document' - -function render(html) { - const container = document.createElement('div') - container.innerHTML = html - const queryByTestId = testId => - container.querySelector(`[data-testid="${testId}"]`) - return {container, queryByTestId} -} - -export {render} diff --git a/src/__tests__/helpers/test-utils.ts b/src/__tests__/helpers/test-utils.ts new file mode 100644 index 00000000..28375829 --- /dev/null +++ b/src/__tests__/helpers/test-utils.ts @@ -0,0 +1,20 @@ +import document from './document' +import * as extensions from '../../matchers' + +function render(html: string) { + const container = document.createElement('div') + container.innerHTML = html + const queryByTestId = (testId: string) => + container.querySelector(`[data-testid="${testId}"]`) + return {container, queryByTestId} +} + +type Extensions = typeof extensions + +declare global { + namespace jest { + interface Matchers extends Extensions {} + } +} + +export {render} diff --git a/src/__tests__/to-be-checked.js b/src/__tests__/to-be-checked.ts similarity index 100% rename from src/__tests__/to-be-checked.js rename to src/__tests__/to-be-checked.ts diff --git a/src/__tests__/to-be-disabled.js b/src/__tests__/to-be-disabled.ts similarity index 100% rename from src/__tests__/to-be-disabled.js rename to src/__tests__/to-be-disabled.ts diff --git a/src/__tests__/to-be-empty.js b/src/__tests__/to-be-empty.ts similarity index 100% rename from src/__tests__/to-be-empty.js rename to src/__tests__/to-be-empty.ts diff --git a/src/__tests__/to-be-in-the-document.js b/src/__tests__/to-be-in-the-document.ts similarity index 100% rename from src/__tests__/to-be-in-the-document.js rename to src/__tests__/to-be-in-the-document.ts diff --git a/src/__tests__/to-be-in-the-dom.js b/src/__tests__/to-be-in-the-dom.ts similarity index 99% rename from src/__tests__/to-be-in-the-dom.js rename to src/__tests__/to-be-in-the-dom.ts index 4ef9fb48..825f6c6c 100644 --- a/src/__tests__/to-be-in-the-dom.js +++ b/src/__tests__/to-be-in-the-dom.ts @@ -52,6 +52,7 @@ test('.toBeInTheDOM', () => { ).toThrowError() expect(() => { + // @ts-ignore expect(valueElement).toBeInTheDOM(fakeElement) }).toThrowError() diff --git a/src/__tests__/to-be-invalid.js b/src/__tests__/to-be-invalid.ts similarity index 98% rename from src/__tests__/to-be-invalid.js rename to src/__tests__/to-be-invalid.ts index 9929d53d..022481de 100644 --- a/src/__tests__/to-be-invalid.js +++ b/src/__tests__/to-be-invalid.ts @@ -19,7 +19,7 @@ import {render} from './helpers/test-utils' * @link https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation * @link https://github.com/jsdom/jsdom */ -function getDOMElement(htmlString, selector) { +function getDOMElement(htmlString: string, selector: string) { return new JSDOM(htmlString).window.document.querySelector(selector) } diff --git a/src/__tests__/to-be-required.js b/src/__tests__/to-be-required.ts similarity index 100% rename from src/__tests__/to-be-required.js rename to src/__tests__/to-be-required.ts diff --git a/src/__tests__/to-be-visible.js b/src/__tests__/to-be-visible.ts similarity index 75% rename from src/__tests__/to-be-visible.js rename to src/__tests__/to-be-visible.ts index 43d47284..490d6d90 100644 --- a/src/__tests__/to-be-visible.js +++ b/src/__tests__/to-be-visible.ts @@ -38,7 +38,7 @@ describe('.toBeVisible', () => { // eslint-disable-next-line max-lines-per-function describe('with a
element', () => { - let subject + let subject: ReturnType | undefined afterEach(() => { subject = undefined @@ -55,26 +55,26 @@ describe('.toBeVisible', () => { }) it('returns true to the details content', () => { - expect(subject.container.querySelector('div')).toBeVisible() + expect(subject!.container.querySelector('div')).toBeVisible() }) it('returns true to the most inner details content', () => { - expect(subject.container.querySelector('small')).toBeVisible() + expect(subject!.container.querySelector('small')).toBeVisible() }) it('returns true to the details summary', () => { - expect(subject.container.querySelector('summary')).toBeVisible() + expect(subject!.container.querySelector('summary')).toBeVisible() }) describe('when the user clicks on the summary', () => { - beforeEach(() => subject.container.querySelector('summary').click()) + beforeEach(() => subject!.container.querySelector('summary')!.click()) it('returns false to the details content', () => { - expect(subject.container.querySelector('div')).not.toBeVisible() + expect(subject!.container.querySelector('div')).not.toBeVisible() }) it('returns true to the details summary', () => { - expect(subject.container.querySelector('summary')).toBeVisible() + expect(subject!.container.querySelector('summary')).toBeVisible() }) }) }) @@ -90,22 +90,22 @@ describe('.toBeVisible', () => { }) it('returns false to the details content', () => { - expect(subject.container.querySelector('div')).not.toBeVisible() + expect(subject!.container.querySelector('div')).not.toBeVisible() }) it('returns true to the summary content', () => { - expect(subject.container.querySelector('summary')).toBeVisible() + expect(subject!.container.querySelector('summary')).toBeVisible() }) describe('when the user clicks on the summary', () => { - beforeEach(() => subject.container.querySelector('summary').click()) + beforeEach(() => subject!.container.querySelector('summary')!.click()) it('returns true to the details content', () => { - expect(subject.container.querySelector('div')).toBeVisible() + expect(subject!.container.querySelector('div')).toBeVisible() }) it('returns true to the details summary', () => { - expect(subject.container.querySelector('summary')).toBeVisible() + expect(subject!.container.querySelector('summary')).toBeVisible() }) }) }) @@ -121,11 +121,11 @@ describe('.toBeVisible', () => { }) it('returns false to the details content', () => { - expect(subject.container.querySelector('div')).not.toBeVisible() + expect(subject!.container.querySelector('div')).not.toBeVisible() }) it('returns false to the details summary', () => { - expect(subject.container.querySelector('summary')).not.toBeVisible() + expect(subject!.container.querySelector('summary')).not.toBeVisible() }) }) @@ -146,23 +146,25 @@ describe('.toBeVisible', () => { it('returns true to the nested details content', () => { expect( - subject.container.querySelector('details > details > div'), + subject!.container.querySelector('details > details > div'), ).toBeVisible() }) it('returns true to the nested details summary', () => { expect( - subject.container.querySelector('details > details > summary'), + subject!.container.querySelector('details > details > summary'), ).toBeVisible() }) it('returns true to the outer details content', () => { - expect(subject.container.querySelector('details > div')).toBeVisible() + expect( + subject!.container.querySelector('details > div'), + ).toBeVisible() }) it('returns true to the outer details summary', () => { expect( - subject.container.querySelector('details > summary'), + subject!.container.querySelector('details > summary'), ).toBeVisible() }) }) @@ -183,23 +185,25 @@ describe('.toBeVisible', () => { it('returns false to the nested details content', () => { expect( - subject.container.querySelector('details > details > div'), + subject!.container.querySelector('details > details > div'), ).not.toBeVisible() }) it('returns true to the nested details summary', () => { expect( - subject.container.querySelector('details > details > summary'), + subject!.container.querySelector('details > details > summary'), ).toBeVisible() }) it('returns true to the outer details content', () => { - expect(subject.container.querySelector('details > div')).toBeVisible() + expect( + subject!.container.querySelector('details > div'), + ).toBeVisible() }) it('returns true to the outer details summary', () => { expect( - subject.container.querySelector('details > summary'), + subject!.container.querySelector('details > summary'), ).toBeVisible() }) }) @@ -220,25 +224,25 @@ describe('.toBeVisible', () => { it('returns false to the nested details content', () => { expect( - subject.container.querySelector('details > details > div'), + subject!.container.querySelector('details > details > div'), ).not.toBeVisible() }) it('returns false to the nested details summary', () => { expect( - subject.container.querySelector('details > details > summary'), + subject!.container.querySelector('details > details > summary'), ).not.toBeVisible() }) it('returns false to the outer details content', () => { expect( - subject.container.querySelector('details > div'), + subject!.container.querySelector('details > div'), ).not.toBeVisible() }) it('returns true to the outer details summary', () => { expect( - subject.container.querySelector('details > summary'), + subject!.container.querySelector('details > summary'), ).toBeVisible() }) }) diff --git a/src/__tests__/to-contain-element.js b/src/__tests__/to-contain-element.ts similarity index 97% rename from src/__tests__/to-contain-element.js rename to src/__tests__/to-contain-element.ts index 00608dd1..a54a6c4c 100644 --- a/src/__tests__/to-contain-element.js +++ b/src/__tests__/to-contain-element.ts @@ -44,6 +44,7 @@ test('.toContainElement negative test cases', () => { expect(nonExistantElement).toContainElement(nonExistantElement), ).toThrowError() expect(() => + // @ts-ignore expect(nonExistantElement).toContainElement(fakeElement), ).toThrowError() expect(() => @@ -53,7 +54,9 @@ test('.toContainElement negative test cases', () => { expect(fakeElement).not.toContainElement(nonExistantElement), ).toThrowError() expect(() => expect(fakeElement).toContainElement(grandparent)).toThrowError() + // @ts-ignore expect(() => expect(grandparent).toContainElement(fakeElement)).toThrowError() + // @ts-ignore expect(() => expect(fakeElement).toContainElement(fakeElement)).toThrowError() expect(() => expect(grandparent).not.toContainElement(child)).toThrowError() expect(() => diff --git a/src/__tests__/to-contain-html.js b/src/__tests__/to-contain-html.ts similarity index 97% rename from src/__tests__/to-contain-html.js rename to src/__tests__/to-contain-html.ts index 2fd12531..74c8048b 100644 --- a/src/__tests__/to-contain-html.js +++ b/src/__tests__/to-contain-html.ts @@ -38,9 +38,11 @@ test('.toContainHTML', () => { expect(nonExistantElement).not.toContainHTML(stringChildElement), ).toThrowError() expect(() => + // @ts-ignore expect(nonExistantElement).not.toContainHTML(nonExistantElement), ).toThrowError() expect(() => + // @ts-ignore expect(stringChildElement).not.toContainHTML(fakeElement), ).toThrowError() expect(() => @@ -63,9 +65,12 @@ test('.toContainHTML', () => { expect(() => expect(grandparent).toContainHTML(nonExistantString), ).toThrowError() + // @ts-ignore expect(() => expect(child).toContainHTML(nonExistantElement)).toThrowError() + // @ts-ignore expect(() => expect(parent).toContainHTML(nonExistantElement)).toThrowError() expect(() => + // @ts-ignore expect(grandparent).toContainHTML(nonExistantElement), ).toThrowError() expect(() => diff --git a/src/__tests__/to-have-attribute.js b/src/__tests__/to-have-attribute.ts similarity index 99% rename from src/__tests__/to-have-attribute.js rename to src/__tests__/to-have-attribute.ts index 8abf6309..9d72c1c8 100644 --- a/src/__tests__/to-have-attribute.js +++ b/src/__tests__/to-have-attribute.ts @@ -39,6 +39,7 @@ test('.toHaveAttribute', () => { expect(queryByTestId('svg-element')).not.toHaveAttribute('width', '12'), ).toThrowError() expect(() => + // @ts-ignore expect({thisIsNot: 'an html element'}).not.toHaveAttribute(), ).toThrowError() diff --git a/src/__tests__/to-have-class.js b/src/__tests__/to-have-class.ts similarity index 100% rename from src/__tests__/to-have-class.js rename to src/__tests__/to-have-class.ts diff --git a/src/__tests__/to-have-display-value.js b/src/__tests__/to-have-display-value.ts similarity index 66% rename from src/__tests__/to-have-display-value.js rename to src/__tests__/to-have-display-value.ts index e49749a9..5aad28ab 100644 --- a/src/__tests__/to-have-display-value.js +++ b/src/__tests__/to-have-display-value.ts @@ -10,17 +10,17 @@ test('it should work as expected', () => { `) - expect(queryByTestId('select')).toHaveDisplayValue('Select a fruit...') - expect(queryByTestId('select')).not.toHaveDisplayValue('Banana') - expect(() => - expect(queryByTestId('select')).not.toHaveDisplayValue('Select a fruit...'), - ).toThrow() + const select = queryByTestId('select') as HTMLSelectElement + + expect(select).toHaveDisplayValue('Select a fruit...') + expect(select).not.toHaveDisplayValue('Banana') expect(() => - expect(queryByTestId('select')).toHaveDisplayValue('Ananas'), + expect(select).not.toHaveDisplayValue('Select a fruit...'), ).toThrow() + expect(() => expect(select).toHaveDisplayValue('Ananas')).toThrow() - queryByTestId('select').value = 'banana' - expect(queryByTestId('select')).toHaveDisplayValue('Banana') + select.value = 'banana' + expect(select).toHaveDisplayValue('Banana') }) test('it should work with select multiple', () => { @@ -32,21 +32,17 @@ test('it should work with select multiple', () => { `) + const select = queryByTestId('select') as HTMLSelectElement - expect(queryByTestId('select')).toHaveDisplayValue(['Ananas', 'Avocado']) + expect(select).toHaveDisplayValue(['Ananas', 'Avocado']) expect(() => - expect(queryByTestId('select')).not.toHaveDisplayValue([ - 'Ananas', - 'Avocado', - ]), + expect(select).not.toHaveDisplayValue(['Ananas', 'Avocado']), ).toThrow() - expect(queryByTestId('select')).not.toHaveDisplayValue('Ananas') - expect(() => - expect(queryByTestId('select')).toHaveDisplayValue('Ananas'), - ).toThrow() + expect(select).not.toHaveDisplayValue('Ananas') + expect(() => expect(select).toHaveDisplayValue('Ananas')).toThrow() - Array.from(queryByTestId('select').options).forEach(option => { + Array.from(select.options).forEach(option => { option.selected = ['ananas', 'banana'].includes(option.value) }) @@ -57,11 +53,11 @@ test('it should work with input elements', () => { const {queryByTestId} = render(` `) + const input = queryByTestId('input') as HTMLInputElement - expect(queryByTestId('input')).toHaveDisplayValue('Luca') - - queryByTestId('input').value = 'Piero' - expect(queryByTestId('input')).toHaveDisplayValue('Piero') + expect(input).toHaveDisplayValue('Luca') + input.value = 'Piero' + expect(input).toHaveDisplayValue('Piero') }) test('it should work with textarea elements', () => { @@ -69,14 +65,11 @@ test('it should work with textarea elements', () => { '', ) - expect(queryByTestId('textarea-example')).toHaveDisplayValue( - 'An example description here.', - ) + const testarea = queryByTestId('textarea-example') as HTMLTextAreaElement - queryByTestId('textarea-example').value = 'Another example' - expect(queryByTestId('textarea-example')).toHaveDisplayValue( - 'Another example', - ) + expect(testarea).toHaveDisplayValue('An example description here.') + testarea.value = 'Another example' + expect(testarea).toHaveDisplayValue('Another example') }) test('it should throw if element is not valid', () => { @@ -108,6 +101,7 @@ test('it should throw if element is not valid', () => { ) try { + // @ts-ignore expect(queryByTestId('checkbox')).toHaveDisplayValue(true) } catch (err) { errorMessage = err.message diff --git a/src/__tests__/to-have-focus.js b/src/__tests__/to-have-focus.ts similarity index 89% rename from src/__tests__/to-have-focus.js rename to src/__tests__/to-have-focus.ts index 8acb8127..c0ca29ec 100644 --- a/src/__tests__/to-have-focus.js +++ b/src/__tests__/to-have-focus.ts @@ -9,7 +9,7 @@ test('.toHaveFocus', () => { `) - const focused = container.querySelector('#focused') + const focused = container.querySelector('#focused') as HTMLLabelElement const notFocused = container.querySelector('#not-focused') document.body.appendChild(container) diff --git a/src/__tests__/to-have-form-values.js b/src/__tests__/to-have-form-values.ts similarity index 89% rename from src/__tests__/to-have-form-values.js rename to src/__tests__/to-have-form-values.ts index e6fc96a8..f141b5af 100644 --- a/src/__tests__/to-have-form-values.js +++ b/src/__tests__/to-have-form-values.ts @@ -241,7 +241,7 @@ describe('.toHaveFormValues', () => { `) const form = container.querySelector('form') - const select = container.querySelector('select') + const select = container.querySelector('select') as HTMLSelectElement expect(form).toHaveFormValues({category: oldValue}) select.value = newValue @@ -263,11 +263,25 @@ describe('.toHaveFormValues', () => { // Form control renderers -function isSelected(value, option) { +function isSelected( + value: string[], + option: { + value: string + label: string + }, +) { return Array.isArray(value) && value.indexOf(option.value) >= 0 } -function renderCheckboxes(name, label, options, value = []) { +function renderCheckboxes( + name: string, + label: string, + options: { + value: string + label: string + }[], + value: string[] = [], +) { return `
${label} @@ -290,7 +304,15 @@ function renderCheckboxes(name, label, options, value = []) { ` } -function renderRadioButtons(name, label, options, value = undefined) { +function renderRadioButtons( + name: string, + label: string, + options: { + value: string + label: string + }[], + value: string | undefined = undefined, +) { return `
${label} @@ -313,7 +335,16 @@ function renderRadioButtons(name, label, options, value = undefined) { ` } -function renderSelect(name, label, options, value, multiple) { +function renderSelect( + name: string, + label: string, + options: { + value: string + label: string + }[], + value: string[], + multiple: boolean, +) { return `