我如何在 Angular 2 中测试此功能
How can i test this function in Angular 2
moveColumns(shifter: number, index: number) {
const columnFields = this.form.get('sizes') as FormArray;
let newIndex = index + shifter;
if (newIndex === -1) {
newIndex = columnFields.length - 1;
} else if (newIndex === columnFields.length) {
newIndex = 0;
}
const currentGroup = columnFields.at(index);
columnFields.removeAt(index);
columnFields.insert(newIndex, currentGroup);
}
我的难点在于它们都在函数变量内部,而不是组件全局变量。
您需要存根表单对象,然后验证函数中发生的更改
// in your test beforeEach
mockComponent.form = new FormArray({sizes: new FormControl());
// And in your function, you can update this form object and validate it
it('should test my function', () => {
const modifiedForm = {} // Add the final result after the function executes
expect(mockComponent.form).toEquals(modifiedForm);
})
moveColumns(shifter: number, index: number) {
const columnFields = this.form.get('sizes') as FormArray;
let newIndex = index + shifter;
if (newIndex === -1) {
newIndex = columnFields.length - 1;
} else if (newIndex === columnFields.length) {
newIndex = 0;
}
const currentGroup = columnFields.at(index);
columnFields.removeAt(index);
columnFields.insert(newIndex, currentGroup);
}
我的难点在于它们都在函数变量内部,而不是组件全局变量。
您需要存根表单对象,然后验证函数中发生的更改
// in your test beforeEach
mockComponent.form = new FormArray({sizes: new FormControl());
// And in your function, you can update this form object and validate it
it('should test my function', () => {
const modifiedForm = {} // Add the final result after the function executes
expect(mockComponent.form).toEquals(modifiedForm);
})