Skip to content

Commit e9734a9

Browse files
crisbetoandrewseguin
authored andcommitted
fix(cdk/testing): entering negative number values not working with reactive forms (#24656)
We were entering negative values into number inputs character-by-character which doesn't appear to work with Angular's reactive forms. These changes switch to entering the entire value all at once. Fixes #24414. (cherry picked from commit 5b4bcf5)
1 parent 2656a03 commit e9734a9

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/cdk/testing/testbed/fake-events/type-in-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function typeInElement(element: HTMLElement, ...modifiersAndKeys: any[])
9797
const enterValueIncrementally =
9898
inputType === 'number'
9999
? // The value can be set character by character in number inputs if it doesn't have any decimals.
100-
keys.every(key => key.key !== '.' && key.keyCode !== PERIOD)
100+
keys.every(key => key.key !== '.' && key.key !== '-' && key.keyCode !== PERIOD)
101101
: incrementalInputTypes.has(inputType);
102102

103103
triggerFocus(element);

src/cdk/testing/tests/cross-environment.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@ export function crossEnvironmentSpecs(
465465
expect(await value.text()).toBe('Number value: 123.456');
466466
});
467467

468+
it('should be able to set a negative input value on a reactive form control', async () => {
469+
const input = await harness.numberInput();
470+
const value = await harness.numberInputValue();
471+
await input.sendKeys('-123');
472+
473+
expect(await input.getProperty<string>('value')).toBe('-123');
474+
expect(await value.text()).toBe('Number value: -123');
475+
});
476+
468477
it('should be able to retrieve dimensions', async () => {
469478
const dimensions = await (await harness.title()).getDimensions();
470479
expect(dimensions).toEqual(jasmine.objectContaining({height: 100, width: 200}));

0 commit comments

Comments
 (0)