angular - 带输入的材料芯片的角度单元测试
问题描述
我正在尝试对 mat-chips 元素进行单元测试,目前我收到一个错误:无法绑定到“matChipInputFor”,因为它不是“输入”的已知属性。目前,Angular Material 页面没有为他们的示例提供单元测试。我使用它的示例在https://material.angular.io/components/chips/overview上进行了描述
如何在给定输入的单元测试中添加该属性,例如 matChipInputFor?
模板
<mat-form-field class="example-chip-list">
<mat-chip-list #chipList aria-label="Fruit selection">
<mat-chip *ngFor="let fruit of fruits" [selectable]="selectable"
[removable]="removable" (removed)="remove(fruit)">
{{fruit.name}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input placeholder="New fruit..."
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)">
</mat-chip-list>
</mat-form-field>
控制器
import {Component} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {MatChipInputEvent} from '@angular/material/chips';
import {COMMA, ENTER} from '@angular/cdk/keycodes';
export interface Fruit {
name: string;
}
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
//required for the kitchen sink form
options: FormGroup;
visible = true;
selectable = true;
removable = true;
addOnBlur = true;
constructor(fb: FormBuilder) {
this.options = fb.group({
hideRequired: false,
floatLabel: 'auto',
});
}
readonly separatorKeysCodes: number[] = [ENTER, COMMA];
fruits: Fruit[] = [
{name: 'Lemon'},
{name: 'Lime'},
{name: 'Apple'},
];
add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
// Add our fruit
if ((value || '').trim()) {
this.fruits.push({name: value.trim()});
}
// Reset the input value
if (input) {
input.value = '';
}
}
remove(fruit: Fruit): void {
const index = this.fruits.indexOf(fruit);
if (index >= 0) {
this.fruits.splice(index, 1);
}
}
}
单元测试(模板有更多元素)
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { GivenComponent } from './given.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ObserversModule } from '@angular/cdk/observers';
import { OverlayModule } from '@angular/cdk/overlay';
//Material imports
import {
MatCard,
MatRadioButton,
MatRadioGroup,
MatIcon,
MatChip,
MatChipList,
MatCheckbox,
MatSlideToggle,
MatRippleModule,
MatTabsModule,
MatDialogModule,
MatSelectModule,
MatInputModule
} from '@angular/material';
describe('GivenComponent', () => {
let component: GivenComponent;
let fixture: ComponentFixture<GivenComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
ReactiveFormsModule,
BrowserAnimationsModule,
ObserversModule,
MatRippleModule,
OverlayModule,
MatTabsModule,
MatDialogModule,
MatSelectModule,
MatInputModule
],
declarations: [
GivenComponent,
MatCard,
MatRadioButton,
MatRadioGroup,
MatIcon,
MatChip,
MatChipList,
MatCheckbox,
MatSlideToggle
]
});
fixture = TestBed.createComponent(GivenComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeDefined();
});
});
解决方案
您需要导入MatChipsModule
提供此指令的内容。
推荐阅读
- javascript - 虚拟 DOM 如何高效的最小示例
- firebase - 当我从 firebase 导入 JSon 时,是否可以为每个元素生成密钥
- apache-kafka - debezium 无法使用带有默认插件 pgoutput 的 postgres 11 访问文件“decoderbufs”
- android - 当 MainAcitivty 调用 onDestroy() 时如何调用所有活动 onDestroy()?
- javascript - JS/JSON 每小时为数据创建一个新行
- node.js - 无法完成 React 在 Github 页面上使用的“npm run deploy”命令
- ios - 停止 UICollectionView 滚动并使其显示所有单元格
- java - OAuth2RestOperations:java.lang.IllegalArgumentException:URI 不是绝对的
- graph - 图建模(社交网络)
- sql-server - updateOrCreate laravel 不适用于 SQL Server