1 import { Component, NgModule, NO_ERRORS_SCHEMA, TemplateRef, ViewChild } from '@angular/core';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { By } from '@angular/platform-browser';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { BsModalRef, BsModalService, ModalModule } from 'ngx-bootstrap/modal';
14 } from '../../../../testing/unit-test-helper';
15 import { BackButtonComponent } from '../back-button/back-button.component';
16 import { ModalComponent } from '../modal/modal.component';
17 import { SubmitButtonComponent } from '../submit-button/submit-button.component';
18 import { ConfirmationModalComponent } from './confirmation-modal.component';
21 export class MockModule {}
25 <ng-template #fillTpl>
26 Template based description.
31 @ViewChild('fillTpl', { static: true })
32 fillTpl: TemplateRef<any>;
36 // Normally private, but public is needed by tests
37 constructor(public modalService: BsModalService) {}
39 private openModal(extendBaseState = {}) {
40 this.modalRef = this.modalService.show(ConfirmationModalComponent, {
41 initialState: Object.assign(
43 titleText: 'Title is a must have',
44 buttonText: 'Action label',
45 bodyTpl: this.fillTpl,
46 description: 'String based description.',
48 this.returnValue = 'The submit action has to hide manually.';
63 onCancel: () => (this.returnValue = 'If you have todo something besides hiding the modal.')
68 describe('ConfirmationModalComponent', () => {
69 let component: ConfirmationModalComponent;
70 let fixture: ComponentFixture<ConfirmationModalComponent>;
71 let mockComponent: MockComponent;
72 let mockFixture: ComponentFixture<MockComponent>;
73 let modalService: BsModalService;
74 let fh: FixtureHelper;
77 * The hide method of `BsModalService` doesn't emit events during tests that's why it's mocked.
79 * The only events of hide are `null`, `'backdrop-click'` and `'esc'` as described here:
80 * https://ngx-universal.herokuapp.com/#/modals#service-events
82 const hide = (x: string) => modalService.onHide.emit(null || x);
84 const expectReturnValue = (v: string) => expect(mockComponent.returnValue).toBe(v);
88 ConfirmationModalComponent,
94 schemas: [NO_ERRORS_SCHEMA],
95 imports: [ModalModule.forRoot(), ReactiveFormsModule, MockModule, RouterTestingModule],
96 providers: [BsModalRef, i18nProviders, SubmitButtonComponent]
100 fh = new FixtureHelper();
101 mockFixture = TestBed.createComponent(MockComponent);
102 mockComponent = mockFixture.componentInstance;
103 mockFixture.detectChanges();
104 modalService = TestBed.inject(BsModalService);
105 spyOn(modalService, 'show').and.callFake((_modalComp, config) => {
106 const data = modalServiceShow(ConfirmationModalComponent, config);
107 fixture = data.fixture;
108 component = data.component;
109 spyOn(component.modalRef, 'hide').and.callFake(hide);
110 fh.updateFixture(fixture);
115 it('should create', () => {
116 mockComponent.basicModal();
117 expect(component).toBeTruthy();
120 describe('Throws errors', () => {
121 const expectError = (config: object, expected: string) => {
122 mockComponent.basicModal();
123 component = Object.assign(component, config);
124 expect(() => component.ngOnInit()).toThrowError(expected);
127 it('has no submit action defined', () => {
132 'No submit action defined'
136 it('has no title defined', () => {
145 it('has no action name defined', () => {
148 buttonText: undefined
150 'No action name defined'
154 it('has no description defined', () => {
158 description: undefined
160 'No description defined'
165 describe('basics', () => {
167 mockComponent.basicModal();
168 spyOn(mockComponent.modalRef, 'hide').and.callFake(hide);
171 it('should show the correct title', () => {
172 expect(fh.getText('.modal-title')).toBe('Title is a must have');
175 it('should show the correct action name', () => {
176 expect(fh.getText('.tc_submitButton')).toBe('Action label');
179 it('should use the correct submit action', () => {
180 // In order to ignore the `ElementRef` usage of `SubmitButtonComponent`
182 fixture.debugElement.query(By.directive(SubmitButtonComponent)).componentInstance,
185 fh.clickElement('.tc_submitButton');
186 expect(mockComponent.modalRef.hide).toHaveBeenCalledTimes(1);
187 expect(component.modalRef.hide).toHaveBeenCalledTimes(0);
188 expectReturnValue('The submit action has to hide manually.');
191 it('should use the default cancel action', () => {
192 fh.clickElement('.tc_backButton');
193 expect(mockComponent.modalRef.hide).toHaveBeenCalledTimes(0);
194 expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
195 expectReturnValue(undefined);
198 it('should show the description', () => {
199 expect(fh.getText('.modal-body')).toBe(
200 'Template based description. String based description.'
205 describe('custom cancel action', () => {
206 const expectCancelValue = () =>
207 expectReturnValue('If you have todo something besides hiding the modal.');
210 mockComponent.customCancelModal();
213 it('should use custom cancel action', () => {
214 fh.clickElement('.tc_backButton');
218 it('should use custom cancel action if escape was pressed', () => {
223 it('should use custom cancel action if clicked outside the modal', () => {
224 hide('backdrop-click');
228 it('should unsubscribe on destroy', () => {
229 hide('backdrop-click');
231 const s = 'This value will not be changed.';
232 mockComponent.returnValue = s;
233 component.ngOnDestroy();
234 hide('backdrop-click');
235 expectReturnValue(s);