Когда я пытался запустить приведенный ниже модульный тест, выдает ошибку «elements.map не является функцией» в этой строке const arrayValues = elements.map((val: Element) => this.getAttrval(val));
component.ts, имеющий следующий код:
getAttrval(val: Element): BrowseUri[] {
console.log(val);
const href: BrowseUri[] = [];
let label = val.getAttribute('label');
let browseUri = val.getAttribute('browseUri');
let id = val.getAttribute('refId');
href.push({ label, browseUri, id });
while (val.hasChildNodes()) {
val = val.children[0];
label = val.getAttribute('label');
browseUri = val.getAttribute('browseUri');
id = val.getAttribute('refId');
href.push({ label, browseUri, id });
}
console.log(href);
return href;
}
parseWhereusedFilters(data: string): any {
var data = '<refs id="4005" refCount="3"><node browseUri="/ca:4/" id="4" label="/" refId="" type="ca"><node browseUri="/ca:4/caref:4008/" ext="text/xml" id="4008" label="XXX" refId="4005" type="caref"/><node browseUri="/ca:4/caref:4037/" ext="text/xml" id="4037" label="doczone" refId="4034" type="caref"><node browseUri="/ca:4/caref:4037/caref:4039/" ext="text/xml" id="4039" label="xxx" refId="4016" type="caref"><node browseUri="/ca:4/caref:4037/caref:4039/caref:4021/" ext="text/xml" id="4021" label="xxx" refId="4005" type="caref"/></node></node><node browseUri="/ca:4/caref:4019/" ext="text/xml" id="4019" label="xxx" refId="4016" type="caref"><node browseUri="/ca:4/caref:4019/caref:4021/" ext="text/xml" id="4021" label="xxx" refId="4005" type="caref"/></node></node></refs>';
const xml = this.xmlParser.parseFromString(data);
const elements = this.xmlParser['getChildren']('node', xml);
console.log(elements);
const arrayValues = elements.map((val: Element) => this.getAttrval(val));
return arrayValues;
}
compoenent.spec.ts, имеющий следующий код:
it('should parse the where used data', inject([XmlParser], (xmlParser: XmlParser) => {
const doc = new Document();
const refs = doc.createElement('refs');
for (let i = 0; i < 4; i++) {
const node = doc.createElement('node');
refs.appendChild(node);
}
doc.appendChild(refs);
let response;
spyOn(xmlParser, 'parseFromString').and.returnValue(of({response}));
spyOn(xmlParser, 'getChildren').and.returnValue(of({response}));
spyOn(component, 'getAttrval').and.returnValue(of({response}));
const items = component.parseWhereusedFilters('<refs id="4005" refCount="3"><node browseUri="/ca:4/" id="4" label="/" refId="" type="ca"><node browseUri="/ca:4/caref:4008/" ext="text/xml" id="4008" label="xxx" refId="4005" type="caref"/><node browseUri="/ca:4/caref:4037/" ext="text/xml" id="4037" label="doczone" refId="4034" type="caref"><node browseUri="/ca:4/caref:4037/caref:4039/" ext="text/xml" id="4039" label="xxx" refId="4016" type="caref"><node browseUri="/ca:4/caref:4037/caref:4039/caref:4021/" ext="text/xml" id="4021" label="xxx" refId="4005" type="caref"/></node></node><node browseUri="/ca:4/caref:4019/" ext="text/xml" id="4019" label="xxx" refId="4016" type="caref"><node browseUri="/ca:4/caref:4019/caref:4021/" ext="text/xml" id="4021" label="xxx" refId="4005" type="caref"/></node></node></refs>');
fixture.detectChanges();
expect(items.length).toEqual(4);
}));