|
| 1 | +/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 16 | +import {CustomModalComponent} from './custom_modal_component'; |
| 17 | +import {CommonModule} from '@angular/common'; |
| 18 | +import { |
| 19 | + Component, |
| 20 | + ElementRef, |
| 21 | + EventEmitter, |
| 22 | + Output, |
| 23 | + ViewChild, |
| 24 | +} from '@angular/core'; |
| 25 | + |
| 26 | +function waitFrame() { |
| 27 | + return new Promise((resolve) => window.requestAnimationFrame(resolve)); |
| 28 | +} |
| 29 | + |
| 30 | +@Component({ |
| 31 | + selector: 'testable-modal', |
| 32 | + template: `<custom-modal #modal (onOpen)="setOpen()" (onClose)="setClosed()"> |
| 33 | + <div>My great content</div> |
| 34 | + </custom-modal> `, |
| 35 | +}) |
| 36 | +class TestableComponent { |
| 37 | + @ViewChild('modal', {static: false}) |
| 38 | + modalComponent!: CustomModalComponent; |
| 39 | + |
| 40 | + @ViewChild('content', {static: false}) |
| 41 | + content!: ElementRef; |
| 42 | + |
| 43 | + isOpen = false; |
| 44 | + |
| 45 | + @Output() onOpen = new EventEmitter(); |
| 46 | + @Output() onClose = new EventEmitter(); |
| 47 | + |
| 48 | + setOpen() { |
| 49 | + this.isOpen = true; |
| 50 | + this.onOpen.emit(); |
| 51 | + } |
| 52 | + |
| 53 | + setClosed() { |
| 54 | + this.isOpen = false; |
| 55 | + this.onClose.emit(); |
| 56 | + } |
| 57 | + |
| 58 | + close() { |
| 59 | + this.modalComponent.close(); |
| 60 | + } |
| 61 | + |
| 62 | + getContentStyle() { |
| 63 | + return (this.modalComponent as any).content.nativeElement.style; |
| 64 | + } |
| 65 | + |
| 66 | + public openAtPosition(position: {x: number; y: number}) { |
| 67 | + this.modalComponent.openAtPosition(position); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +describe('custom modal', () => { |
| 72 | + beforeEach(async () => { |
| 73 | + await TestBed.configureTestingModule({ |
| 74 | + declarations: [TestableComponent, CustomModalComponent], |
| 75 | + imports: [CommonModule], |
| 76 | + }).compileComponents(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('waits a frame before emitting onOpen or onClose', async () => { |
| 80 | + const fixture = TestBed.createComponent(TestableComponent); |
| 81 | + fixture.detectChanges(); |
| 82 | + fixture.componentInstance.openAtPosition({x: 0, y: 0}); |
| 83 | + expect(fixture.componentInstance.isOpen).toBeFalse(); |
| 84 | + await waitFrame(); |
| 85 | + expect(fixture.componentInstance.isOpen).toBeTrue(); |
| 86 | + fixture.componentInstance.close(); |
| 87 | + fixture.detectChanges(); |
| 88 | + await waitFrame(); |
| 89 | + expect(fixture.componentInstance.isOpen).toBeFalse(); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('openAtPosition', () => { |
| 93 | + it('applies top and left offsets', () => { |
| 94 | + const fixture = TestBed.createComponent(TestableComponent); |
| 95 | + fixture.detectChanges(); |
| 96 | + fixture.componentInstance.openAtPosition({x: 20, y: 10}); |
| 97 | + expect(fixture.componentInstance.getContentStyle().top).toEqual('10px'); |
| 98 | + expect(fixture.componentInstance.getContentStyle().left).toEqual('20px'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('emits onOpen', async () => { |
| 102 | + const fixture = TestBed.createComponent(TestableComponent); |
| 103 | + const spy = spyOn(fixture.componentInstance.onOpen, 'emit'); |
| 104 | + fixture.detectChanges(); |
| 105 | + fixture.componentInstance.openAtPosition({x: 20, y: 10}); |
| 106 | + expect(spy).not.toHaveBeenCalled(); |
| 107 | + await waitFrame(); |
| 108 | + expect(spy).toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('closing behavior', () => { |
| 113 | + let fixture: ComponentFixture<TestableComponent>; |
| 114 | + beforeEach(async () => { |
| 115 | + fixture = TestBed.createComponent(TestableComponent); |
| 116 | + fixture.detectChanges(); |
| 117 | + fixture.componentInstance.openAtPosition({x: 0, y: 0}); |
| 118 | + await waitFrame(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('closes when escape key is pressed', async () => { |
| 122 | + expect(fixture.componentInstance.isOpen).toBeTrue(); |
| 123 | + const event = new KeyboardEvent('keydown', {key: 'escape'}); |
| 124 | + document.dispatchEvent(event); |
| 125 | + await waitFrame(); |
| 126 | + |
| 127 | + expect(fixture.componentInstance.isOpen).toBeFalse(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('closes when user clicks outside modal', async () => { |
| 131 | + expect(fixture.componentInstance.isOpen).toBeTrue(); |
| 132 | + document.body.click(); |
| 133 | + await waitFrame(); |
| 134 | + |
| 135 | + expect(fixture.componentInstance.isOpen).toBeFalse(); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments