Skip to content

Basepattern await init #1080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const initBasePattern = function ($el, options, trigger) {
};

const Base = async function ($el, options, trigger) {
if (!$el) {
if (($el?.jquery && $el.length === 0) || !$el) {
log.warn("No element given to pattern.");
return;
}
Expand Down
11 changes: 11 additions & 0 deletions src/core/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ describe("pat-base: The Base class for patterns", function () {
expect(tmp.el).toBeFalsy();
});

it("Does nothing when initialized with an empty jQuery object", function () {
const Tmp = Base.extend({
name: "example",
init: () => {},
});
const tmp = new Tmp($());
expect(tmp instanceof Tmp).toBeTruthy();
expect(tmp.$el).toBeFalsy();
expect(tmp.el).toBeFalsy();
});

it("will automatically register a pattern in the registry when extended", function () {
jest.spyOn(registry, "register");
var NewPattern = Base.extend({
Expand Down
12 changes: 12 additions & 0 deletions src/core/basepattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ class BasePattern {
init() {
// Extend this method in your pattern.
}

/**
* Listen to an event on the element only once.
*
* @param {string} event_name - Name of the event to listen to.
* @param {function} callback - Callback to call when the event is thrown.
*/
one(event_name, event_callback) {
this.el.addEventListener(`${event_name}.${this.name}.patterns`, event_callback, {
once: true,
});
}
}

export default BasePattern;
Expand Down
17 changes: 17 additions & 0 deletions src/core/basepattern.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,21 @@ describe("Basepattern class tests", function () {
// gh-copilot wrote this line.
expect(el["pattern-example"]).toBeInstanceOf(Pat);
});

it("6.1 - Registers a one-time event listener on the element.", async function () {
const events = (await import("./events")).default;
class Pat extends BasePattern {
static name = "example";
static trigger = ".example";
}

const el = document.createElement("div");
el.classList.add("example");

const pat = new Pat(el);
await events.await_pattern_init(pat);

// If test reaches this expect statement, the init event catched.
expect(true).toBe(true);
});
});
17 changes: 17 additions & 0 deletions src/core/events.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Base from "./base";
import { BasePattern } from "./basepattern";
import events from "./events";
import utils from "./utils";

Expand Down Expand Up @@ -77,6 +78,22 @@ describe("core.events tests", () => {
// If test reaches this expect statement, all is fine.
expect(true).toBe(true);
});

it("Awaits a class based pattern to be initialized", async () => {
class Pat extends BasePattern {
static name = "tmp";
static trigger = ".pat-tmp";
init() {}
}

const el = document.createElement("div");
const instance = new Pat(el);

await events.await_pattern_init(instance);

// If test reaches this expect statement, all is fine.
expect(true).toBe(true);
});
});

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