Javascript 如何测试角度中的转置内容?
在测试带有转置槽的角度组件时,我们没有 显式是指检查转写的内容是否按预期放置在组件内部。 例如:Javascript 如何测试角度中的转置内容?,javascript,angular,unit-testing,testing,Javascript,Angular,Unit Testing,Testing,在测试带有转置槽的角度组件时,我们没有 显式是指检查转写的内容是否按预期放置在组件内部。 例如: // base-button.component.ts @Component({ selector: 'base-button', template: `<button [type]="type"> <ng-content></ng-content> </button>`, }) export class BaseButtonCom
// base-button.component.ts
@Component({
selector: 'base-button',
template: `<button [type]="type">
<ng-content></ng-content>
</button>`,
})
export class BaseButtonComponent {
@Input() type = 'button';
}
我们可以对组件的每个属性和方法执行此操作,但是
转载内容?一种解决方法是为测试目的创建主机组件:
// base-button.component.spec.ts
...
@Component({
template: `<base-button>Foo bar</base-button>`
})
export class BaseButtonHostComponent {}
...
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BaseButtonComponent, BaseButtonHostComponent ]
})
.compileComponents();
}));
it('should transclude the content correctly', () => {
const hostFixture = TestBed.createComponent(BaseButtonHostComponent);
hostFixture.detectChanges();
const button = hostFixture.nativeElement.querySelector('button');
expect(button.textContent === 'Foo bar');
});
...
//base-button.component.spec.ts
...
@组成部分({
模板:`foobar`
})
导出类BaseButtonHostComponent{}
...
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[BaseButtonComponent,BaseButtonHostComponent]
})
.compileComponents();
}));
它('应该正确地排除内容',()=>{
const hostFixture=TestBed.createComponent(BaseButtonHostComponent);
hostFixture.detectChanges();
const button=hostFixture.nativeElement.querySelector('button');
expect(button.textContent==='foobar');
});
...
但是,正如你所能想象的,这是相当不方便的,也是因为必须这样做
对于包含转置内容的每个组件,以及可能的每个
元素
在其模板中。还有其他方法吗?确实有一种相当模糊的方法。基本上,
TestBed.createComponent
调用
组件的工厂create
方法,该方法还支持要创建的可投影DOM节点
插入转换槽
// @angular/core/testing.js
createComponent(component) {
...
const componentFactory = this._compiler.getComponentFactory(component);
...
const componentRef = componentFactory.create(Injector.NULL, [], `#${rootElId}`, this._moduleRef);
...
}
我们也必须这样做,这里有个窍门:
// base-button.component.spec.ts
describe('BaseButtonComponent', () => {
let factory: ComponentFactory<BaseButtonComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BaseButtonComponent ]
})
.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [ BaseButtonComponent ]
}
})
.compileComponents();
const resolver = <ComponentFactoryResolver>TestBed.get(ComponentFactoryResolver, null);
factory = resolver.resolveComponentFactory(BaseButtonComponent);
}));
it('should transclude the provided nodes into the button', () => {
const tnode = document.createTextNode('Foo bar');
const componentRef = factory.create(Injector.NULL, [[ tnode ]]);
const button = componentRef.location.nativeElement.querySelector('button');
expect(button.textContent === 'Foo bar');
});
});
很遗憾,
TestBed.createComponent
不允许立即这样做。对我不起作用。我最终做了如下的事情:
// base-button.component.spec.ts
describe('BaseButtonComponent', () => {
let factory: ComponentFactory<BaseButtonComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BaseButtonComponent ]
})
.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [ BaseButtonComponent ]
}
})
.compileComponents();
const resolver = <ComponentFactoryResolver>TestBed.get(ComponentFactoryResolver, null);
factory = resolver.resolveComponentFactory(BaseButtonComponent);
}));
it('should transclude the provided nodes into the button', () => {
const tnode = document.createTextNode('Foo bar');
const componentRef = factory.create(Injector.NULL, [[ tnode ]]);
const button = componentRef.location.nativeElement.querySelector('button');
expect(button.textContent === 'Foo bar');
});
});
function createComponentWithContents(factory, ...contents) {
const template = document.createElement('template');
const projectableNodes = contents.map(html => {
template.innerHTML = html;
return [ ...template.content.childNodes ];
});
return factory.create(Injector.NULL, projectableNodes);
}
const componentRef = createComponentWithContents(factory, '<i class="fa fa-star"></i> Win!');