Skip to content

Commit 5afcb23

Browse files
authored
Merge pull request #1080 from Patternslib/basepattern-await-init
Basepattern await init
2 parents 61892d4 + 34b14b4 commit 5afcb23

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/core/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const initBasePattern = function ($el, options, trigger) {
4646
};
4747

4848
const Base = async function ($el, options, trigger) {
49-
if (!$el) {
49+
if (($el?.jquery && $el.length === 0) || !$el) {
5050
log.warn("No element given to pattern.");
5151
return;
5252
}

src/core/base.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ describe("pat-base: The Base class for patterns", function () {
6969
expect(tmp.el).toBeFalsy();
7070
});
7171

72+
it("Does nothing when initialized with an empty jQuery object", function () {
73+
const Tmp = Base.extend({
74+
name: "example",
75+
init: () => {},
76+
});
77+
const tmp = new Tmp($());
78+
expect(tmp instanceof Tmp).toBeTruthy();
79+
expect(tmp.$el).toBeFalsy();
80+
expect(tmp.el).toBeFalsy();
81+
});
82+
7283
it("will automatically register a pattern in the registry when extended", function () {
7384
jest.spyOn(registry, "register");
7485
var NewPattern = Base.extend({

src/core/basepattern.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ class BasePattern {
7171
init() {
7272
// Extend this method in your pattern.
7373
}
74+
75+
/**
76+
* Listen to an event on the element only once.
77+
*
78+
* @param {string} event_name - Name of the event to listen to.
79+
* @param {function} callback - Callback to call when the event is thrown.
80+
*/
81+
one(event_name, event_callback) {
82+
this.el.addEventListener(`${event_name}.${this.name}.patterns`, event_callback, {
83+
once: true,
84+
});
85+
}
7486
}
7587

7688
export default BasePattern;

src/core/basepattern.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,21 @@ describe("Basepattern class tests", function () {
143143
// gh-copilot wrote this line.
144144
expect(el["pattern-example"]).toBeInstanceOf(Pat);
145145
});
146+
147+
it("6.1 - Registers a one-time event listener on the element.", async function () {
148+
const events = (await import("./events")).default;
149+
class Pat extends BasePattern {
150+
static name = "example";
151+
static trigger = ".example";
152+
}
153+
154+
const el = document.createElement("div");
155+
el.classList.add("example");
156+
157+
const pat = new Pat(el);
158+
await events.await_pattern_init(pat);
159+
160+
// If test reaches this expect statement, the init event catched.
161+
expect(true).toBe(true);
162+
});
146163
});

src/core/events.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Base from "./base";
2+
import { BasePattern } from "./basepattern";
23
import events from "./events";
34
import utils from "./utils";
45

@@ -77,6 +78,22 @@ describe("core.events tests", () => {
7778
// If test reaches this expect statement, all is fine.
7879
expect(true).toBe(true);
7980
});
81+
82+
it("Awaits a class based pattern to be initialized", async () => {
83+
class Pat extends BasePattern {
84+
static name = "tmp";
85+
static trigger = ".pat-tmp";
86+
init() {}
87+
}
88+
89+
const el = document.createElement("div");
90+
const instance = new Pat(el);
91+
92+
await events.await_pattern_init(instance);
93+
94+
// If test reaches this expect statement, all is fine.
95+
expect(true).toBe(true);
96+
});
8097
});
8198

8299
describe("2 - event factories", () => {

0 commit comments

Comments
 (0)