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 { NgbActiveModal, NgbModalModule, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
9 import { configureTestBed, FixtureHelper } from '../../../../testing/unit-test-helper';
10 import { ModalService } from '../../services/modal.service';
11 import { BackButtonComponent } from '../back-button/back-button.component';
12 import { ModalComponent } from '../modal/modal.component';
13 import { SubmitButtonComponent } from '../submit-button/submit-button.component';
14 import { ConfirmationModalComponent } from './confirmation-modal.component';
17 export class MockModule {}
21 <ng-template #fillTpl>
22 Template based description.
27 @ViewChild('fillTpl', { static: true })
28 fillTpl: TemplateRef<any>;
29 modalRef: NgbModalRef;
32 // Normally private, but public is needed by tests
33 constructor(public modalService: ModalService) {}
35 private openModal(extendBaseState = {}) {
36 this.modalRef = this.modalService.show(
37 ConfirmationModalComponent,
40 titleText: 'Title is a must have',
41 buttonText: 'Action label',
42 bodyTpl: this.fillTpl,
43 description: 'String based description.',
45 this.returnValue = 'The submit action has to hide manually.';
59 onCancel: () => (this.returnValue = 'If you have todo something besides hiding the modal.')
64 describe('ConfirmationModalComponent', () => {
65 let component: ConfirmationModalComponent;
66 let fixture: ComponentFixture<ConfirmationModalComponent>;
67 let mockComponent: MockComponent;
68 let mockFixture: ComponentFixture<MockComponent>;
69 let fh: FixtureHelper;
71 const expectReturnValue = (v: string) => expect(mockComponent.returnValue).toBe(v);
75 ConfirmationModalComponent,
81 schemas: [NO_ERRORS_SCHEMA],
82 imports: [ReactiveFormsModule, MockModule, RouterTestingModule, NgbModalModule],
83 providers: [NgbActiveModal, SubmitButtonComponent]
87 fh = new FixtureHelper();
88 mockFixture = TestBed.createComponent(MockComponent);
89 mockComponent = mockFixture.componentInstance;
90 mockFixture.detectChanges();
92 spyOn(TestBed.inject(ModalService), 'show').and.callFake((_modalComp, config) => {
93 fixture = TestBed.createComponent(ConfirmationModalComponent);
94 component = fixture.componentInstance;
95 component = Object.assign(component, config);
96 component.activeModal = { close: () => true } as any;
97 spyOn(component.activeModal, 'close').and.callThrough();
98 fh.updateFixture(fixture);
102 it('should create', () => {
103 mockComponent.basicModal();
104 expect(component).toBeTruthy();
107 describe('Throws errors', () => {
108 const expectError = (config: object, expected: string) => {
109 mockComponent.basicModal();
110 component = Object.assign(component, config);
111 expect(() => component.ngOnInit()).toThrowError(expected);
114 it('has no submit action defined', () => {
119 'No submit action defined'
123 it('has no title defined', () => {
132 it('has no action name defined', () => {
135 buttonText: undefined
137 'No action name defined'
141 it('has no description defined', () => {
145 description: undefined
147 'No description defined'
152 describe('basics', () => {
154 mockComponent.basicModal();
155 spyOn(component, 'onSubmit').and.callThrough();
158 it('should show the correct title', () => {
159 expect(fh.getText('.modal-title')).toBe('Title is a must have');
162 it('should show the correct action name', () => {
163 expect(fh.getText('.tc_submitButton')).toBe('Action label');
166 it('should use the correct submit action', () => {
167 // In order to ignore the `ElementRef` usage of `SubmitButtonComponent`
169 fixture.debugElement.query(By.directive(SubmitButtonComponent)).componentInstance,
172 fh.clickElement('.tc_submitButton');
173 expect(component.onSubmit).toHaveBeenCalledTimes(1);
174 expect(component.activeModal.close).toHaveBeenCalledTimes(0);
175 expectReturnValue('The submit action has to hide manually.');
178 it('should use the default cancel action', () => {
179 fh.clickElement('.tc_backButton');
180 expect(component.onSubmit).toHaveBeenCalledTimes(0);
181 expect(component.activeModal.close).toHaveBeenCalledTimes(1);
182 expectReturnValue(undefined);
185 it('should show the description', () => {
186 expect(fh.getText('.modal-body')).toBe(
187 'Template based description. String based description.'