Skip to content

Commit

Permalink
fix(pat-inject): Fix problem with pat-autosubmit.
Browse files Browse the repository at this point in the history
Fix a problem since Patternslib 9.9.6 where pat-inject didn't work
together with pat-autosubmit.
  • Loading branch information
thet committed Oct 22, 2023
1 parent 9d20ea1 commit 648bf4a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/pat/auto-submit/auto-submit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "../../core/jquery-ext";
import $ from "jquery";
import Base from "../../core/base";
import events from "../../core/events";
import input_change_events from "../../lib/input-change-events";
import logging from "../../core/logging";
import Parser from "../../core/parser";
Expand Down Expand Up @@ -119,7 +120,7 @@ export default Base.extend({

onInputChange(e) {
e.stopPropagation();
this.$el.submit();
this.el.dispatchEvent(events.submit_event());
log.debug("triggered by " + e.type);
},
});
26 changes: 19 additions & 7 deletions src/pat/auto-submit/auto-submit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,19 @@ describe("pat-autosubmit", function () {
</form>
`;
const input = document.querySelector(".pat-autosubmit");
const instance = new Pattern(input);
const spy = jest.spyOn(instance.$el, "submit");
new Pattern(input);
let submit_input_dispatched = false;
let submit_form_dispatched = false;
input.addEventListener("submit", () => {
submit_input_dispatched = true;
});
document.querySelector("form").addEventListener("submit", () => {
submit_form_dispatched = true;
});
input.dispatchEvent(events.input_event());
await utils.timeout(1);
expect(spy).toHaveBeenCalled();
expect(submit_input_dispatched).toBe(true);
expect(submit_form_dispatched).toBe(true);
});

it("2.2 - when pat-clone'd input is changed", async function () {
Expand All @@ -104,16 +112,20 @@ describe("pat-autosubmit", function () {
const el_clone = document.querySelector(".pat-clone");
const button_clone = document.querySelector(".add-clone");

const instance = new Pattern(el);
new pattern_clone(el_clone);
let submit_dispatched = false;
el.addEventListener("submit", () => {
submit_dispatched = true;
});

const spy = jest.spyOn(instance.$el, "submit");
new Pattern(el);
new pattern_clone(el_clone);

button_clone.click();

document.querySelector("form input").dispatchEvent(events.input_event());

expect(spy).toHaveBeenCalled();
await utils.timeout(1);
expect(submit_dispatched).toBe(true);
});

it("2.3 - when pat-clone removes an element", function () {
Expand Down
11 changes: 8 additions & 3 deletions src/pat/auto-suggest/auto-suggest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,20 @@ describe("pat-autosuggest", function () {

const pattern_autosubmit = (await import("../auto-submit/auto-submit")).default; // prettier-ignore
const input = document.querySelector("input");
let submit_dispatched = false;
input.addEventListener("submit", () => {
submit_dispatched = true;
});

new pattern(input);
const instance_autosubmit = new pattern_autosubmit(input);
const spy = jest.spyOn(instance_autosubmit.$el, "submit");
new pattern_autosubmit(input);

await utils.timeout(1); // wait a tick for async to settle.

$(".select2-input").click();
$(document.querySelector(".select2-result")).mouseup();

expect(spy).toHaveBeenCalled();
expect(submit_dispatched).toBe(true);
});

it("4.2 - Works with pat-validate on an empty selection.", async function () {
Expand Down

0 comments on commit 648bf4a

Please sign in to comment.