Modal Service tests.

This commit is contained in:
Matthew Ross 2018-04-11 18:43:20 -04:00
parent 7132e26f8a
commit 68550a2276
3 changed files with 118 additions and 13 deletions

View File

@ -15,7 +15,7 @@ export class ModalService {
}
registerModal(newModal: Modal): void {
let modal = this.findModal(newModal.modalId);
let modal = this.modals.find(modal => modal.modalId === newModal.modalId);
// Delete existing to replace the modal
if (modal) {
@ -26,7 +26,7 @@ export class ModalService {
}
open(modalId: string): void {
let modal = this.findModal(modalId);
let modal = this.modals.find(modal => modal.modalId === modalId);
if (modal) {
modal.animate = (this.userOptions.show_animations);
@ -42,7 +42,7 @@ export class ModalService {
}
close(modalId: string, checkBlocking = false): void {
let modal = this.findModal(modalId);
let modal = this.modals.find(modal => modal.modalId === modalId);
if (modal) {
if (checkBlocking && modal.blocking) {
@ -54,14 +54,5 @@ export class ModalService {
}
}
private findModal(modalId: string): Modal {
for (let modal of this.modals) {
if (modal.modalId === modalId) {
return modal;
}
}
return null;
}
}

View File

@ -8,7 +8,7 @@ import { SharedModule } from '../../../../src/app/shared/shared.module';
import { ModalService } from '../../../../src/app/shared/services';
import { Modal } from '../../../../src/app/shared/modal/modal.component';
fdescribe('Modal', () => {
describe('Modal', () => {
let component: Modal,
fixture: ComponentFixture<Modal>;
@ -63,5 +63,26 @@ fdescribe('Modal', () => {
expect(called).toEqual(true);
});
it('handles the Escape key', () => {
const keyUp = component['keyup'].bind(component);
(<any>component.modalService).close = (id, checkBlocking) => {
expect(checkBlocking).toEqual(true);
};
keyUp(<any>{ keyCode: 27 });
});
it('handles the Enter key', () => {
const keyUp = component['keyup'].bind(component);
let called = false;
component.defaultActionElement = { nativeElement: {
click: () => called = true
} };
keyUp(<any>{ keyCode: 13 });
});
});

View File

@ -0,0 +1,93 @@
import { RouterTestingModule } from '@angular/router/testing';
import { TestBed, getTestBed } from '@angular/core/testing'
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
import { SharedModule } from '../../../../src/app/shared/shared.module';
import { AuthService } from '../../../../src/app/shared/services';
import { ModalService } from '../../../../src/app/shared/modal/modal.service';
describe('ModalService', () => {
let injector: TestBed;
let service: ModalService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
HttpClientTestingModule,
SharedModule
],
providers: [ModalService, AuthService]
});
injector = getTestBed();
service = injector.get(ModalService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('allows a modal to register', () => {
service.registerModal(<any>{ modalId: 'MODAL' });
expect(service['modals'].length).toEqual(1);
});
it('overwrites when registering same modalId', () => {
service.registerModal(<any>{ modalId: 'MODAL' });
service.registerModal(<any>{ modalId: 'MODAL' });
expect(service['modals'].length).toEqual(1);
});
it('can open a modal', done => {
const modal = <any>{
modalId: 'TEST',
focusElement: {
nativeElement: {
focus: () => {}
}
}
};
service['modals'].push(modal);
service['userOptions'] = <any>{ show_animations: true };
service.open(modal.modalId);
setTimeout(() => {
expect(modal.isOpen).toEqual(true);
modal.focusElement = null;
service.open(modal.modalId);
service.open('nope');
done();
}, 110);
});
it('can close a modal', () => {
const modal = <any>{
modalId: 'TEST',
blocking: true
};
service['modals'].push(modal);
service.close('nope');
service.close('TEST');
expect(modal.isOpen).toEqual(false);
modal.isOpen = true;
service.close('TEST', true);
expect(modal.isOpen).toEqual(true);
});
});