forked from Ks89/angular-modal-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5fee43
commit ec23c05
Showing
5 changed files
with
211 additions
and
38 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
.../angular-modal-gallery/src/lib/components/modal-gallery/attach-to-overlay.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { inject, TestBed, waitForAsync } from '@angular/core/testing'; | ||
import { AttachToOverlayService } from './attach-to-overlay.service'; | ||
import { ModalGalleryService } from './modal-gallery.service'; | ||
import { OverlayModule, OverlayRef } from '@angular/cdk/overlay'; | ||
import { Image } from '../../model/image.class'; | ||
import { ModalGalleryRef } from './modal-gallery-ref'; | ||
|
||
const IMAGES: Image[] = [ | ||
new Image(0, { | ||
// modal | ||
img: '../assets/images/gallery/img1.jpg', | ||
extUrl: 'http://www.google.com' | ||
}), | ||
new Image(1, { | ||
// modal | ||
img: '../assets/images/gallery/img2.png', | ||
description: 'Description 2' | ||
}), | ||
new Image( | ||
2, | ||
{ | ||
// modal | ||
img: '../assets/images/gallery/img3.jpg', | ||
description: 'Description 3', | ||
extUrl: 'http://www.google.com' | ||
}, | ||
{ | ||
// plain | ||
img: '../assets/images/gallery/thumbs/img3.png', | ||
title: 'custom title 2', | ||
alt: 'custom alt 2', | ||
ariaLabel: 'arial label 2' | ||
} | ||
), | ||
new Image(3, { | ||
// modal | ||
img: '../assets/images/gallery/img4.jpg', | ||
description: 'Description 4', | ||
extUrl: 'http://www.google.com' | ||
}), | ||
new Image( | ||
4, | ||
{ | ||
// modal | ||
img: '../assets/images/gallery/img5.jpg' | ||
}, | ||
{ | ||
// plain | ||
img: '../assets/images/gallery/thumbs/img5.jpg' | ||
} | ||
) | ||
]; | ||
|
||
describe('AttachToOverlayService', () => { | ||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [OverlayModule], | ||
providers: [ | ||
AttachToOverlayService, | ||
{ | ||
provide: ModalGalleryService, | ||
useClass: ModalGalleryService | ||
} | ||
] | ||
}); | ||
})); | ||
|
||
it('should instantiate service when inject service', inject([AttachToOverlayService], (service: AttachToOverlayService) => { | ||
expect(service instanceof AttachToOverlayService).toEqual(true); | ||
})); | ||
|
||
describe('#attachToOverlay()', () => { | ||
describe('---YES---', () => { | ||
it('should call the attach method on the given overlayRef', inject([ModalGalleryService], (modalGalleryService: ModalGalleryService) => { | ||
const ID: number = 1; | ||
const config = { | ||
id: ID, | ||
images: IMAGES, | ||
currentImage: IMAGES[0] | ||
}; | ||
const ref: ModalGalleryRef | undefined = modalGalleryService.open({ | ||
id: ID, | ||
images: IMAGES, | ||
currentImage: IMAGES[0] | ||
}); | ||
expect(ref).toBeDefined(); | ||
expect(ref instanceof ModalGalleryRef).toBeTrue(); | ||
let attachCalled = false; | ||
const mockOverlayRef = { | ||
attach: () => { | ||
attachCalled = true; | ||
} | ||
}; | ||
|
||
modalGalleryService.triggerAttachToOverlay.emit({ | ||
config, | ||
dialogRef: ref as ModalGalleryRef, | ||
overlayRef: mockOverlayRef as unknown as OverlayRef | ||
}); | ||
|
||
expect(attachCalled).toBeTrue(); | ||
})); | ||
}); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
.../ks89/angular-modal-gallery/src/lib/components/modal-gallery/attach-to-overlay.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Injectable, Injector } from '@angular/core'; | ||
import { ModalGalleryRef } from './modal-gallery-ref'; | ||
import { ModalGalleryComponent } from './modal-gallery.component'; | ||
import { DIALOG_DATA } from './modal-gallery.tokens'; | ||
import { ComponentPortal } from '@angular/cdk/portal'; | ||
import { AttachToOverlayPayload, ModalGalleryService } from './modal-gallery.service'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class AttachToOverlayService { | ||
constructor(private injector: Injector, private modalGalleryService: ModalGalleryService) {} | ||
|
||
/** | ||
* To be called by a provider with the APP_INITIALIZER token. | ||
*/ | ||
public initialize(): void { | ||
this.modalGalleryService.triggerAttachToOverlay.subscribe(payload => this.attachToOverlay(payload)); | ||
} | ||
|
||
/** | ||
* Private method to attach ModalGalleryComponent to the overlay. | ||
* @param payload {@link AttachToOverlayPayload} with all necessary information | ||
* @private | ||
*/ | ||
private attachToOverlay(payload: AttachToOverlayPayload): void { | ||
const injector: Injector = Injector.create({ | ||
parent: this.injector, | ||
providers: [ | ||
{ provide: ModalGalleryRef, useValue: payload.dialogRef }, | ||
{ provide: DIALOG_DATA, useValue: payload.config } | ||
] | ||
}); | ||
|
||
const containerPortal = new ComponentPortal(ModalGalleryComponent, null, injector); | ||
payload.overlayRef.attach(containerPortal); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters