|
| 1 | +import {TestBed, inject} from '@angular/core/testing'; |
| 2 | +import {dispatchKeyboardEvent} from '@angular/cdk/testing'; |
| 3 | +import {ESCAPE} from '@angular/cdk/keycodes'; |
| 4 | +import {Overlay} from '../overlay'; |
| 5 | +import {OverlayContainer} from '../overlay-container'; |
| 6 | +import {OverlayModule} from '../index'; |
| 7 | +import {OverlayKeyboardDispatcher} from './overlay-keyboard-dispatcher'; |
| 8 | + |
| 9 | +describe('OverlayKeyboardDispatcher', () => { |
| 10 | + let keyboardDispatcher: OverlayKeyboardDispatcher; |
| 11 | + let overlay: Overlay; |
| 12 | + let overlayContainerElement: HTMLElement; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + TestBed.configureTestingModule({ |
| 16 | + imports: [OverlayModule], |
| 17 | + providers: [ |
| 18 | + {provide: OverlayContainer, useFactory: () => { |
| 19 | + overlayContainerElement = document.createElement('div'); |
| 20 | + return {getContainerElement: () => overlayContainerElement}; |
| 21 | + }} |
| 22 | + ], |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(inject([OverlayKeyboardDispatcher, Overlay], |
| 27 | + (kbd: OverlayKeyboardDispatcher, o: Overlay) => { |
| 28 | + keyboardDispatcher = kbd; |
| 29 | + overlay = o; |
| 30 | + })); |
| 31 | + |
| 32 | + it('should track overlays in order as they are attached and detached', () => { |
| 33 | + const overlayOne = overlay.create(); |
| 34 | + const overlayTwo = overlay.create(); |
| 35 | + |
| 36 | + // Attach overlays |
| 37 | + keyboardDispatcher.add(overlayOne); |
| 38 | + keyboardDispatcher.add(overlayTwo); |
| 39 | + |
| 40 | + expect(keyboardDispatcher._attachedOverlays.length) |
| 41 | + .toBe(2, 'Expected both overlays to be tracked.'); |
| 42 | + expect(keyboardDispatcher._attachedOverlays[0]).toBe(overlayOne, 'Expected one to be first.'); |
| 43 | + expect(keyboardDispatcher._attachedOverlays[1]).toBe(overlayTwo, 'Expected two to be last.'); |
| 44 | + |
| 45 | + // Detach first one and re-attach it |
| 46 | + keyboardDispatcher.remove(overlayOne); |
| 47 | + keyboardDispatcher.add(overlayOne); |
| 48 | + |
| 49 | + expect(keyboardDispatcher._attachedOverlays[0]) |
| 50 | + .toBe(overlayTwo, 'Expected two to now be first.'); |
| 51 | + expect(keyboardDispatcher._attachedOverlays[1]) |
| 52 | + .toBe(overlayOne, 'Expected one to now be last.'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should dispatch body keyboard events to the most recently attached overlay', () => { |
| 56 | + const overlayOne = overlay.create(); |
| 57 | + const overlayTwo = overlay.create(); |
| 58 | + const overlayOneSpy = jasmine.createSpy('overlayOne keyboard event spy'); |
| 59 | + const overlayTwoSpy = jasmine.createSpy('overlayOne keyboard event spy'); |
| 60 | + |
| 61 | + overlayOne.keydownEvents().subscribe(overlayOneSpy); |
| 62 | + overlayTwo.keydownEvents().subscribe(overlayTwoSpy); |
| 63 | + |
| 64 | + // Attach overlays |
| 65 | + keyboardDispatcher.add(overlayOne); |
| 66 | + keyboardDispatcher.add(overlayTwo); |
| 67 | + |
| 68 | + dispatchKeyboardEvent(document.body, 'keydown', ESCAPE); |
| 69 | + |
| 70 | + // Most recent overlay should receive event |
| 71 | + expect(overlayOneSpy).not.toHaveBeenCalled(); |
| 72 | + expect(overlayTwoSpy).toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should dispatch targeted keyboard events to the overlay containing that target', () => { |
| 76 | + const overlayOne = overlay.create(); |
| 77 | + const overlayTwo = overlay.create(); |
| 78 | + const overlayOneSpy = jasmine.createSpy('overlayOne keyboard event spy'); |
| 79 | + const overlayTwoSpy = jasmine.createSpy('overlayOne keyboard event spy'); |
| 80 | + |
| 81 | + overlayOne.keydownEvents().subscribe(overlayOneSpy); |
| 82 | + overlayTwo.keydownEvents().subscribe(overlayTwoSpy); |
| 83 | + |
| 84 | + // Attach overlays |
| 85 | + keyboardDispatcher.add(overlayOne); |
| 86 | + keyboardDispatcher.add(overlayTwo); |
| 87 | + |
| 88 | + const overlayOnePane = overlayOne.overlayElement; |
| 89 | + |
| 90 | + dispatchKeyboardEvent(document.body, 'keydown', ESCAPE, overlayOnePane); |
| 91 | + |
| 92 | + // Targeted overlay should receive event |
| 93 | + expect(overlayOneSpy).toHaveBeenCalled(); |
| 94 | + expect(overlayTwoSpy).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | +}); |
0 commit comments