mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-10 11:24:10 -04:00
Add utility helper for making form data sendable
Useful for assertions where the data you're comparing is already in this format.
This commit is contained in:
@@ -4,6 +4,7 @@ const html = require('./helpers/html.js');
|
||||
const elements = require('./helpers/elements.js');
|
||||
const rendering = require('./helpers/rendering.js');
|
||||
const forms = require('./helpers/forms.js');
|
||||
const utilities = require('./helpers/utilities.js');
|
||||
|
||||
exports.triggerEvent = events.triggerEvent;
|
||||
exports.clickElementWithMouse = events.clickElementWithMouse;
|
||||
@@ -18,3 +19,4 @@ exports.element = elements.element;
|
||||
exports.WindowMock = rendering.WindowMock;
|
||||
exports.ScreenMock = rendering.ScreenMock;
|
||||
exports.spyOnFormSubmit = forms.spyOnFormSubmit;
|
||||
exports.getFormDataFromPairs = utilities.getFormDataFromPairs;
|
||||
|
||||
22
tests/javascripts/support/helpers/utilities.js
Normal file
22
tests/javascripts/support/helpers/utilities.js
Normal file
@@ -0,0 +1,22 @@
|
||||
// general helpers, not related to the DOM and usable in different contexts
|
||||
|
||||
// turn a list of key=value pairs (like tuples) into data that can be sent via AJAX
|
||||
// taken from https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript
|
||||
// but requiring an array as input rather than a hash, to preserve order of pairs
|
||||
function getFormDataFromPairs (pairs) {
|
||||
|
||||
const urlEncodedDataPairs = [];
|
||||
|
||||
pairs.forEach(pair => {
|
||||
|
||||
urlEncodedDataPairs.push(`${window.encodeURIComponent(pair[0])}=${window.encodeURIComponent(pair[1])}`);
|
||||
|
||||
});
|
||||
|
||||
// Combine the pairs into a single string and replace all %-encoded spaces to
|
||||
// the '+' character; matches the behaviour of browser form submissions.
|
||||
return urlEncodedDataPairs.join('&').replace(/%20/g, '+');
|
||||
|
||||
};
|
||||
|
||||
exports.getFormDataFromPairs = getFormDataFromPairs;
|
||||
Reference in New Issue
Block a user