Skip to content

Commit

Permalink
fix(pat inject): Fix issue with submit buttons which are added later.
Browse files Browse the repository at this point in the history
Fix an issue with submit buttons which were added after initialization
of pat-inject which did not send their value (e.g. via a pat-inject call).

Ref: scrum-1670
  • Loading branch information
thet committed Nov 3, 2023
1 parent 2e7331b commit efc6dab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/pat/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ const inject = {
});
// setup event handlers
if ($el[0]?.tagName === "FORM") {
events.add_event_listener(
$el[0],
"click",
"pat-inject--form-submit-click",
(e) => {
if (
e.target.matches(
"[type=submit], button:not([type=button]), [type=image]"
)
) {
// make sure the submitting button is sent
// with the form
ajax.onClickSubmit(e);
}
}
);
events.add_event_listener(
$el[0],
"submit",
Expand All @@ -113,20 +129,6 @@ const inject = {
this.onTrigger(e);
}
);
for (const button of $el[0].querySelectorAll(
"[type=submit], button:not([type=button]), [type=image]"
)) {
events.add_event_listener(
button,
"click",
"pat-inject--form-submit-click",
(e) => {
// make sure the submitting button is sent
// with the form
ajax.onClickSubmit(e);
}
);
}
} else if ($el.is(".pat-subform")) {
log.debug("Initializing subform with injection");
} else {
Expand Down
23 changes: 23 additions & 0 deletions src/pat/inject/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,29 @@ describe("pat-inject", function () {
expect(pattern.onTrigger).toHaveBeenCalledTimes(1);
});
});

it("9.2.7 - Sends submit button form values even if submit button is added after initialization.", async function () {
document.body.innerHTML = `
<form class="pat-inject" action="test.cgi">
</form>
`;

const pat_ajax = (await import("../ajax/ajax.js")).default;
jest.spyOn(pat_ajax, "onClickSubmit");
jest.spyOn(pattern, "onTrigger");

const form = document.querySelector("form");

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

form.innerHTML = `<button type="submit"/>`;
const button = form.querySelector("button");
button.click();

expect(pat_ajax.onClickSubmit).toHaveBeenCalledTimes(1);
expect(pattern.onTrigger).toHaveBeenCalledTimes(1);
});
});
});

Expand Down

0 comments on commit efc6dab

Please sign in to comment.