1
- import { HarnessLoader } from '@angular/cdk/testing' ;
1
+ import { HarnessLoader , parallel } from '@angular/cdk/testing' ;
2
2
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed' ;
3
3
import { FormControl , ReactiveFormsModule , Validators } from '@angular/forms' ;
4
4
import { Component } from '@angular/core' ;
@@ -78,12 +78,55 @@ describe('MatChipGridHarness', () => {
78
78
79
79
expect ( await harness . isInvalid ( ) ) . toBe ( true ) ;
80
80
} ) ;
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
+ } ) ;
81
124
} ) ;
82
125
83
126
@Component ( {
84
127
template : `
85
128
<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>
87
130
<mat-chip-row>Chip B</mat-chip-row>
88
131
<mat-chip-row>Chip C</mat-chip-row>
89
132
<input [matChipInputFor]="grid"/>
@@ -93,4 +136,6 @@ describe('MatChipGridHarness', () => {
93
136
class ChipGridHarnessTest {
94
137
control = new FormControl ( 'value' , [ Validators . required ] ) ;
95
138
required = false ;
139
+ firstChipEditable = false ;
140
+ editSpy = jasmine . createSpy ( 'editSpy' ) ;
96
141
}
0 commit comments