简而言之,我正在寻找一种方法使谓词By.css([css-selector])
(参见https://angular.io/docs/ts/latest/api/platform-browser/index/By-class.html)在命名空间(SVG)时在Angular2(v2.0.1 +)组件单元测试中工作参与其中。
(相关谓词By.all()
和By.directive(DirectiveClass)
工作正常。)
以下是展示此问题的独立规范。结果是
DivComponent class specs:
✔ NG should evaluate component and template.
✔ Div should be locatable by tag.
✔ Div should be locatable by id.
✔ Div should be locatable by class.
✔ Match should fail a negative.
ShapesComponent class specs:
✔ NG should evaluate the component and template.
✖ Circle should be locatable by tag.
✖ Circle should be locatable by ns:tag.
✖ Circle should be locatable by id.
✖ Circle should be locatable by class.
规格:
import {TestBed, ComponentFixture} from '@angular/core/testing';
import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
/*
* A simple component in HTML namespace
*/
@Component({
selector: '[app-div]',
template: '<div class="myDivClass" id="myDivId">Hello World</div>'
})
export class DivComponent {
}
/*
* A simple component in SVG namespace
*/
@Component({
selector: '[app-shape]',
template: '<svg:circle class="myCircleClass" id="myCircleId" ' +
'cx="80" cy="80" r="20"></svg:circle>'
})
export class ShapesComponent {
}
function dumpElements(elements: DebugElement[]) {
console.log('Matcher found ' + elements.length + ' elements.');
for (let i = 0; i < elements.length; i++) {
console.log('element[' + i + '] = '
+ elements[i].nativeElement.tagName
+ ': ' + elements[i].nativeElement.outerHTML);
}
}
/*
* Test "By.css()" on the DivComponent.
*/
describe('DivComponent class specs:', () => {
let divComponent: DivComponent;
let fixture: ComponentFixture<DivComponent>;
let de: DebugElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
DivComponent
]
});
TestBed.compileComponents();
fixture = TestBed.createComponent(DivComponent);
fixture.detectChanges();
de = fixture.debugElement;
});
it('NG should evaluate component and template.', () => {
// Dump elements if needed:
// dumpElements(de.queryAll(By.all()));
expect(de.queryAll(By.all()).length).toBe(1);
});
it('Div should be locatable by tag.', () => {
expect(de.queryAll(By.css('div')).length).toBe(1);
});
it('Div should be locatable by id.', () => {
expect(de.queryAll(By.css('#myDivId')).length).toBe(1);
});
it('Div should be locatable by class.', () => {
expect(de.queryAll(By.css('.myDivClass')).length).toBe(1);
});
it('Match should fail a negative.', () => {
expect(de.queryAll(By.css('#negative')).length).toBe(0);
});
});
/*
* Use the same "By.css()" syntax on the ShapeComponent.
*/
describe('ShapesComponent class specs:', () => {
let divComponent: ShapesComponent;
let fixture: ComponentFixture<ShapesComponent>;
let de: DebugElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
ShapesComponent
]
});
TestBed.compileComponents();
fixture = TestBed.createComponent(ShapesComponent);
fixture.detectChanges();
de = fixture.debugElement;
});
it('NG should evaluate the component and template.', () => {
// Dump elements if needed:
// dumpElements(de.queryAll(By.all()));
expect(de.queryAll(By.all()).length).toBe(1); // pass
});
it('Circle should be locatable by tag.', () => {
expect(de.queryAll(By.css('circle')).length).toBe(1);
});
it('Circle should be locatable by ns:tag.', () => {
expect(de.queryAll(By.css('svg:circle')).length).toBe(1);
});
it('Circle should be locatable by id.', () => {
expect(de.queryAll(By.css('#myCircleId')).length).toBe(1);
});
it('Circle should be locatable by class.', () => {
expect(de.queryAll(By.css('.myCircleClass')).length).toBe(1);
});
});
答案 0 :(得分:0)
@herkulano发表评论:
通过在nativeElement上执行querySelector来实现它:element.nativeElement.querySelector('。chart')
答案 1 :(得分:0)
通过在nativeElement上执行querySelector来实现它:
element.nativeElement.querySelector('.chart')
答案 2 :(得分:0)
我知道这是一个老问题,但我也在Angular 5中遇到这个问题。如果我这样做,它不起作用:
debugElement.query(By.css('svg.underline'))
但是,如果我这样做,它可以正常工作:
debugElement.query(By.css('svg[class=underline]'))