Is it possible to mock an Attribute Directive in Angular?
The MockDirective
does work, but it was misconfigured.
export function MockDirective(options: Component): Directive {
const metadata: Directive = {
selector: options.selector,
inputs: options.inputs,
outputs: options.outputs
};
return Directive(metadata)(class _ { });
}
Turned out to be a missing mock declaration for the @Input('skinColorPicker')
property:
TestBed.configureTestingModule({
declarations: [
PrintSidebarComponent,
MockDirective({
selector: '[skinColorPicker]',
inputs: ['skinColorPicker'] }) // does work
]
})
My solution is identical to @jenson-button-event's solution, with some minor changes so it compiles in TypeScript.
export function MockDirective(options: Component): Directive {
const metadata: Directive = {
selector: options.selector,
inputs: options.inputs,
outputs: options.outputs
};
return <any>Directive(metadata)(class _ {}); // <----- add <any>
}
TestBed.configureTestingModule({
declarations: [
PrintSidebarComponent,
MockDirective({
selector: '[skinColorPicker]',
inputs: [] // <--- empty, unless the directive has inputs
})
]
})
This solution is exactly the same as @jenson-button-event's and @maia's solution, except for improved Typescript correctness. Without the improvements it did not work for me.
export function MockDirective(options: Component): Type<Directive> {
const metadata: Directive = {
selector: options.selector,
inputs: options.inputs,
outputs: options.outputs,
};
return Directive(metadata)(class MockDirectiveClass {
});
}
TestBed.configureTestingModule({
declarations: [
PrintSidebarComponent,
MockDirective({
selector: '[skinColorPicker]',
inputs: [] // <--- empty, unless the directive has inputs
})
]
})