我有一个相当简单的工作 Angular 2 组件。该组件根据我已排序的值数组呈现一些 div 兄弟元素。该组件实现OnChanges
. 排序功能发生在ngOnChanges()
. 最初,我的@Input()
属性引用了一个未排序的值数组。一旦更改检测开始,生成的 DOM 元素就会按预期排序。
我编写了一个 Karma 单元测试来验证组件中的排序逻辑是否已经发生,并且预期的 DOM 元素是否按排序顺序呈现。我以编程方式在单元测试中设置组件属性。调用后fixture.detectChanges()
,组件渲染其 DOM。但是,我是 ngOnChanges()` 函数,我看到它永远不会执行。对这种行为进行单元测试的正确方法是什么?
这是我的组件:
@Component({
selector: 'field-value-map-item',
templateUrl: 'field-value-map-item.component.html',
styleUrls: [ 'field-value-map-item.component.less' ]
})
export class FieldValueMapItemComponent implements OnChanges {
@Input() data: FieldMapping
private mappings: { [id: number]: Array<ValueMapping> }
ngOnChanges(changes: any): void {
if (changes.data) { // && !changes.data.isFirstChange()) {
this.handleMappingDataChange(changes.data.currentValue)
}
}
private handleMappingDataChange(mapping: FieldMapping) {
// update mappings data structure
this.mappings = {};
if (mapping.inputFields) {
mapping.inputFields.forEach((field: Field) => {
field.allowedValues = sortBy(field.allowedValues, [ 'valueText' ])
})
}
if (mapping.valueMap) {
// console.log(mapping.valueMap.mappings.map((item) => item.input))
// order mappings by outputValue.valueText so that all target
// values are ordered.
let orderedMappings = sortBy(mapping.valueMap.mappings, [ 'inputValue.valueText' ])
orderedMappings.forEach((mapping) => {
if (!this.mappings[mapping.outputValue.id]) {
this.mappings[mapping.outputValue.id] = []
}
this.mappings[mapping.outputValue.id].push(mapping)
})
}
}
}
组件模板:
<div class="field-value-map-item">
<div *ngIf="data && data.valueMap"
class="field-value-map-item__container">
<div class="field-value-map-item__source">
<avs-header-panel
*ngIf="data.valueMap['@type'] === 'static'"
[title]="data.inputFields[0].name">
<ol>
<li class="field-value-mapitem__value"
*ngFor="let value of data.inputFields[0].allowedValues">
{{value.valueText}}
</li>
</ol>
</avs-header-panel>
</div>
<div class="field-value-map-item__target">
<div *ngFor="let value of data.outputField.allowedValues">
<avs-header-panel [title]="value.valueText">
<div *ngIf="mappings && mappings[value.id]">
<div class="field-value-mapitem__mapped-value"
*ngFor="let mapping of mappings[value.id]">
{{mapping.inputValue.valueText}}
</div>
</div>
</avs-header-panel>
</div>
</div>
</div>
</div>
这是我的单元测试:
describe('component: field-mapping/FieldValueMapItemComponent', () => {
let component: FieldValueMapItemComponent
let fixture: ComponentFixture<FieldValueMapItemComponent>
let de: DebugElement
let el: HTMLElement
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
FieldValueMapItemComponent
],
imports: [
CommonModule
],
providers: [ ],
schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
})
fixture = TestBed.createComponent(FieldValueMapItemComponent)
component = fixture.componentInstance
de = fixture.debugElement.query(By.css('div'))
el = de.nativeElement
})
afterEach(() => {
fixture.destroy()
})
describe('render DOM elements', () => {
beforeEach(() => {
spyOn(component, 'ngOnChanges').and.callThrough()
component.data = CONFIGURATION.fieldMappings[0]
fixture.detectChanges()
})
it('should call ngOnChanges', () => {
expect(component.ngOnChanges).toHaveBeenCalled() // this fails!
})
})
describe('sort output data values', () => {
beforeEach(() => {
component.data = CONFIGURATION.fieldMappings[1]
fixture.detectChanges()
})
it('should sort source field allowed values by their valueText property', () => {
let valueEls = el.querySelectorAll('.field-value-mapitem__value')
let valueText = map(valueEls, 'innerText')
expect(valueText.join(',')).toBe('Active,Foo,Terminated,Zip') // this fails
})
})
})