Skip to content

Commit

Permalink
Merge pull request #1185 from Patternslib/fix-submit
Browse files Browse the repository at this point in the history
Fix submit working with pat-inject
  • Loading branch information
thet authored Oct 23, 2023
2 parents a6314d5 + 79436cb commit 3dfc452
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
17 changes: 9 additions & 8 deletions src/pat/ajax/ajax.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import registry from "../../core/registry";
import pattern from "./ajax";
import $ from "jquery";
import events from "../../core/events";
import pattern from "./ajax";
import registry from "../../core/registry";
import { jest } from "@jest/globals";

var $lab;
Expand Down Expand Up @@ -47,13 +48,13 @@ describe("pat-ajax", function () {
});

it("triggers ajax request on submit", function () {
$form.submit();
$form[0].dispatchEvent(events.submit_event());
expect(spy_ajax).toHaveBeenCalled();
});

it("honors method='post'", function () {
$form.attr("method", "post");
$form.submit();
$form[0].dispatchEvent(events.submit_event());
var ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
expect(ajaxargs.url).toEqual("action.html");
expect(ajaxargs.method).toEqual("POST");
Expand All @@ -73,7 +74,7 @@ describe("pat-ajax", function () {
});

it("does not include submit buttons if not clicked", function () {
$form.submit();
$form[0].dispatchEvent(events.submit_event());
var ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
expect(ajaxargs.url).toEqual("action.html");
expect(ajaxargs.data).toEqual("input1=value1");
Expand All @@ -99,7 +100,7 @@ describe("pat-ajax", function () {
document.body.innerHTML = `<form class="pat-ajax" action="somewhere.html"/>`;
registry.scan(document.body);
jest.spyOn($, "ajax");
$(".pat-ajax").submit();
$(".pat-ajax")[0].dispatchEvent(events.submit_event());
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
expect(ajaxargs.url).toEqual("somewhere.html");
});
Expand Down Expand Up @@ -127,7 +128,7 @@ describe("pat-ajax", function () {
`;
registry.scan(document.body);
jest.spyOn($, "ajax");
$(".pat-ajax").submit();
$(".pat-ajax")[0].dispatchEvent(events.submit_event());
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
expect(ajaxargs.url).toEqual("else.html");
});
Expand Down Expand Up @@ -214,7 +215,7 @@ describe("pat-ajax", function () {
data-pat-ajax="browser-cache: cache"
/>`;
registry.scan(document.body);
$(".pat-ajax").submit(); // need jquery submit here
$(".pat-ajax")[0].dispatchEvent(events.submit_event());
const ajaxargs = spy_ajax.mock.calls[spy_ajax.mock.calls.length - 1][0];
expect(ajaxargs.cache).toBe(false);
spy_ajax.mockRestore();
Expand Down
2 changes: 1 addition & 1 deletion src/pat/inject/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ describe("pat-inject", function () {
registry.scan(document.body);
await utils.timeout(1);

$(".pat-inject").submit(); // need jquery submit here
document.querySelector(".pat-inject").dispatchEvent(events.submit_event());
const ajaxargs = spy_ajax.mock.calls[spy_ajax.mock.calls.length - 1][0];
expect(ajaxargs.cache).toBe(false);
spy_ajax.mockRestore();
Expand Down
2 changes: 1 addition & 1 deletion src/pat/push/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default Base.extend({
if (this.options.mode === "desktop-notification") {
this.desktop_notification();
} else if (this.el.tagName === "FORM") {
this.el.submit();
this.el.dispatchEvent(events.submit_event());
} else {
this.perform_inject();
}
Expand Down
2 changes: 1 addition & 1 deletion src/pat/sortable/sortable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe("pat-sortable", function () {
$("#item3").prependTo($("ol")); // Simulate dragging it to the top.
const submitCallback = jest.fn();
submitCallback.mockReturnValue(false);
$(form).submit(submitCallback);
form.addEventListener("submit", submitCallback);
document
.querySelector("#item3 a.sortable-handle")
.dispatchEvent(events.dragend_event());
Expand Down
10 changes: 8 additions & 2 deletions src/pat/subform/subform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import $ from "jquery";
import ajax from "../ajax/ajax";
import Base from "../../core/base";
import events from "../../core/events";
import inject from "../inject/inject";
import logging from "../../core/logging";

Expand All @@ -16,7 +17,12 @@ export default Base.extend({
trigger: ".pat-subform",

init($el) {
$el.submit(this.submit.bind(this));
events.add_event_listener(
$el[0],
"submit",
"pat-subform--submit",
this.submit.bind(this)
);
$el.find("input").on("keyup keypress keydown", this.keyboard_handler.bind(this));
$el.find("button[type=submit]").on("click", this.submitClicked.bind(this));
},
Expand Down Expand Up @@ -76,7 +82,7 @@ export default Base.extend({
if (!$subform.is(".pat-autosubmit")) {
return;
}
return $subform.submit();
$subform[0].dispatchEvent(events.submit_event());
},

submitClicked(ev) {
Expand Down

0 comments on commit 3dfc452

Please sign in to comment.