Skip to content

fix(cdk/testing): allow for comma key to be sent #27486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cdk/testing/protractor/protractor-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const keyMap = {
[TestKey.F11]: Key.F11,
[TestKey.F12]: Key.F12,
[TestKey.META]: Key.META,
[TestKey.COMMA]: ',',
};

/** Converts a `ModifierKeys` object to a list of Protractor `Key`s. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const seleniumWebDriverKeyMap = {
[TestKey.F11]: webdriver.Key.F11,
[TestKey.F12]: webdriver.Key.F12,
[TestKey.META]: webdriver.Key.META,
[TestKey.COMMA]: ',',
};

/** Gets a list of WebDriver `Key`s for the given `ModifierKeys`. */
Expand Down
1 change: 1 addition & 0 deletions src/cdk/testing/test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export enum TestKey {
F11,
F12,
META,
COMMA, // Commas are a common separator key.
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/cdk/testing/testbed/unit-test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const keyMap = {
[TestKey.F11]: {keyCode: keyCodes.F11, key: 'F11'},
[TestKey.F12]: {keyCode: keyCodes.F12, key: 'F12'},
[TestKey.META]: {keyCode: keyCodes.META, key: 'Meta'},
[TestKey.COMMA]: {keyCode: keyCodes.COMMA, key: ','},
};

/** A `TestElement` implementation for unit tests. */
Expand Down
1 change: 1 addition & 0 deletions src/material/chips/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ng_test_library(
srcs = glob(["**/*.spec.ts"]),
deps = [
":testing",
"//src/cdk/keycodes",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/chips",
Expand Down
19 changes: 17 additions & 2 deletions src/material/chips/testing/chip-input-harness.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {HarnessLoader} from '@angular/cdk/testing';
import {HarnessLoader, TestKey} from '@angular/cdk/testing';
import {COMMA} from '@angular/cdk/keycodes';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
Expand Down Expand Up @@ -69,12 +70,24 @@ describe('MatChipInputHarness', () => {
await harness.blur();
expect(await harness.isFocused()).toBe(false);
});

it('should be able to trigger a separator key', async () => {
const input = await loader.getHarness(MatChipInputHarness);
await input.setValue('Hello');
await input.sendSeparatorKey(TestKey.COMMA);
expect(fixture.componentInstance.add).toHaveBeenCalled();
});
});

@Component({
template: `
<mat-chip-grid #grid1>
<input [matChipInputFor]="grid1" [required]="required" placeholder="Placeholder" />
<input
[matChipInputFor]="grid1"
[required]="required"
placeholder="Placeholder"
(matChipInputTokenEnd)="add()"
[matChipInputSeparatorKeyCodes]="separatorKeyCodes"/>
</mat-chip-grid>

<mat-chip-grid #grid2>
Expand All @@ -84,4 +97,6 @@ describe('MatChipInputHarness', () => {
})
class ChipInputHarnessTest {
required = false;
add = jasmine.createSpy('add spy');
separatorKeyCodes = [COMMA];
}
2 changes: 2 additions & 0 deletions tools/public_api_guard/cdk/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ export enum TestKey {
// (undocumented)
BACKSPACE = 0,
// (undocumented)
COMMA = 30,
// (undocumented)
CONTROL = 4,
// (undocumented)
DELETE = 16,
Expand Down