From c4813c3d20fb21dc05e1ea0f8fed1da75a770993 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Sat, 31 Aug 2019 17:30:07 +0200 Subject: [PATCH] fix: allow type to receive a number value --- projects/testing-library/src/lib/user-events/type.ts | 7 ++++--- src/app/examples/03-forms.spec.ts | 2 +- src/app/examples/04-forms-with-material.spec.ts | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/projects/testing-library/src/lib/user-events/type.ts b/projects/testing-library/src/lib/user-events/type.ts index dcd5db5b..f658ad3b 100644 --- a/projects/testing-library/src/lib/user-events/type.ts +++ b/projects/testing-library/src/lib/user-events/type.ts @@ -36,7 +36,7 @@ export function createType(fireEvent: FireFunction & FireObject) { }; } - return async function type(element: HTMLElement, value: string, options?: TypeOptions) { + return async function type(element: HTMLElement, value: string | number, options?: TypeOptions) { const { allAtOnce = false, delay = 0 } = options || {}; const initialValue = (element as HTMLInputElement).value; @@ -46,9 +46,10 @@ export function createType(fireEvent: FireFunction & FireObject) { return; } + const text = value.toString(); let actuallyTyped = ''; - for (let index = 0; index < value.length; index++) { - const char = value[index]; + for (let index = 0; index < text.length; index++) { + const char = text[index]; const key = char; const keyCode = char.charCodeAt(0); diff --git a/src/app/examples/03-forms.spec.ts b/src/app/examples/03-forms.spec.ts index d7f6758c..80bb8277 100644 --- a/src/app/examples/03-forms.spec.ts +++ b/src/app/examples/03-forms.spec.ts @@ -27,7 +27,7 @@ test('is possible to fill in a form and verify error messages (with the help of expect(component.queryByText('color is required')).not.toBeInTheDocument(); expect(scoreControl).toBeInvalid(); - component.type(scoreControl, '7'); + component.type(scoreControl, 7); expect(scoreControl).toBeValid(); expect(errors).not.toBeInTheDocument(); diff --git a/src/app/examples/04-forms-with-material.spec.ts b/src/app/examples/04-forms-with-material.spec.ts index faca26b2..14c966a5 100644 --- a/src/app/examples/04-forms-with-material.spec.ts +++ b/src/app/examples/04-forms-with-material.spec.ts @@ -19,7 +19,7 @@ test('is possible to fill in a form and verify error messages (with the help of expect(errors).toContainElement(component.queryByText('color is required')); component.type(nameControl, 'Tim'); - component.type(scoreControl, '12'); + component.type(scoreControl, 12); component.selectOptions(colorControl, 'Green'); expect(component.queryByText('name is required')).not.toBeInTheDocument(); @@ -27,7 +27,7 @@ test('is possible to fill in a form and verify error messages (with the help of expect(component.queryByText('color is required')).not.toBeInTheDocument(); expect(scoreControl).toBeInvalid(); - component.type(scoreControl, '7'); + component.type(scoreControl, 7); expect(scoreControl).toBeValid(); expect(errors).not.toBeInTheDocument();