ng2-semantic-ui
ng2-semantic-ui copied to clipboard
Can you unit test modal components?
I have a component for a confirm modal similar to the example in the docs, and when I unit test the component, I get this:
Error: StaticInjectorError(DynamicTestModule)[ConfirmModalComponent -> Modal]:
StaticInjectorError(Platform: core)[ConfirmModalComponent -> Modal]:
NullInjectorError: No provider for Modal!
Adding Modal to the providers array results in this error:
Failed: Can't resolve all parameters for Modal: (?, ?).
Adding SuiModal to providers array does nothing.
Test imports:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ConfirmModalComponent],
providers: [
Modal,
],
})
.compileComponents();
}));
Component
export class ConfirmModalComponent implements OnInit {
constructor(public modal: SuiModal<IConfirmModalContext, void, void>) { }
ngOnInit() { }
}
Found a way around by mocking the SuiModal class like so:
const modalContext: IConfirmModalContext = {
title: 'Title',
question: 'test?',
};
const fakeModalService = {
approve: () => null,
context: modalContext,
};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ConfirmModalComponent],
providers: [
{ provide: SuiModal, useValue: fakeModalService },
],
})
.compileComponents();
}));