diff --git a/package.json b/package.json index 5dda64e2..17162bb2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-emfular-tool", - "version": "1.1.1", + "version": "1.1.2", "description": "Small Angular library with helper schematics for EMFular, currently alerting, history, and io facilities", "keywords": ["EMFular","Angular", "IO", "alert"], "author": { diff --git a/src/lib/modal/modal-ref.spec.ts b/src/lib/modal/modal-ref.spec.ts index 5567a94b..30899f98 100644 --- a/src/lib/modal/modal-ref.spec.ts +++ b/src/lib/modal/modal-ref.spec.ts @@ -4,51 +4,41 @@ import {ModalRef} from './modal-ref'; describe('ModalRef', () => { - it('closes the overlay', () => { - const overlayRef = { - hasAttached: vi.fn().mockReturnValue(true), - dispose: vi.fn() - } as unknown as OverlayRef; - - const ref = new ModalRef(overlayRef); - - ref.close(); - - expect(overlayRef.dispose).toHaveBeenCalledOnce(); - }); - it('emits the result when closed', () => { const overlayRef = { hasAttached: vi.fn().mockReturnValue(true), dispose: vi.fn() } as unknown as OverlayRef; - const ref = new ModalRef(overlayRef); - let result: string | undefined; - ref.closed.subscribe(value => result = value); - ref.close('hello'); - expect(result).toBe('hello'); }); - it('emits undefined when closed without a result', () => { + + it('emits a falsly result when closed', () => { + const overlayRef = { + hasAttached: vi.fn().mockReturnValue(true), + dispose: vi.fn() + } as unknown as OverlayRef; + const ref = new ModalRef(overlayRef); + let result: boolean | undefined = undefined; + ref.closed.subscribe(value => result = value); + ref.close(false); + expect(result).toBe(false); + }); + it('emits nothing when closed without a result', () => { const overlayRef = { hasAttached: vi.fn().mockReturnValue(true), dispose: vi.fn() } as unknown as OverlayRef; - const ref = new ModalRef(overlayRef); - let result: string | undefined = 'initial'; - ref.closed.subscribe(value => result = value); - ref.close(); - - expect(result).toBeUndefined(); + expect(overlayRef.dispose).toHaveBeenCalledOnce(); + expect(result).toEqual('initial'); }); it('completes closed when closed', () => { @@ -56,17 +46,12 @@ describe('ModalRef', () => { hasAttached: vi.fn().mockReturnValue(true), dispose: vi.fn() } as unknown as OverlayRef; - const ref = new ModalRef(overlayRef); - let completed = false; - ref.closed.subscribe({ complete: () => completed = true }); - ref.close(); - expect(completed).toBe(true); }); @@ -75,15 +60,10 @@ describe('ModalRef', () => { hasAttached: vi.fn().mockReturnValue(false), dispose: vi.fn() } as unknown as OverlayRef; - const ref = new ModalRef(overlayRef); - let emitted = false; - ref.closed.subscribe(() => emitted = true); - ref.close(); - expect(overlayRef.dispose).not.toHaveBeenCalled(); expect(emitted).toBe(false); }); @@ -93,17 +73,13 @@ describe('ModalRef', () => { hasAttached: vi.fn().mockReturnValue(true), dispose: vi.fn() } as unknown as OverlayRef; - // After dispose(), simulate the overlay no longer being attached. overlayRef.hasAttached .mockReturnValueOnce(true) .mockReturnValueOnce(false); - - const ref = new ModalRef(overlayRef); - + const ref: ModalRef = new ModalRef(overlayRef); ref.close('first'); ref.close('second'); - expect(overlayRef.dispose).toHaveBeenCalledOnce(); }); diff --git a/src/lib/modal/modal-ref.ts b/src/lib/modal/modal-ref.ts index 5c7be3b1..371d08f9 100644 --- a/src/lib/modal/modal-ref.ts +++ b/src/lib/modal/modal-ref.ts @@ -3,8 +3,7 @@ import {OverlayRef} from "@angular/cdk/overlay"; export class ModalRef { - private readonly closedSubject = new Subject(); - + private readonly closedSubject = new Subject(); readonly closed = this.closedSubject.asObservable(); constructor( @@ -15,9 +14,10 @@ export class ModalRef { if (!this.overlayRef.hasAttached()) { return; } - this.overlayRef.dispose(); - this.closedSubject.next(result); + if (result !== undefined) { + this.closedSubject.next(result); + } this.closedSubject.complete(); } } \ No newline at end of file diff --git a/src/lib/modal/modal.service.spec.ts b/src/lib/modal/modal.service.spec.ts index 6ed3c4d7..06fadb39 100644 --- a/src/lib/modal/modal.service.spec.ts +++ b/src/lib/modal/modal.service.spec.ts @@ -1,7 +1,7 @@ import {Component} from '@angular/core'; import {TestBed} from '@angular/core/testing'; -import {OverlayContainer} from '@angular/cdk/overlay'; -import {describe, expect, it, beforeEach, afterEach} from 'vitest'; +import {Overlay, OverlayContainer} from '@angular/cdk/overlay'; +import {describe, expect, it, beforeEach, afterEach, vi} from 'vitest'; import {ModalService} from './modal.service'; @Component({ @@ -12,6 +12,7 @@ class TestModalComponent {} describe('ModalService', () => { let service: ModalService; + let overlay: Overlay; let overlayContainer: OverlayContainer; let overlayElement: HTMLElement; @@ -21,6 +22,7 @@ describe('ModalService', () => { }); service = TestBed.inject(ModalService); + overlay = TestBed.inject(Overlay); overlayContainer = TestBed.inject(OverlayContainer); overlayElement = overlayContainer.getContainerElement(); }); @@ -54,18 +56,18 @@ describe('ModalService', () => { expect(result).toBe('hello'); }); - it('emits undefined when the modal is closed without a result', () => { + it('emits nothing when the modal is closed without a result', () => { const instance = service.createModal< TestModalComponent, string >(TestModalComponent, {}); - let result: string | undefined = 'not-closed'; + let result: string = 'not-closed'; instance.ref.closed.subscribe(value => { result = value; }); instance.ref.close(); - expect(result).toBeUndefined(); + expect(result).toEqual('not-closed'); }); it('closes without a result when the backdrop is clicked', () => { @@ -86,7 +88,33 @@ describe('ModalService', () => { expect(backdrop).not.toBeNull(); backdrop.click(); - expect(result).toBeUndefined(); + expect(result).toEqual('not-closed'); + }); + + it('uses the explicitly given position strategy', () => { + const positionStrategy = overlay.position() + .global() + .top('10px'); + const createSpy = vi.spyOn(overlay, 'create'); + const instance = service.createModal(TestModalComponent, { + positionStrategy + }); + expect(createSpy).toHaveBeenCalledWith( + expect.objectContaining({ + positionStrategy + }) + ); + instance.ref.close(); }); + it('uses the centered position strategy by default', () => { + const createSpy = vi.spyOn(overlay, 'create'); + const instance = service.createModal(TestModalComponent, {}); + expect(createSpy).toHaveBeenCalledWith( + expect.objectContaining({ + positionStrategy: service['defaultStrategyCentered'] + }) + ); + instance.ref.close(); + }); }); \ No newline at end of file diff --git a/src/lib/modal/modal.service.ts b/src/lib/modal/modal.service.ts index 6173917d..374f4214 100644 --- a/src/lib/modal/modal.service.ts +++ b/src/lib/modal/modal.service.ts @@ -1,5 +1,5 @@ import {Injectable} from "@angular/core"; -import {Overlay, OverlayConfig} from "@angular/cdk/overlay"; +import {Overlay, OverlayConfig, PositionStrategy} from "@angular/cdk/overlay"; import {ComponentPortal, ComponentType} from "@angular/cdk/portal"; import {ModalRef} from "./modal-ref"; import {ModalInstance} from "./modal-instance"; @@ -9,20 +9,29 @@ import {ModalInstance} from "./modal-instance"; }) export class ModalService { + private defaultStrategyCentered: PositionStrategy; + constructor( private readonly overlay: Overlay, - ) {} + ) { + this.defaultStrategyCentered = this.overlay.position().global() + .centerHorizontally() + .centerVertically() + } createModal( component: ComponentType, config: OverlayConfig ): ModalInstance { - const overlayRef = this.overlay.create(config); + const overlayConfig: OverlayConfig = { + ...config, + positionStrategy: config.positionStrategy ?? this.defaultStrategyCentered + }; + const overlayRef = this.overlay.create(overlayConfig); const ref = new ModalRef(overlayRef); overlayRef.backdropClick().subscribe(() => { ref.close(); }); - const componentRef = overlayRef.attach( new ComponentPortal(component) );