1- import { HarnessLoader } from '@angular/cdk/testing' ;
1+ import { HarnessLoader , parallel } from '@angular/cdk/testing' ;
22import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed' ;
33import { FormControl , ReactiveFormsModule , Validators } from '@angular/forms' ;
44import { Component } from '@angular/core' ;
@@ -78,12 +78,55 @@ describe('MatChipGridHarness', () => {
7878
7979 expect ( await harness . isInvalid ( ) ) . toBe ( true ) ;
8080 } ) ;
81+
82+ it ( 'should get whether a chip is editable' , async ( ) => {
83+ const grid = await loader . getHarness ( MatChipGridHarness ) ;
84+ const chips = await grid . getRows ( ) ;
85+ fixture . componentInstance . firstChipEditable = true ;
86+
87+ expect ( await parallel ( ( ) => chips . map ( chip => chip . isEditable ( ) ) ) ) . toEqual ( [
88+ true ,
89+ false ,
90+ false ,
91+ ] ) ;
92+ } ) ;
93+
94+ it ( 'should throw when trying to edit a chip that is not editable' , async ( ) => {
95+ const grid = await loader . getHarness ( MatChipGridHarness ) ;
96+ const chip = ( await grid . getRows ( ) ) [ 0 ] ;
97+ let error : string | null = null ;
98+ fixture . componentInstance . firstChipEditable = false ;
99+
100+ try {
101+ await chip . startEditing ( ) ;
102+ } catch ( e : any ) {
103+ error = e . message ;
104+ }
105+
106+ expect ( error ) . toBe ( 'Cannot begin editing a chip that is not editable.' ) ;
107+ } ) ;
108+
109+ it ( 'should be able to edit a chip row' , async ( ) => {
110+ const grid = await loader . getHarness ( MatChipGridHarness ) ;
111+ const chip = ( await grid . getRows ( ) ) [ 0 ] ;
112+ fixture . componentInstance . firstChipEditable = true ;
113+
114+ await chip . startEditing ( ) ;
115+ await ( await chip . getEditInput ( ) ) . setValue ( 'new value' ) ;
116+ await chip . finishEditing ( ) ;
117+
118+ expect ( fixture . componentInstance . editSpy ) . toHaveBeenCalledWith (
119+ jasmine . objectContaining ( {
120+ value : 'new value' ,
121+ } ) ,
122+ ) ;
123+ } ) ;
81124} ) ;
82125
83126@Component ( {
84127 template : `
85128 <mat-chip-grid [formControl]="control" [required]="required" #grid>
86- <mat-chip-row>Chip A</mat-chip-row>
129+ <mat-chip-row [editable]="firstChipEditable" (edited)="editSpy($event)" >Chip A</mat-chip-row>
87130 <mat-chip-row>Chip B</mat-chip-row>
88131 <mat-chip-row>Chip C</mat-chip-row>
89132 <input [matChipInputFor]="grid"/>
@@ -93,4 +136,6 @@ describe('MatChipGridHarness', () => {
93136class ChipGridHarnessTest {
94137 control = new FormControl ( 'value' , [ Validators . required ] ) ;
95138 required = false ;
139+ firstChipEditable = false ;
140+ editSpy = jasmine . createSpy ( 'editSpy' ) ;
96141}
0 commit comments