Skip to content

Commit 1935ea1

Browse files
committed
[ajax.js] Fix undefined options in ec-crud-ajax-form-before event
1 parent 21c5fd8 commit 1935ea1

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

assets/js/ajax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export function link (link, options) {
251251
return sendRequest(options)
252252
}
253253

254-
export function sendForm (form, options) {
254+
export function sendForm (form, options = {}) {
255255
const eventBefore = new CustomEvent('ec-crud-ajax-form-before', {
256256
bubbles: true,
257257
cancelable: true,

tests/assets/js/ajax.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,40 @@ describe('Test Ajax.form', function () {
14371437
expect(callbackFormComplete).not.toHaveBeenCalled()
14381438
})
14391439

1440+
it('Send request with form without options argument and options changed by ec-crud-ajax-form-before event', async function () {
1441+
$('body').append('<form action="/goodRequest" method="POST" class="html-test" id="formToTest"><input type="hidden" name="_token" value="my-csrf-token" /><input type="text" name="var1" /></form>')
1442+
$('#formToTest input[name=var1]').val('My value 1')
1443+
1444+
const callbackFormBefore = jasmine.createSpy('form_before')
1445+
1446+
$(document).on('ec-crud-ajax-form-before', function (event) {
1447+
expect(event.detail.options).toBeInstanceOf(Object)
1448+
event.detail.options.options = {
1449+
headers: {
1450+
'X-Test': '1'
1451+
}
1452+
}
1453+
callbackFormBefore()
1454+
})
1455+
1456+
// The "options" argument is omitted, like in onSubmitFormAuto
1457+
const promise = ajax.sendForm('#formToTest')
1458+
expect(promise).toBeInstanceOf(Promise)
1459+
1460+
const response = await promise
1461+
1462+
const requestHeaders = {}
1463+
Object.entries(jasmine.Ajax.requests.mostRecent().requestHeaders).forEach((header) => {
1464+
requestHeaders[header[0].toLowerCase()] = header[1]
1465+
})
1466+
1467+
expect(response).toBeInstanceOf(Response)
1468+
expect(jasmine.Ajax.requests.mostRecent().url).toMatch('/goodRequest')
1469+
expect(jasmine.Ajax.requests.mostRecent().data()).toEqual([['_token', 'my-csrf-token'], ['var1', 'My value 1']]) // Parsed by addJasmineAjaxFormDataSupport
1470+
expect(requestHeaders['x-test']).toEqual('1')
1471+
expect(callbackFormBefore).toHaveBeenCalled()
1472+
})
1473+
14401474
it('Send request with form and complete callback', async function () {
14411475
$('body').append('<form action="/goodRequest" method="POST" class="html-test" id="formToTest"><input type="text" name="var1" /><input type="text" name="var2" /></form>')
14421476
$('#formToTest input[name=var1]').val('My value 1')

0 commit comments

Comments
 (0)