|
| 1 | +import {TestBed, inject} from '@angular/core/testing'; |
| 2 | +import {Component, NgModule} from '@angular/core'; |
| 3 | +import {OverlayModule, OverlayContainer, Overlay} from '../index'; |
| 4 | +import {OverlayOutsideClickDispatcher} from './overlay-outside-click-dispatcher'; |
| 5 | +import {ComponentPortal} from '@angular/cdk/portal'; |
| 6 | + |
| 7 | + |
| 8 | +describe('OverlayOutsideClickDispatcher', () => { |
| 9 | + let outsideClickDispatcher: OverlayOutsideClickDispatcher; |
| 10 | + let overlay: Overlay; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + TestBed.configureTestingModule({ |
| 14 | + imports: [OverlayModule, TestComponentModule], |
| 15 | + }); |
| 16 | + |
| 17 | + inject([OverlayOutsideClickDispatcher, Overlay], |
| 18 | + (ocd: OverlayOutsideClickDispatcher, o: Overlay) => { |
| 19 | + outsideClickDispatcher = ocd; |
| 20 | + overlay = o; |
| 21 | + })(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(inject([OverlayContainer], (overlayContainer: OverlayContainer) => { |
| 25 | + overlayContainer.ngOnDestroy(); |
| 26 | + })); |
| 27 | + |
| 28 | + it('should track overlays in order as they are attached and detached', () => { |
| 29 | + const overlayOne = overlay.create(); |
| 30 | + const overlayTwo = overlay.create(); |
| 31 | + |
| 32 | + outsideClickDispatcher.add(overlayOne); |
| 33 | + outsideClickDispatcher.add(overlayTwo); |
| 34 | + |
| 35 | + expect(outsideClickDispatcher._attachedOverlays.length) |
| 36 | + .toBe(2, 'Expected both overlays to be tracked.'); |
| 37 | + expect(outsideClickDispatcher._attachedOverlays[0]) |
| 38 | + .toBe(overlayOne, 'Expected one to be first.'); |
| 39 | + expect(outsideClickDispatcher._attachedOverlays[1]) |
| 40 | + .toBe(overlayTwo, 'Expected two to be last.'); |
| 41 | + |
| 42 | + outsideClickDispatcher.remove(overlayOne); |
| 43 | + outsideClickDispatcher.add(overlayOne); |
| 44 | + |
| 45 | + expect(outsideClickDispatcher._attachedOverlays[0]) |
| 46 | + .toBe(overlayTwo, 'Expected two to now be first.'); |
| 47 | + expect(outsideClickDispatcher._attachedOverlays[1]) |
| 48 | + .toBe(overlayOne, 'Expected one to now be last.'); |
| 49 | + |
| 50 | + overlayOne.dispose(); |
| 51 | + overlayTwo.dispose(); |
| 52 | + }); |
| 53 | + |
| 54 | + it( |
| 55 | + 'should dispatch mouse click events to the attached overlays', |
| 56 | + () => { |
| 57 | + const overlayOne = overlay.create(); |
| 58 | + const overlayTwo = overlay.create(); |
| 59 | + const overlayOneSpy = jasmine.createSpy('overlayOne mouse click event spy'); |
| 60 | + const overlayTwoSpy = jasmine.createSpy('overlayTwo mouse click event spy'); |
| 61 | + |
| 62 | + overlayOne.outsidePointerEvents().subscribe(overlayOneSpy); |
| 63 | + overlayTwo.outsidePointerEvents().subscribe(overlayTwoSpy); |
| 64 | + |
| 65 | + outsideClickDispatcher.add(overlayOne); |
| 66 | + outsideClickDispatcher.add(overlayTwo); |
| 67 | + |
| 68 | + const button = document.createElement('button'); |
| 69 | + document.body.appendChild(button); |
| 70 | + button.click(); |
| 71 | + |
| 72 | + expect(overlayOneSpy).toHaveBeenCalled(); |
| 73 | + expect(overlayTwoSpy).toHaveBeenCalled(); |
| 74 | + |
| 75 | + button.parentNode!.removeChild(button); |
| 76 | + overlayOne.dispose(); |
| 77 | + overlayTwo.dispose(); |
| 78 | + }); |
| 79 | + |
| 80 | + it( |
| 81 | + 'should dispatch mouse click events to the attached overlays even when propagation is stopped', |
| 82 | + () => { |
| 83 | + const overlayRef = overlay.create(); |
| 84 | + const spy = jasmine.createSpy('overlay mouse click event spy'); |
| 85 | + overlayRef.outsidePointerEvents().subscribe(spy); |
| 86 | + |
| 87 | + outsideClickDispatcher.add(overlayRef); |
| 88 | + |
| 89 | + const button = document.createElement('button'); |
| 90 | + document.body.appendChild(button); |
| 91 | + button.addEventListener('click', event => event.stopPropagation()); |
| 92 | + button.click(); |
| 93 | + |
| 94 | + expect(spy).toHaveBeenCalled(); |
| 95 | + |
| 96 | + button.parentNode!.removeChild(button); |
| 97 | + overlayRef.dispose(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should dispose of the global click event handler correctly', () => { |
| 101 | + const overlayRef = overlay.create(); |
| 102 | + const body = document.body; |
| 103 | + |
| 104 | + spyOn(body, 'addEventListener'); |
| 105 | + spyOn(body, 'removeEventListener'); |
| 106 | + |
| 107 | + outsideClickDispatcher.add(overlayRef); |
| 108 | + expect(body.addEventListener).toHaveBeenCalledWith('click', jasmine.any(Function), true); |
| 109 | + |
| 110 | + overlayRef.dispose(); |
| 111 | + expect(body.removeEventListener).toHaveBeenCalledWith('click', jasmine.any(Function), true); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should not add the same overlay to the stack multiple times', () => { |
| 115 | + const overlayOne = overlay.create(); |
| 116 | + const overlayTwo = overlay.create(); |
| 117 | + |
| 118 | + outsideClickDispatcher.add(overlayOne); |
| 119 | + outsideClickDispatcher.add(overlayTwo); |
| 120 | + outsideClickDispatcher.add(overlayOne); |
| 121 | + |
| 122 | + expect(outsideClickDispatcher._attachedOverlays).toEqual([overlayTwo, overlayOne]); |
| 123 | + |
| 124 | + overlayOne.dispose(); |
| 125 | + overlayTwo.dispose(); |
| 126 | + }); |
| 127 | + |
| 128 | + it(`should not dispatch click event when click on element |
| 129 | + included in excludeFromOutsideClick array`, () => { |
| 130 | + const overlayRef = overlay.create(); |
| 131 | + const spy = jasmine.createSpy('overlay mouse click event spy'); |
| 132 | + overlayRef.outsidePointerEvents().subscribe(spy); |
| 133 | + |
| 134 | + const overlayConfig = overlayRef.getConfig(); |
| 135 | + expect(overlayConfig.excludeFromOutsideClick).toBeDefined(); |
| 136 | + expect(overlayConfig.excludeFromOutsideClick!.length).toBe(0); |
| 137 | + |
| 138 | + overlayRef.attach(new ComponentPortal(TestComponent)); |
| 139 | + |
| 140 | + const buttonShouldNotDetach = document.createElement('button'); |
| 141 | + document.body.appendChild(buttonShouldNotDetach); |
| 142 | + overlayConfig.excludeFromOutsideClick!.push(buttonShouldNotDetach); |
| 143 | + buttonShouldNotDetach.click(); |
| 144 | + |
| 145 | + expect(spy).not.toHaveBeenCalled(); |
| 146 | + |
| 147 | + buttonShouldNotDetach.parentNode!.removeChild(buttonShouldNotDetach); |
| 148 | + overlayRef.dispose(); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | + |
| 153 | +@Component({ |
| 154 | + template: 'Hello' |
| 155 | +}) |
| 156 | +class TestComponent { } |
| 157 | + |
| 158 | + |
| 159 | +// Create a real (non-test) NgModule as a workaround for |
| 160 | +// https://github.com/angular/angular/issues/10760 |
| 161 | +@NgModule({ |
| 162 | + exports: [TestComponent], |
| 163 | + declarations: [TestComponent], |
| 164 | + entryComponents: [TestComponent], |
| 165 | +}) |
| 166 | +class TestComponentModule { } |
0 commit comments