|
1 | 1 | import { expect } from '@vaadin/chai-plugins'; |
2 | 2 | import { aTimeout, fixtureSync, isIOS, nextFrame, oneEvent } from '@vaadin/testing-helpers'; |
3 | 3 | import sinon from 'sinon'; |
4 | | -import '../src/vaadin-overlay.js'; |
| 4 | +import { Overlay } from '../src/vaadin-overlay.js'; |
5 | 5 | import { createOverlay } from './helpers.js'; |
6 | 6 |
|
7 | 7 | describe('vaadin-overlay', () => { |
@@ -274,4 +274,86 @@ describe('vaadin-overlay', () => { |
274 | 274 | overlay._detectIosNavbar.restore(); |
275 | 275 | }); |
276 | 276 | }); |
| 277 | + |
| 278 | + describe('disconnected', () => { |
| 279 | + customElements.define( |
| 280 | + 'overlay-wrapper', |
| 281 | + class extends HTMLElement { |
| 282 | + constructor() { |
| 283 | + super(); |
| 284 | + |
| 285 | + this.attachShadow({ mode: 'open' }); |
| 286 | + |
| 287 | + const overlay = document.createElement('wrapped-overlay'); |
| 288 | + this.overlay = overlay; |
| 289 | + |
| 290 | + // Forward the slotted content from wrapper to overlay |
| 291 | + const slot = document.createElement('slot'); |
| 292 | + overlay.appendChild(slot); |
| 293 | + |
| 294 | + overlay.renderer = (root) => { |
| 295 | + root.innerHTML = '<input placeholder="Input">'; |
| 296 | + }; |
| 297 | + |
| 298 | + this.shadowRoot.append(overlay); |
| 299 | + } |
| 300 | + |
| 301 | + get opened() { |
| 302 | + return this.overlay.opened; |
| 303 | + } |
| 304 | + |
| 305 | + set opened(opened) { |
| 306 | + this.overlay.opened = opened; |
| 307 | + } |
| 308 | + |
| 309 | + connectedCallback() { |
| 310 | + const root = document.createElement('div'); |
| 311 | + this.overlay.__customRoot = root; |
| 312 | + this.append(root); |
| 313 | + } |
| 314 | + |
| 315 | + disconnectedCallback() { |
| 316 | + this.opened = false; |
| 317 | + } |
| 318 | + }, |
| 319 | + ); |
| 320 | + |
| 321 | + customElements.define( |
| 322 | + 'wrapped-overlay', |
| 323 | + class extends Overlay { |
| 324 | + get _contentRoot() { |
| 325 | + return this.__customRoot; |
| 326 | + } |
| 327 | + |
| 328 | + _attachOverlay() { |
| 329 | + this.setAttribute('popover', 'manual'); |
| 330 | + this.showPopover(); |
| 331 | + } |
| 332 | + |
| 333 | + _detachOverlay() { |
| 334 | + this.hidePopover(); |
| 335 | + } |
| 336 | + }, |
| 337 | + ); |
| 338 | + |
| 339 | + let owner, overlay; |
| 340 | + |
| 341 | + beforeEach(() => { |
| 342 | + owner = document.createElement('overlay-wrapper'); |
| 343 | + document.body.appendChild(owner); |
| 344 | + overlay = owner.shadowRoot.querySelector('wrapped-overlay'); |
| 345 | + }); |
| 346 | + |
| 347 | + it('should not call overlay opening when overlay is disconnected', async () => { |
| 348 | + overlay.opened = true; |
| 349 | + await oneEvent(overlay, 'vaadin-overlay-open'); |
| 350 | + |
| 351 | + const spy = sinon.spy(overlay, '_attachOverlay'); |
| 352 | + |
| 353 | + owner.remove(); |
| 354 | + owner.opened = true; |
| 355 | + |
| 356 | + expect(spy.called).to.be.false; |
| 357 | + }); |
| 358 | + }); |
277 | 359 | }); |
0 commit comments