Skip to content

Commit dc6af45

Browse files
fix: allow type to receive a number value (#44)
1 parent 9f9e8c3 commit dc6af45

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

projects/testing-library/src/lib/user-events/type.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function createType(fireEvent: FireFunction & FireObject) {
3636
};
3737
}
3838

39-
return async function type(element: HTMLElement, value: string, options?: TypeOptions) {
39+
return async function type(element: HTMLElement, value: string | number, options?: TypeOptions) {
4040
const { allAtOnce = false, delay = 0 } = options || {};
4141
const initialValue = (element as HTMLInputElement).value;
4242

@@ -46,9 +46,10 @@ export function createType(fireEvent: FireFunction & FireObject) {
4646
return;
4747
}
4848

49+
const text = value.toString();
4950
let actuallyTyped = '';
50-
for (let index = 0; index < value.length; index++) {
51-
const char = value[index];
51+
for (let index = 0; index < text.length; index++) {
52+
const char = text[index];
5253
const key = char;
5354
const keyCode = char.charCodeAt(0);
5455

src/app/examples/03-forms.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test('is possible to fill in a form and verify error messages (with the help of
2727
expect(component.queryByText('color is required')).not.toBeInTheDocument();
2828

2929
expect(scoreControl).toBeInvalid();
30-
component.type(scoreControl, '7');
30+
component.type(scoreControl, 7);
3131
expect(scoreControl).toBeValid();
3232

3333
expect(errors).not.toBeInTheDocument();

src/app/examples/04-forms-with-material.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ test('is possible to fill in a form and verify error messages (with the help of
1919
expect(errors).toContainElement(component.queryByText('color is required'));
2020

2121
component.type(nameControl, 'Tim');
22-
component.type(scoreControl, '12');
22+
component.type(scoreControl, 12);
2323
component.selectOptions(colorControl, 'Green');
2424

2525
expect(component.queryByText('name is required')).not.toBeInTheDocument();
2626
expect(component.queryByText('score must be lesser than 10')).toBeInTheDocument();
2727
expect(component.queryByText('color is required')).not.toBeInTheDocument();
2828

2929
expect(scoreControl).toBeInvalid();
30-
component.type(scoreControl, '7');
30+
component.type(scoreControl, 7);
3131
expect(scoreControl).toBeValid();
3232

3333
expect(errors).not.toBeInTheDocument();

0 commit comments

Comments
 (0)