mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
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:
@@ -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")
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user