Skip to content

Commit de542df

Browse files
authored
fix(material-experimental/mdc-chips): add missing functionality and align tests (#20814)
* Adds support for the `MAT_RIPPLE_GLOBAL_OPTIONS` injection token in the MDC-based chips. * Aligns the tests between the standard and MDC versions so that it's easier to lint them.
1 parent b0e97af commit de542df

File tree

9 files changed

+44
-16
lines changed

9 files changed

+44
-16
lines changed

src/material-experimental/mdc-chips/chip-input.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('MDC-based MatChipInput', () => {
8989
expect(label.textContent).toContain('or don\'t');
9090
});
9191

92-
it('should become disabled if the chip grid is disabled', () => {
92+
it('should become disabled if the list is disabled', () => {
9393
expect(inputNativeElement.hasAttribute('disabled')).toBe(false);
9494
expect(chipInputDirective.disabled).toBe(false);
9595

@@ -100,7 +100,7 @@ describe('MDC-based MatChipInput', () => {
100100
expect(chipInputDirective.disabled).toBe(true);
101101
});
102102

103-
it('should be aria-required if the chip grid is required', () => {
103+
it('should be aria-required if the list is required', () => {
104104
expect(inputNativeElement.hasAttribute('aria-required')).toBe(false);
105105

106106
fixture.componentInstance.required = true;

src/material-experimental/mdc-chips/chip-option.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import {SPACE} from '@angular/cdk/keycodes';
33
import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing/private';
44
import {Component, DebugElement, ViewChild} from '@angular/core';
55
import {waitForAsync, ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
6+
import {
7+
MAT_RIPPLE_GLOBAL_OPTIONS,
8+
RippleGlobalOptions,
9+
} from '@angular/material-experimental/mdc-core';
610
import {By} from '@angular/platform-browser';
711
import {chipCssClasses} from '@material/chips';
812
import {Subject} from 'rxjs';
@@ -20,14 +24,16 @@ describe('MDC-based Option Chips', () => {
2024
let chipDebugElement: DebugElement;
2125
let chipNativeElement: HTMLElement;
2226
let chipInstance: MatChipOption;
23-
27+
let globalRippleOptions: RippleGlobalOptions;
2428
let dir = 'ltr';
2529

2630
beforeEach(waitForAsync(() => {
31+
globalRippleOptions = {};
2732
TestBed.configureTestingModule({
2833
imports: [MatChipsModule],
2934
declarations: [SingleChip],
3035
providers: [
36+
{provide: MAT_RIPPLE_GLOBAL_OPTIONS, useFactory: () => globalRippleOptions},
3137
{provide: Directionality, useFactory: () => ({
3238
value: dir,
3339
change: new Subject()
@@ -167,6 +173,17 @@ describe('MDC-based Option Chips', () => {
167173
expect(spy).not.toHaveBeenCalled();
168174
subscription.unsubscribe();
169175
});
176+
177+
it('should be able to disable ripples through ripple global options at runtime', () => {
178+
expect(chipInstance._isRippleDisabled())
179+
.toBe(false, 'Expected chip ripples to be enabled.');
180+
181+
globalRippleOptions.disabled = true;
182+
183+
expect(chipInstance._isRippleDisabled())
184+
.toBe(true, 'Expected chip ripples to be disabled.');
185+
});
186+
170187
});
171188

172189
describe('keyboard behavior', () => {

src/material-experimental/mdc-chips/chip-remove.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('MDC-based Chip Remove', () => {
3737
}));
3838

3939
describe('basic behavior', () => {
40-
it('should apply the `mat-mdc-chip-remove` CSS class', () => {
40+
it('should apply a CSS class to the remove icon', () => {
4141
let buttonElement = chipNativeElement.querySelector('button')!;
4242

4343
expect(buttonElement.classList).toContain('mat-mdc-chip-remove');
@@ -67,7 +67,7 @@ describe('MDC-based Chip Remove', () => {
6767
expect(chipNativeElement.classList.contains('mdc-chip--exit')).toBe(true);
6868
});
6969

70-
it ('should emit (removed) event when exit animation is complete', () => {
70+
it('should emit (removed) event when exit animation is complete', () => {
7171
let buttonElement = chipNativeElement.querySelector('button')!;
7272

7373
testChip.removable = true;

src/material-experimental/mdc-chips/chip-row.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import {
2828
ViewEncapsulation
2929
} from '@angular/core';
3030
import {DOCUMENT} from '@angular/common';
31+
import {
32+
MAT_RIPPLE_GLOBAL_OPTIONS,
33+
RippleGlobalOptions,
34+
} from '@angular/material-experimental/mdc-core';
3135
import {MatChip, MatChipEvent} from './chip';
3236
import {MatChipEditInput} from './chip-edit-input';
3337
import {GridKeyManagerRow} from './grid-key-manager';
@@ -100,8 +104,10 @@ export class MatChipRow extends MatChip implements AfterContentInit, AfterViewIn
100104
changeDetectorRef: ChangeDetectorRef,
101105
elementRef: ElementRef, ngZone: NgZone,
102106
@Optional() dir: Directionality,
103-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
104-
super(changeDetectorRef, elementRef, ngZone, dir, animationMode);
107+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
108+
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS)
109+
globalRippleOptions?: RippleGlobalOptions) {
110+
super(changeDetectorRef, elementRef, ngZone, dir, animationMode, globalRippleOptions);
105111
}
106112

107113
ngAfterContentInit() {

src/material-experimental/mdc-chips/chip.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('MDC-based MatChip', () => {
3939
}));
4040

4141
describe('MatBasicChip', () => {
42-
it('adds the `mat-mdc-basic-chip` class', () => {
42+
it('adds a class to indicate that it is a basic chip', () => {
4343
fixture = TestBed.createComponent(BasicChip);
4444
fixture.detectChanges();
4545

@@ -180,7 +180,7 @@ describe('MDC-based MatChip', () => {
180180
expect(chipNativeElement.getAttribute('aria-disabled')).toBe('true');
181181
});
182182

183-
it('should not be focusable', () => {
183+
it('should make disabled chips non-focusable', () => {
184184
expect(chipNativeElement.getAttribute('tabindex')).toBeFalsy();
185185
});
186186

src/material-experimental/mdc-chips/chip.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ import {
3737
HasTabIndex,
3838
HasTabIndexCtor,
3939
MatRipple,
40+
MAT_RIPPLE_GLOBAL_OPTIONS,
4041
mixinColor,
4142
mixinDisableRipple,
4243
mixinTabIndex,
4344
RippleAnimationConfig,
45+
RippleGlobalOptions,
4446
} from '@angular/material-experimental/mdc-core';
4547
import {MDCChipAdapter, MDCChipFoundation} from '@material/chips';
4648
import {numbers} from '@material/ripple';
@@ -352,7 +354,9 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
352354
public _changeDetectorRef: ChangeDetectorRef,
353355
readonly _elementRef: ElementRef, protected _ngZone: NgZone,
354356
@Optional() private _dir: Directionality,
355-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
357+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
358+
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS)
359+
private _globalRippleOptions?: RippleGlobalOptions) {
356360
super(_elementRef);
357361
this._chipFoundation = new MDCChipFoundation(this._chipAdapter);
358362
this._animationsDisabled = animationMode === 'NoopAnimations';
@@ -463,7 +467,8 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
463467

464468
/** Whether or not the ripple should be disabled. */
465469
_isRippleDisabled(): boolean {
466-
return this.disabled || this.disableRipple || this._animationsDisabled || this._isBasicChip;
470+
return this.disabled || this.disableRipple || this._animationsDisabled ||
471+
this._isBasicChip || !!this._globalRippleOptions?.disabled;
467472
}
468473

469474
_notifyInteraction() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('MatChipInput', () => {
8585
expect(label.textContent).toContain('or don\'t');
8686
});
8787

88-
it('should become disabled if the chip list is disabled', () => {
88+
it('should become disabled if the list is disabled', () => {
8989
expect(inputNativeElement.hasAttribute('disabled')).toBe(false);
9090
expect(chipInputDirective.disabled).toBe(false);
9191

@@ -130,7 +130,7 @@ describe('MatChipInput', () => {
130130
expect(listElement.getAttribute('tabindex')).toBe('0', 'Expected tabindex to remain 0');
131131
}));
132132

133-
it('should be aria-required if the chip list is required', () => {
133+
it('should be aria-required if the list is required', () => {
134134
expect(inputNativeElement.hasAttribute('aria-required')).toBe(false);
135135

136136
fixture.componentInstance.required = true;

src/material/chips/chip-remove.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Chip Remove', () => {
3030
}));
3131

3232
describe('basic behavior', () => {
33-
it('should apply the `mat-chip-remove` CSS class', () => {
33+
it('should apply a CSS class to the remove icon', () => {
3434
let buttonElement = chipNativeElement.querySelector('button')!;
3535

3636
expect(buttonElement.classList).toContain('mat-chip-remove');
@@ -48,7 +48,7 @@ describe('Chip Remove', () => {
4848
expect(buttonElement.hasAttribute('type')).toBe(false);
4949
});
5050

51-
it('should emits (removed) on click', () => {
51+
it('should emit (removed) on click', () => {
5252
let buttonElement = chipNativeElement.querySelector('button')!;
5353

5454
testChip.removable = true;

src/material/chips/chip.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('MatChip', () => {
4040
}));
4141

4242
describe('MatBasicChip', () => {
43-
it('adds the `mat-basic-chip` class', () => {
43+
it('adds a class to indicate that it is a basic chip', () => {
4444
fixture = TestBed.createComponent(BasicChip);
4545
fixture.detectChanges();
4646

0 commit comments

Comments
 (0)