|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { |
| 3 | + click, |
| 4 | + enterKeyDown, |
| 5 | + escKeyDown, |
| 6 | + fixtureSync, |
| 7 | + mousedown, |
| 8 | + mouseup, |
| 9 | + nextRender, |
| 10 | + oneEvent, |
| 11 | +} from '@vaadin/testing-helpers'; |
| 12 | +import sinon from 'sinon'; |
| 13 | +import '../vaadin-overlay.js'; |
| 14 | +import { createOverlay } from './helpers.js'; |
| 15 | + |
| 16 | +describe('interactions', () => { |
| 17 | + describe('Esc', () => { |
| 18 | + let overlay; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + overlay = createOverlay('overlay content'); |
| 22 | + overlay.opened = true; |
| 23 | + await oneEvent(overlay, 'vaadin-overlay-open'); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(async () => { |
| 27 | + overlay.opened = false; |
| 28 | + await nextRender(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should close on Esc', () => { |
| 32 | + escKeyDown(document.body); |
| 33 | + |
| 34 | + expect(overlay.opened).to.be.false; |
| 35 | + }); |
| 36 | + |
| 37 | + it('should fire the vaadin-overlay-escape-press event on Esc', () => { |
| 38 | + const spy = sinon.spy(); |
| 39 | + overlay.addEventListener('vaadin-overlay-escape-press', spy); |
| 40 | + |
| 41 | + escKeyDown(document.body); |
| 42 | + |
| 43 | + expect(spy.calledOnce).to.be.true; |
| 44 | + }); |
| 45 | + |
| 46 | + it('should not fire the vaadin-overlay-escape-press event on other key press', () => { |
| 47 | + const spy = sinon.spy(); |
| 48 | + overlay.addEventListener('vaadin-overlay-escape-press', spy); |
| 49 | + |
| 50 | + enterKeyDown(document.body); |
| 51 | + |
| 52 | + expect(spy.called).to.be.false; |
| 53 | + }); |
| 54 | + |
| 55 | + it('should not close on Esc if the event was prevented', () => { |
| 56 | + overlay.addEventListener('vaadin-overlay-escape-press', (e) => e.preventDefault()); |
| 57 | + |
| 58 | + escKeyDown(document.body); |
| 59 | + |
| 60 | + expect(overlay.opened).to.be.true; |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('click', () => { |
| 65 | + let parent, overlay, overlayPart, backdrop; |
| 66 | + |
| 67 | + beforeEach(async () => { |
| 68 | + parent = document.createElement('div'); |
| 69 | + overlay = fixtureSync('<vaadin-overlay></vaadin-overlay>', parent); |
| 70 | + overlay.renderer = (root) => { |
| 71 | + root.textContent = 'overlay content'; |
| 72 | + }; |
| 73 | + await nextRender(); |
| 74 | + overlayPart = overlay.$.overlay; |
| 75 | + backdrop = overlay.$.backdrop; |
| 76 | + overlay.opened = true; |
| 77 | + await oneEvent(overlay, 'vaadin-overlay-open'); |
| 78 | + }); |
| 79 | + |
| 80 | + afterEach(async () => { |
| 81 | + overlay.opened = false; |
| 82 | + await nextRender(); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('close on click', () => { |
| 86 | + it('should not close on inside click', () => { |
| 87 | + click(overlayPart); |
| 88 | + |
| 89 | + expect(overlay.opened).to.be.true; |
| 90 | + }); |
| 91 | + |
| 92 | + it('should close on outside click', () => { |
| 93 | + click(parent); |
| 94 | + |
| 95 | + expect(overlay.opened).to.be.false; |
| 96 | + }); |
| 97 | + |
| 98 | + it('should close on backdrop click', () => { |
| 99 | + overlay.withBackdrop = true; |
| 100 | + |
| 101 | + click(backdrop); |
| 102 | + |
| 103 | + expect(overlay.opened).to.be.false; |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('vaadin-overlay-outside-click event', () => { |
| 108 | + it('should fire the event on outside click', () => { |
| 109 | + const spy = sinon.spy(); |
| 110 | + overlay.addEventListener('vaadin-overlay-outside-click', spy); |
| 111 | + |
| 112 | + click(parent); |
| 113 | + |
| 114 | + expect(spy.calledOnce).to.be.true; |
| 115 | + }); |
| 116 | + |
| 117 | + it('should fire the event on backdrop click', () => { |
| 118 | + const spy = sinon.spy(); |
| 119 | + overlay.addEventListener('vaadin-overlay-outside-click', spy); |
| 120 | + |
| 121 | + overlay.withBackdrop = true; |
| 122 | + click(backdrop); |
| 123 | + |
| 124 | + expect(spy.calledOnce).to.be.true; |
| 125 | + }); |
| 126 | + |
| 127 | + it('should not fire the event on inside click', () => { |
| 128 | + const spy = sinon.spy(); |
| 129 | + overlay.addEventListener('vaadin-overlay-outside-click', spy); |
| 130 | + |
| 131 | + click(overlayPart); |
| 132 | + |
| 133 | + expect(spy.called).to.be.false; |
| 134 | + }); |
| 135 | + |
| 136 | + it('should not close on outside click if the event was prevented', () => { |
| 137 | + overlay.addEventListener('vaadin-overlay-outside-click', (e) => e.preventDefault()); |
| 138 | + |
| 139 | + click(parent); |
| 140 | + |
| 141 | + expect(overlay.opened).to.be.true; |
| 142 | + }); |
| 143 | + |
| 144 | + it('should not close on backdrop click if the event was prevented', () => { |
| 145 | + overlay.addEventListener('vaadin-overlay-outside-click', (e) => e.preventDefault()); |
| 146 | + |
| 147 | + click(backdrop); |
| 148 | + |
| 149 | + expect(overlay.opened).to.be.true; |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('vaadin-overlay-close event', () => { |
| 154 | + it('should prevent closing the overlay if the event was prevented', (done) => { |
| 155 | + overlay.addEventListener('vaadin-overlay-close', (e) => { |
| 156 | + e.preventDefault(); |
| 157 | + |
| 158 | + setTimeout(() => { |
| 159 | + expect(overlay.opened).to.be.true; |
| 160 | + done(); |
| 161 | + }, 1); |
| 162 | + }); |
| 163 | + click(parent); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('vaadin-overlay-closing event', () => { |
| 168 | + it('should fire the event when the overlay is closing', async () => { |
| 169 | + const spy = sinon.spy(); |
| 170 | + overlay.addEventListener('vaadin-overlay-closing', spy); |
| 171 | + |
| 172 | + click(parent); |
| 173 | + await nextRender(); |
| 174 | + |
| 175 | + expect(spy.calledOnce).to.be.true; |
| 176 | + }); |
| 177 | + |
| 178 | + it('should not fire the event when preventing vaadin-overlay-close', async () => { |
| 179 | + const spy = sinon.spy(); |
| 180 | + overlay.addEventListener('vaadin-overlay-closing', spy); |
| 181 | + overlay.addEventListener('vaadin-overlay-close', (e) => e.preventDefault()); |
| 182 | + click(parent); |
| 183 | + await nextRender(); |
| 184 | + expect(spy.called).to.be.false; |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('vaadin-overlay-closed event', () => { |
| 189 | + it('should fire the event after the overlay has closed', async () => { |
| 190 | + const closingSpy = sinon.spy(); |
| 191 | + overlay.addEventListener('vaadin-overlay-closing', closingSpy); |
| 192 | + |
| 193 | + const closedSpy = sinon.spy(); |
| 194 | + overlay.addEventListener('vaadin-overlay-closed', closedSpy); |
| 195 | + |
| 196 | + click(parent); |
| 197 | + await nextRender(); |
| 198 | + |
| 199 | + expect(closedSpy.calledOnce).to.be.true; |
| 200 | + expect(closedSpy.calledAfter(closingSpy)).to.be.true; |
| 201 | + }); |
| 202 | + |
| 203 | + it('should not fire the event when preventing vaadin-overlay-close', async () => { |
| 204 | + const spy = sinon.spy(); |
| 205 | + overlay.addEventListener('vaadin-overlay-closed', spy); |
| 206 | + overlay.addEventListener('vaadin-overlay-close', (e) => e.preventDefault()); |
| 207 | + |
| 208 | + click(parent); |
| 209 | + await nextRender(); |
| 210 | + |
| 211 | + expect(spy.called).to.be.false; |
| 212 | + }); |
| 213 | + }); |
| 214 | + |
| 215 | + describe('moving mouse pointer during click', () => { |
| 216 | + it('should close if both mousedown and mouseup outside', () => { |
| 217 | + mousedown(parent); |
| 218 | + mouseup(parent); |
| 219 | + click(parent); |
| 220 | + |
| 221 | + expect(overlay.opened).to.be.false; |
| 222 | + }); |
| 223 | + |
| 224 | + it('should not close if mousedown outside and mouseup inside', () => { |
| 225 | + mousedown(parent); |
| 226 | + mouseup(overlayPart); |
| 227 | + click(overlayPart); |
| 228 | + |
| 229 | + expect(overlay.opened).to.be.true; |
| 230 | + }); |
| 231 | + |
| 232 | + it('should not close if mousedown inside and mouseup outside', () => { |
| 233 | + mousedown(overlayPart); |
| 234 | + mouseup(parent); |
| 235 | + click(parent); |
| 236 | + |
| 237 | + expect(overlay.opened).to.be.true; |
| 238 | + }); |
| 239 | + |
| 240 | + it('should not close if both mousedown mouseup inside', () => { |
| 241 | + mousedown(overlayPart); |
| 242 | + mouseup(overlayPart); |
| 243 | + click(overlayPart); |
| 244 | + |
| 245 | + expect(overlay.opened).to.be.true; |
| 246 | + }); |
| 247 | + }); |
| 248 | + |
| 249 | + describe('mouseup after contextmenu event opening', () => { |
| 250 | + // On some platforms, contextmenu event is dispatched right on |
| 251 | + // mousedown, before mouseup. If the contextmenu event opens |
| 252 | + // the overlay, then one contextmenu-related mouseup event also occurs. |
| 253 | + |
| 254 | + it('should close on outside click after mouseup inside', () => { |
| 255 | + // Assume the overlay was opened on contextmenu event |
| 256 | + mouseup(overlayPart); |
| 257 | + |
| 258 | + mousedown(parent); |
| 259 | + mouseup(parent); |
| 260 | + click(parent); |
| 261 | + |
| 262 | + expect(overlay.opened).to.be.false; |
| 263 | + }); |
| 264 | + |
| 265 | + it('should close on outside click after mouseup outside', () => { |
| 266 | + // Assume the overlay was opened on contextmenu event, |
| 267 | + // but this time the cursor was moved outside before mouseup |
| 268 | + mouseup(parent); |
| 269 | + |
| 270 | + mousedown(parent); |
| 271 | + mouseup(parent); |
| 272 | + click(parent); |
| 273 | + |
| 274 | + expect(overlay.opened).to.be.false; |
| 275 | + }); |
| 276 | + |
| 277 | + it('should not close on inside click after mouseup inside', () => { |
| 278 | + mouseup(overlayPart); |
| 279 | + |
| 280 | + mousedown(overlayPart); |
| 281 | + mouseup(overlayPart); |
| 282 | + click(overlayPart); |
| 283 | + |
| 284 | + expect(overlay.opened).to.be.true; |
| 285 | + }); |
| 286 | + |
| 287 | + it('should not close on outside click after mouseup outside', () => { |
| 288 | + mouseup(parent); |
| 289 | + |
| 290 | + mousedown(overlayPart); |
| 291 | + mouseup(overlayPart); |
| 292 | + click(overlayPart); |
| 293 | + |
| 294 | + expect(overlay.opened).to.be.true; |
| 295 | + }); |
| 296 | + }); |
| 297 | + }); |
| 298 | +}); |
0 commit comments