Skip to content

Commit 5b4bcf5

Browse files
authored
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.
1 parent cb206be commit 5b4bcf5

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
@@ -483,6 +483,15 @@ export function crossEnvironmentSpecs(
483483
expect(await value.text()).toBe('Number value: 123.456');
484484
});
485485

486+
it('should be able to set a negative input value on a reactive form control', async () => {
487+
const input = await harness.numberInput();
488+
const value = await harness.numberInputValue();
489+
await input.sendKeys('-123');
490+
491+
expect(await input.getProperty<string>('value')).toBe('-123');
492+
expect(await value.text()).toBe('Number value: -123');
493+
});
494+
486495
it('should be able to retrieve dimensions', async () => {
487496
const dimensions = await (await harness.title()).getDimensions();
488497
expect(dimensions).toEqual(jasmine.objectContaining({height: 100, width: 200}));

0 commit comments

Comments
 (0)