1 import { Component, ViewChild } from '@angular/core';
2 import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { FormGroupDirective, ReactiveFormsModule } from '@angular/forms';
5 import { ModalModule } from 'ngx-bootstrap';
6 import { Observable } from 'rxjs/Observable';
7 import { Subscriber } from 'rxjs/Subscriber';
9 import { ModalComponent } from '../modal/modal.component';
10 import { SubmitButtonComponent } from '../submit-button/submit-button.component';
11 import { DeletionLinkComponent } from './deletion-link.component';
15 <cd-deletion-link #ctrlDeleteButton
16 metaType="Controller delete handling"
18 (toggleDeletion)="fakeDeleteController()">
19 The spinner is handled by the controller if you have use the modal as ViewChild in order to
20 use it's functions to stop the spinner or close the dialog.
22 <cd-deletion-link #modalDeleteButton
23 metaType="Modal delete handling"
24 [deletionObserver]="fakeDelete()"
26 The spinner is handled by the modal if your given deletion function returns a Observable.
31 @ViewChild('ctrlDeleteButton') ctrlDeleteButton: DeletionLinkComponent;
32 @ViewChild('modalDeleteButton') modalDeleteButton: DeletionLinkComponent;
33 someData = [1, 2, 3, 4, 5];
37 this.finished = [6, 7, 8, 9];
41 return (): Observable<any> => {
42 return new Observable((observer: Subscriber<any>) => {
43 Observable.timer(100).subscribe(() => {
44 observer.next(this.finish());
51 fakeDeleteController() {
52 Observable.timer(100).subscribe(() => {
54 this.ctrlDeleteButton.hideModal();
59 describe('DeletionLinkComponent', () => {
60 let mockComponent: MockComponent;
61 let component: DeletionLinkComponent;
62 let fixture: ComponentFixture<MockComponent>;
64 beforeEach(async(() => {
65 TestBed.configureTestingModule({
66 declarations: [ MockComponent, DeletionLinkComponent, ModalComponent,
67 SubmitButtonComponent],
68 imports: [ModalModule.forRoot(), ReactiveFormsModule],
74 fixture = TestBed.createComponent(MockComponent);
75 mockComponent = fixture.componentInstance;
76 component = mockComponent.ctrlDeleteButton;
77 fixture.detectChanges();
80 it('should create', () => {
81 expect(component).toBeTruthy();
84 describe('component functions', () => {
86 const mockShowModal = () => {
87 component.showModal(null);
90 const changeValue = (value) => {
91 component.confirmation.setValue(value);
92 component.confirmation.markAsDirty();
93 component.confirmation.updateValueAndValidity();
97 spyOn(component.modalService, 'show').and.returnValue({
102 it('should test showModal', () => {
103 changeValue('something');
104 expect(mockShowModal).toBeTruthy();
105 expect(component.confirmation.value).toBe('something');
106 expect(component.modalService.show).not.toHaveBeenCalled();
108 expect(component.modalService.show).toHaveBeenCalled();
109 expect(component.confirmation.value).toBe(null);
110 expect(component.confirmation.pristine).toBe(true);
113 it('should test hideModal', () => {
114 expect(component.bsModalRef).not.toBeTruthy();
116 expect(component.bsModalRef).toBeTruthy();
117 expect(component.hideModal).toBeTruthy();
118 spyOn(component.bsModalRef, 'hide').and.stub();
119 expect(component.bsModalRef.hide).not.toHaveBeenCalled();
120 component.hideModal();
121 expect(component.bsModalRef.hide).toHaveBeenCalled();
124 describe('invalid control', () => {
126 const testInvalidControl = (submitted: boolean, error: string, expected: boolean) => {
127 expect(component.invalidControl(submitted, error)).toBe(expected);
131 component.deletionForm.reset();
134 it('should test empty values', () => {
135 expect(component.invalidControl).toBeTruthy();
136 component.deletionForm.reset();
137 testInvalidControl(false, undefined, false);
138 testInvalidControl(true, 'required', true);
139 component.deletionForm.reset();
140 changeValue('let-me-pass');
142 testInvalidControl(true, 'required', true);
145 it('should test pattern', () => {
146 changeValue('let-me-pass');
147 testInvalidControl(false, 'pattern', true);
148 changeValue('ctrl-test');
149 testInvalidControl(false, undefined, false);
150 testInvalidControl(true, undefined, false);
154 describe('deletion call', () => {
156 spyOn(component.toggleDeletion, 'emit');
157 spyOn(component, 'stopLoadingSpinner');
158 spyOn(component, 'hideModal').and.stub();
161 describe('Controller driven', () => {
164 expect(component.toggleDeletion.emit).not.toHaveBeenCalled();
165 expect(component.stopLoadingSpinner).not.toHaveBeenCalled();
166 expect(component.hideModal).not.toHaveBeenCalled();
169 it('should delete without doing anything but call emit', () => {
170 component.deletionCall();
171 expect(component.stopLoadingSpinner).not.toHaveBeenCalled();
172 expect(component.hideModal).not.toHaveBeenCalled();
173 expect(component.toggleDeletion.emit).toHaveBeenCalled();
176 it('should test fake deletion that closes modal', <any>fakeAsync(() => {
177 mockComponent.fakeDeleteController();
178 expect(component.hideModal).not.toHaveBeenCalled();
179 expect(mockComponent.finished).toBe(undefined);
181 expect(component.hideModal).toHaveBeenCalled();
182 expect(mockComponent.finished).toEqual([6, 7, 8, 9]);
186 describe('Modal driven', () => {
187 it('should delete and close modal', <any>fakeAsync(() => {
188 component = mockComponent.modalDeleteButton;
190 spyOn(component.toggleDeletion, 'emit');
191 spyOn(component, 'stopLoadingSpinner');
192 spyOn(component, 'hideModal').and.stub();
193 spyOn(mockComponent, 'fakeDelete');
195 component.deletionCall();
196 expect(mockComponent.finished).toBe(undefined);
197 expect(component.toggleDeletion.emit).not.toHaveBeenCalled();
198 expect(component.hideModal).not.toHaveBeenCalled();
201 expect(component.toggleDeletion.emit).not.toHaveBeenCalled();
202 expect(component.stopLoadingSpinner).not.toHaveBeenCalled();
203 expect(component.hideModal).toHaveBeenCalled();
204 expect(mockComponent.finished).toEqual([6, 7, 8, 9]);