gpt4 book ai didi

javascript - 带有@Input/@Output 和 react 形式的单元测试组件

转载 作者:行者123 更新时间:2023-11-28 21:20:59 26 4
gpt4 key购买 nike

这是我的组件:

import { Component, OnInit, Input, Output, EventEmitter, ChangeDetectionStrategy, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup , Validators } from '@angular/forms';
import { Subscription } from 'rxjs';
import { Values } from '../_models/values';

@Component({
selector: 'some',
templateUrl: './my.component.html',
styleUrls: ['./my.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class Mycomponent implements OnInit, OnDestroy {

@Input()
values: Array<string>;

@Output()
selectedValues = new EventEmitter<Values>();

private myForm: FormGroup;

@Input()
errorMsg: string;

private selectSubscription: Subscription;

constructor(private fb: FormBuilder) {}

ngOnInit() {
this.myForm = this.fb.group({
'selectAll': [false],
'values': [this.values, Validators.required]
});

this.selectSubscription = this.myForm.get('selectAll').valueChanges.subscribe(value => {
this.changeSelection(value);
});
}

submit(): void {
console.log('called');

console.log(this.myForm.value.values);

const theSelectedValues = {
vals: this.myForm.value.values
};
this.selectedValues.emit(theSelectedValues);
}

private changeSelection(selectAll: boolean): void {
if (selectAll) {
const valuesSelect = this.myForm.controls['values'];
valuesSelect.disable();
} else {
this.myForm.controls['values'].enable();

}
}

ngOnDestroy() {
this.selectSubscription.unsubscribe();
}

}

模板:

<form [formGroup]="myForm" (ngSubmit)="submit()">
<fieldset>
<mat-checkbox formControlName="all">Select all</mat-checkbox>
</fieldset>
<fieldset>
<select id="chooser" formControlName="values" multiple>
<option *ngFor="let val of values?.urls" [value]="val">{{val}}</option>
</select>
</fieldset>
<button mat-button [disabled]="myForm.invalid">Go!</button>
</form>
<div *ngIf="errorMsg">{{errorMsg}}</div>

测试

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { Mycomponent } from './my.component';
import { By } from '@angular/platform-browser';
import { FormBuilder } from '@angular/forms';
import { NO_ERRORS_SCHEMA } from '@angular/core';

describe('Mycomponent', () => {
let component: Mycomponent;
let fixture: ComponentFixture<Mycomponent>;


beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [],
schemas: [NO_ERRORS_SCHEMA],
declarations: [Mycomponent],
providers: [
FormBuilder ]
})
.compileComponents();

}));

beforeEach(() => {
fixture = TestBed.createComponent(Mycomponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should emit selected values', () => {

spyOn(component.selectedValues, 'emit');
component.values = ['abc', 'de'];

fixture.detectChanges();

expect(fixture.debugElement.queryAll(By.css('option')).length).toBe(2); // is 0

const formDE = fixture.debugElement.query(By.css('form'));
formDE.triggerEventHandler('ngSubmit', {});

// urls: null
expect(component.selectedValues.emit).toHaveBeenCalledWith({ vals: ['abc', 'de'] });
});
});

测试失败是因为

一个)

component.values = ['abc', 'de'];

不会导致表单有两个选项元素

和b)

expect(component.selectedValues.emit).toHaveBeenCalledWith({ vals: ['abc', 'de'] });

被调用,但使用 { vals: null }

代码可以运行,应用程序本身也可以正常运行,只是测试失败了。

如何正确设置表单,@Input 元素?

我看过一些博客文章,但无法将它们改编为我的代码。

最佳答案

这是因为您正在使用 Onpush 策略。当使用 onPush 时,变化检测是从父组件而不是组件本身向下传播的。

我的建议是将您的测试组件包装在宿主组件中。在 Angular 的 gitHub 页面上有一个与此相关的悬而未决的问题,您可以查找它以进一步阅读。

关于javascript - 带有@Input/@Output 和 react 形式的单元测试组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52000324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com