I have a component that uses one of two helper classes, like:
import {HelperA} ...
import {HelperB} ...
...
@Component({..})
export class MyComponent implements OnInit {
helper: Helper;
constructor(private ref: ElementRef, private device: MyDeviceDetectionService) {}
ngOnInit() {
if (this.device.isMobile) {
this.helper = new HelperA(this.ref);
} else {
this.helper = new HelperB(this.ref);
}
}
}
I realise that this is hard to unit test, so how can I inject these? Ideally I only need one of them, depending on whether isMobile
is true or false.
It's possible you can push all of that out to the injector. Assuming the two helpers have a common superclass named
Helper
, use theuseFactory
provider option to create whichever you need:Then the factory would look like:
Note that this will have to be in the component's providers array, as the element reference isn't available at the module level.