import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { click, render, triggerKeyEvent } from "@ember/test-helpers";
import { exists, query } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars";
import { showPopover } from "discourse/lib/d-popover";
module("Integration | Component | d-popover", function (hooks) {
setupRenderingTest(hooks);
test("show/hide popover from lib", async function (assert) {
this.set("onButtonClick", (_, event) => {
showPopover(event, { content: "test", trigger: "click", duration: 0 });
});
await render(hbs`
`);
assert.notOk(document.querySelector("div[data-tippy-root]"));
await click(".btn");
assert.strictEqual(
document.querySelector("div[data-tippy-root]").innerText.trim(),
"test"
);
await click(".btn");
assert.notOk(document.querySelector("div[data-tippy-root]"));
});
test("show/hide popover from component", async function (assert) {
await render(hbs`
`);
assert.notOk(exists(".d-popover.is-expanded"));
assert.notOk(exists(".test"));
await click(".trigger");
assert.ok(exists(".d-popover.is-expanded"));
assert.strictEqual(query(".test").innerText.trim(), "foo");
await click(".closer");
assert.notOk(exists(".d-popover.is-expanded"));
});
test("using options with component", async function (assert) {
await render(hbs`
`);
await click(".btn");
assert.strictEqual(query(".tippy-content").innerText.trim(), "bar");
});
test("d-popover component accepts a block", async function (assert) {
await render(hbs`
`);
assert.ok(exists(".d-icon-chevron-down"));
await click(".btn");
assert.ok(exists(".d-icon-chevron-up"));
});
test("d-popover component accepts a class property", async function (assert) {
await render(hbs``);
assert.ok(exists(".d-popover.foo"));
});
test("d-popover component closes on escape key", async function (assert) {
await render(hbs`
`);
await click(".btn");
assert.ok(exists(".d-popover.is-expanded"));
await triggerKeyEvent(document, "keydown", "Escape");
assert.notOk(exists(".d-popover.is-expanded"));
});
});