${tag}>
`);
// Wait until it's done opening (handling features is async)
await nextFrame();
expect(beforeSpy).not.to.have.been.called;
await el._overlayCtrl.hide();
expect(beforeSpy).to.have.been.called;
expect(el.opened).to.be.false;
});
it('fires before-opened" event on show', async () => {
const beforeSpy = sinon.spy();
el = await fixture(html`
<${tag} @before-opened="${beforeSpy}">
content of the overlay
${tag}>
`);
expect(beforeSpy).not.to.have.been.called;
await el._overlayCtrl.show();
expect(beforeSpy).to.have.been.called;
expect(el.opened).to.be.true;
});
it('allows to call `preventDefault()` on "before-opened"/"before-closed" events', async () => {
function preventer(ev) {
ev.preventDefault();
}
el = await fixture(html`
<${tag} @before-opened="${preventer}" @before-closed="${preventer}">
content of the overlay
${tag}>
`);
el.querySelector('[slot="invoker"]').click();
await nextFrame();
expect(el.opened).to.be.false;
// Also, the opened state should be synced back to that of the OverlayController
el.opened = true;
expect(el.opened).to.be.true;
await nextFrame();
expect(el.opened).to.be.false;
});
it('hides content on "close-overlay" event within the content ', async () => {
function sendCloseEvent(e) {
e.target.dispatchEvent(new Event('close-overlay', { bubbles: true }));
}
const closeBtn = await fixture(html`
`);
el = await fixture(html`
<${tag} opened>
content of the overlay
${closeBtn}
${tag}>
`);
closeBtn.click();
expect(el.opened).to.be.false;
});
});
describe(`OverlayMixin${suffix} nested`, () => {
it('reconstructs the overlay when disconnected and reconnected to DOM (support for nested overlay nodes)', async () => {
const nestedEl = await fixture(html`
<${tag}>
${tag}>
`);
if (mainEl._overlayCtrl.placementMode === 'global') {
// Specifically checking the output in global root node, because the _contentOverlayNode still references
// the node that was removed in the teardown but hasn't been garbage collected due to reference to it still existing..
// Find the outlets that are not backdrop outlets
const outletsInGlobalRootNode = Array.from(overlays.globalRootNode.children).filter(
child =>
child.slot === '_overlay-shadow-outlet' &&
!child.classList.contains('global-overlays__backdrop'),
);
// Check the last one, which is the most nested one
const lastContentNodeInContainer =
outletsInGlobalRootNode[outletsInGlobalRootNode.length - 1];
expect(outletsInGlobalRootNode.length).to.equal(2);
// Check that it indeed has the intended content
expect(lastContentNodeInContainer.firstElementChild.innerText).to.equal(
'content of the nested overlay',
);
expect(lastContentNodeInContainer.firstElementChild.slot).to.equal('content');
} else {
const actualNestedOverlay = mainEl._overlayContentNode.firstElementChild;
const outletNode = Array.from(actualNestedOverlay.children).find(
child => child.slot === '_overlay-shadow-outlet',
);
const contentNode = Array.from(outletNode.children).find(child => child.slot === 'content');
expect(contentNode).to.not.be.undefined;
expect(contentNode.innerText).to.equal('content of the nested overlay');
}
});
});
}