Skip to content
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

Fix submit working with pat-inject #1185

Merged
merged 5 commits into from
Oct 23, 2023
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
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