我正在使用一些实现ShadowDOM v1规范的Polymer 2组件,我需要在shadowRoots中选择元素(例如输入)来运行单元测试,并使用Jasmine进行少量集成测试。
预期行为:
我们应该能够使用Jasmine选择器访问Shadow DOM元素。
var allInputs = fixture.debugElement.queryAll(By.css(' input')); //不会在shadow DOM中提供输入元素。
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
describe('AppComponent', () =>
{
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() =>
{
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
FormsModule
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
}));
it('should have default value set for first name and last name', () =>
{
fixture.detectChanges();
var allInputs = fixture.debugElement.queryAll(By.css('input'));
var firstNameInput = allInputs[0].nativeElement; // Unable to retrieve the input element from the polymer web component.
var lastNameInput = allInputs[1].nativeElement; // Unable to retrieve the input element from the polymer web component.
expect(firstNameInput.value).toBe('');
expect(lastNameInput.value).toBe('');
});
});
在Angular页面中测试Polymer Web Components的最佳方法是什么? 或者我们是否有机制在单元测试Angular组件时模拟Web组件?
我的环境:
Angular CLI:1.6.4 节点:6.10.0 Angular:5.2.0
&#34; devDependencies&#34;:{ &#34; karma-jasmine&#34;:&#34; ^ 0.3.6&#34;, &#34; jasmine&#34;:&#34; ^ 2.4.1&#34;, &#34; jasmine-core&#34;:&#34; ^ 2.2.0&#34; }