Skip to content

Commit e544947

Browse files
committed
feat(core basepattern): Provide emit_update helper method on pattern instances to easily emit a pat-update event and already fill the most important properties. Expects an optional action parameter and an optional options object.
1 parent 3e5d101 commit e544947

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/core/basepattern.js

+9
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ class BasePattern {
104104
// Extend this method in your pattern.
105105
}
106106

107+
emit_update(action = undefined, options = {}) {
108+
options = Object.assign({
109+
pattern: this.name,
110+
dom: this.el,
111+
action: action,
112+
}, options);
113+
this.el.dispatchEvent(events.update_event(options));
114+
}
115+
107116
/**
108117
* Listen to an event on the element only once.
109118
*

src/core/basepattern.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,29 @@ describe("Basepattern class tests", function () {
364364
// If test reaches this expect statement, the init event catched.
365365
expect(true).toBe(true);
366366
});
367+
368+
it("7 - Emit update event.", async function () {
369+
const events = (await import("./events")).default;
370+
class Pat extends BasePattern {
371+
static name = "example";
372+
static trigger = ".example";
373+
}
374+
375+
const el = document.createElement("div");
376+
el.classList.add("example");
377+
378+
const pat = new Pat(el);
379+
await utils.timeout(1);
380+
381+
let event;
382+
el.addEventListener("pat-update", (e) => {
383+
event = e;
384+
});
385+
386+
pat.emit_update("test");
387+
388+
expect(event.detail.pattern).toBe("example");
389+
expect(event.detail.dom).toBe(el);
390+
expect(event.detail.action).toBe("test");
391+
});
367392
});

0 commit comments

Comments
 (0)