Fix bug when submitting form triggered a warning and potentially left submit button disable

jquery-ujs automatically disable submit button when submitting the form.
If one choose cancel on the leaving page warning, then the submit buttons
end up in a disable state, with no way to re enable them. This fix
prevent the warning from being triggered when submitting the form, so
we can't end up in the scenario described.
This commit is contained in:
Gaetan Craig-Riou
2023-02-08 16:28:41 +11:00
parent 5cfedddba4
commit fd278e0086
3 changed files with 56 additions and 9 deletions

View File

@@ -15,7 +15,8 @@ describe("UnsavedChangesController", () => {
document.body.innerHTML = `
<form
id="test-form"
data-controller="unsaved-changes" data-action="beforeunload@window->unsaved-changes#leavingPage turbolinks:before-visit@window->unsaved-changes#leavingPage"
data-controller="unsaved-changes"
data-action="unsaved-changes#submit beforeunload@window->unsaved-changes#leavingPage turbolinks:before-visit@window->unsaved-changes#leavingPage"
data-unsaved-changes-changed="false"
>
<input id="test-checkbox" type="checkbox" data-action="change->unsaved-changes#formIsChanged"/>
@@ -29,8 +30,9 @@ describe("UnsavedChangesController", () => {
beforeEach(() => {
document.body.innerHTML = `
<form
id="test-form" data-controller="unsaved-changes"
data-action="beforeunload@window->unsaved-changes#leavingPage turbolinks:before-visit@window->unsaved-changes#leavingPage"
id="test-form"
data-controller="unsaved-changes"
data-action="unsaved-changes#submit beforeunload@window->unsaved-changes#leavingPage turbolinks:before-visit@window->unsaved-changes#leavingPage"
data-unsaved-changes-changed="false"
data-unsaved-changes-disable-submit-button="true"
>
@@ -53,7 +55,7 @@ describe("UnsavedChangesController", () => {
<form
id="test-form"
data-controller="unsaved-changes"
data-action="beforeunload@window->unsaved-changes#leavingPage turbolinks:before-visit@window->unsaved-changes#leavingPage"
data-action="unsaved-changes#submit beforeunload@window->unsaved-changes#leavingPage turbolinks:before-visit@window->unsaved-changes#leavingPage"
data-unsaved-changes-changed="false"
data-unsaved-changes-disable-submit-button="false"
>
@@ -192,4 +194,39 @@ describe("UnsavedChangesController", () => {
})
})
})
describe('#submit', () => {
let checkbox
beforeEach(() => {
// Add a mock I18n object to
const mockedT = jest.fn()
mockedT.mockImplementation((string) => (string))
global.I18n = {
t: mockedT
}
checkbox = document.getElementById("test-checkbox")
})
afterEach(() => {
delete global.I18n
})
describe('when submiting the form', () => {
it("changed is set to true", () => {
const form = document.getElementById("test-form")
// interact with the form
checkbox.click()
// submit the form
const submitEvent = new Event("submit")
form.dispatchEvent(submitEvent)
expect(form.dataset.unsavedChangesChanged).toBe("false")
})
})
})
})