@joist/element
Version:
Intelligently apply styles to WebComponents
105 lines (77 loc) • 2.02 kB
text/typescript
import { assert } from "chai";
import { element } from "./element.js";
import { listen } from "./listen.js";
describe("@listen()", () => {
it("should add listener to an outer HTMLElement", (done) => {
({
tagName: "listener-1",
})
class MyElement extends HTMLElement {
("click")
onClick(e: Event) {
assert.equal(e.type, "click");
done();
}
}
const el = new MyElement();
document.body.append(el);
el.dispatchEvent(new Event("click"));
el.remove();
});
it("should add listener to the shadow root if available", (done) => {
({
tagName: "listener-2",
shadowDom: [],
})
class MyElement extends HTMLElement {
("click")
onClick(e: Event) {
assert.equal(e.type, "click");
done();
}
}
const el = new MyElement();
document.body.append(el);
el.shadowRoot?.dispatchEvent(new Event("click"));
el.remove();
});
it("should restrict argument to an event or an event subtype", (done) => {
class CustomEvent extends Event {
test = "Hello World";
constructor() {
super("customevent");
}
}
({
tagName: "listener-3",
})
class MyElement extends HTMLElement {
("customevent")
onClick(e: CustomEvent) {
assert.equal(e.type, "customevent");
done();
}
}
const el = new MyElement();
document.body.append(el);
el.dispatchEvent(new CustomEvent());
el.remove();
});
it("should respect a provided selector function", (done) => {
({
tagName: "listener-4",
shadowDom: [],
})
class MyElement extends HTMLElement {
("click", (host) => host)
onClick(e: Event) {
assert.equal(e.type, "click");
done();
}
}
const el = new MyElement();
document.body.append(el);
el.dispatchEvent(new Event("click"));
el.remove();
});
});