diff --git a/src/app/shared/modal/modal.service.ts b/src/app/shared/modal/modal.service.ts index c6988fe..dfbe8e7 100644 --- a/src/app/shared/modal/modal.service.ts +++ b/src/app/shared/modal/modal.service.ts @@ -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; - } } diff --git a/test/app/shared/modal/modal.component.spec.ts b/test/app/shared/modal/modal.component.spec.ts index e044dce..fd173c3 100644 --- a/test/app/shared/modal/modal.component.spec.ts +++ b/test/app/shared/modal/modal.component.spec.ts @@ -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; @@ -63,5 +63,26 @@ fdescribe('Modal', () => { expect(called).toEqual(true); }); + it('handles the Escape key', () => { + const keyUp = component['keyup'].bind(component); + + (component.modalService).close = (id, checkBlocking) => { + expect(checkBlocking).toEqual(true); + }; + + keyUp({ keyCode: 27 }); + }); + + it('handles the Enter key', () => { + const keyUp = component['keyup'].bind(component); + let called = false; + + component.defaultActionElement = { nativeElement: { + click: () => called = true + } }; + + keyUp({ keyCode: 13 }); + }); + }); diff --git a/test/app/shared/modal/modal.service.spec.ts b/test/app/shared/modal/modal.service.spec.ts index e69de29..939a51a 100644 --- a/test/app/shared/modal/modal.service.spec.ts +++ b/test/app/shared/modal/modal.service.spec.ts @@ -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({ modalId: 'MODAL' }); + + expect(service['modals'].length).toEqual(1); + }); + + it('overwrites when registering same modalId', () => { + service.registerModal({ modalId: 'MODAL' }); + service.registerModal({ modalId: 'MODAL' }); + + expect(service['modals'].length).toEqual(1); + }); + + it('can open a modal', done => { + const modal = { + modalId: 'TEST', + focusElement: { + nativeElement: { + focus: () => {} + } + } + }; + + service['modals'].push(modal); + service['userOptions'] = { 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 = { + 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); + }); + +}); +