Skip to content

Commit 89ade6a

Browse files
authored
fix(cdk/testing): allow for comma key to be sent (#27486)
Adds a comma key to `TestKey` since commas aren't treated correctly in strings sent through `sendKeys` and it's common for them to be used as separators in chip lists.
1 parent ffb2464 commit 89ade6a

File tree

7 files changed

+24
-2
lines changed

7 files changed

+24
-2
lines changed

src/cdk/testing/protractor/protractor-element.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const keyMap = {
5050
[TestKey.F11]: Key.F11,
5151
[TestKey.F12]: Key.F12,
5252
[TestKey.META]: Key.META,
53+
[TestKey.COMMA]: ',',
5354
};
5455

5556
/** Converts a `ModifierKeys` object to a list of Protractor `Key`s. */

src/cdk/testing/selenium-webdriver/selenium-webdriver-keys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const seleniumWebDriverKeyMap = {
4444
[TestKey.F11]: webdriver.Key.F11,
4545
[TestKey.F12]: webdriver.Key.F12,
4646
[TestKey.META]: webdriver.Key.META,
47+
[TestKey.COMMA]: ',',
4748
};
4849

4950
/** Gets a list of WebDriver `Key`s for the given `ModifierKeys`. */

src/cdk/testing/test-element.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export enum TestKey {
6565
F11,
6666
F12,
6767
META,
68+
COMMA, // Commas are a common separator key.
6869
}
6970

7071
/**

src/cdk/testing/testbed/unit-test-element.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const keyMap = {
6161
[TestKey.F11]: {keyCode: keyCodes.F11, key: 'F11'},
6262
[TestKey.F12]: {keyCode: keyCodes.F12, key: 'F12'},
6363
[TestKey.META]: {keyCode: keyCodes.META, key: 'Meta'},
64+
[TestKey.COMMA]: {keyCode: keyCodes.COMMA, key: ','},
6465
};
6566

6667
/** A `TestElement` implementation for unit tests. */

src/material/chips/testing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ng_test_library(
1919
srcs = glob(["**/*.spec.ts"]),
2020
deps = [
2121
":testing",
22+
"//src/cdk/keycodes",
2223
"//src/cdk/testing",
2324
"//src/cdk/testing/testbed",
2425
"//src/material/chips",

src/material/chips/testing/chip-input-harness.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {HarnessLoader} from '@angular/cdk/testing';
1+
import {HarnessLoader, TestKey} from '@angular/cdk/testing';
2+
import {COMMA} from '@angular/cdk/keycodes';
23
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
34
import {Component} from '@angular/core';
45
import {ComponentFixture, TestBed} from '@angular/core/testing';
@@ -69,12 +70,24 @@ describe('MatChipInputHarness', () => {
6970
await harness.blur();
7071
expect(await harness.isFocused()).toBe(false);
7172
});
73+
74+
it('should be able to trigger a separator key', async () => {
75+
const input = await loader.getHarness(MatChipInputHarness);
76+
await input.setValue('Hello');
77+
await input.sendSeparatorKey(TestKey.COMMA);
78+
expect(fixture.componentInstance.add).toHaveBeenCalled();
79+
});
7280
});
7381

7482
@Component({
7583
template: `
7684
<mat-chip-grid #grid1>
77-
<input [matChipInputFor]="grid1" [required]="required" placeholder="Placeholder" />
85+
<input
86+
[matChipInputFor]="grid1"
87+
[required]="required"
88+
placeholder="Placeholder"
89+
(matChipInputTokenEnd)="add()"
90+
[matChipInputSeparatorKeyCodes]="separatorKeyCodes"/>
7891
</mat-chip-grid>
7992
8093
<mat-chip-grid #grid2>
@@ -84,4 +97,6 @@ describe('MatChipInputHarness', () => {
8497
})
8598
class ChipInputHarnessTest {
8699
required = false;
100+
add = jasmine.createSpy('add spy');
101+
separatorKeyCodes = [COMMA];
87102
}

tools/public_api_guard/cdk/testing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ export enum TestKey {
263263
// (undocumented)
264264
BACKSPACE = 0,
265265
// (undocumented)
266+
COMMA = 30,
267+
// (undocumented)
266268
CONTROL = 4,
267269
// (undocumented)
268270
DELETE = 16,

0 commit comments

Comments
 (0)