-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update newsletter recovery to use new JSON API (#15787)
- Loading branch information
Showing
6 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,61 @@ const FormUtils = { | |
xhr.send(params); | ||
}, | ||
|
||
/** | ||
* Perform an AJAX POST to Basket as JSON | ||
* @param {String} email | ||
* @param {Object} params (JSON object) | ||
* @param {String} url (Basket API endpoint) | ||
* @param {Function} successCallback | ||
* @param {Function} errorCallback | ||
*/ | ||
postJsonToBasket: (email, params, url, successCallback, errorCallback) => { | ||
const xhr = new XMLHttpRequest(); | ||
|
||
// Emails used in automation for page-level integration tests | ||
// should avoid hitting basket directly. | ||
if (email === '[email protected]') { | ||
successCallback(); | ||
return; | ||
} else if (email === '[email protected]') { | ||
errorCallback(); | ||
return; | ||
} | ||
|
||
xhr.onload = function (e) { | ||
let response = e.target.response || e.target.responseText; | ||
|
||
if (typeof response !== 'object') { | ||
response = JSON.parse(response); | ||
} | ||
|
||
if (response) { | ||
if ( | ||
response.status === 'ok' && | ||
e.target.status >= 200 && | ||
e.target.status < 300 | ||
) { | ||
successCallback(); | ||
} else if (response.status === 'error' && response.desc) { | ||
errorCallback(response.desc); | ||
} else { | ||
errorCallback(); | ||
} | ||
} else { | ||
errorCallback(); | ||
} | ||
}; | ||
|
||
xhr.onerror = errorCallback; | ||
xhr.open('POST', url, true); | ||
xhr.setRequestHeader('Content-type', 'application/json'); | ||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); | ||
xhr.timeout = 5000; | ||
xhr.ontimeout = errorCallback; | ||
xhr.responseType = 'json'; | ||
xhr.send(JSON.stringify(params)); | ||
}, | ||
|
||
/** | ||
* Removes Basket UUID token from page URL path and updates browser history. | ||
* Note: this function will remove *everything* from the path *after* the token. | ||
|
@@ -299,6 +354,22 @@ const FormUtils = { | |
return q.join('&'); | ||
}, | ||
|
||
/** | ||
* Helper function to serialize form data for XHR request. | ||
* @param {HTMLElement} form | ||
* @returns {Object} JSON object | ||
*/ | ||
serializeToJson: (form) => { | ||
const json = {}; | ||
for (let i = 0; i < form.elements.length; i++) { | ||
const elem = form.elements[i]; | ||
if (elem.name) { | ||
json[elem.name] = elem.value; | ||
} | ||
} | ||
return json; | ||
}, | ||
|
||
/** | ||
* Helper function that strips HTML tags from text form input. | ||
* @param {String} text | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,6 +229,42 @@ describe('serialize', function () { | |
}); | ||
}); | ||
|
||
describe('serializeToJson', function () { | ||
afterEach(function () { | ||
const form = document.querySelector('.send-to-device-form'); | ||
form.parentNode.removeChild(form); | ||
}); | ||
|
||
it('should return a JSON object as expected', function () { | ||
const form = `<form class="send-to-device-form" action="https://basket.mozilla.org/news/subscribe/" method="post"> | ||
<div class="send-to-device-form-fields"> | ||
<div class="platform-container"> | ||
<input type="hidden" name="newsletters" value="download-firefox-mobile-reco"> | ||
<input type="hidden" name="source-url" value="https://www.mozilla.org/en-US/firefox/browsers/mobile/ios/"> | ||
</div> | ||
<div class="mzp-c-field mzp-l-stretch"> | ||
<label class="mzp-c-field-label" for="s2d-hero-input">Enter your email</label> | ||
<input id="s2d-hero-input" class="mzp-c-field-control send-to-device-input" name="email" type="text" required="" value="[email protected]"> | ||
</div> | ||
<div class="mzp-c-button-container mzp-l-stretch"> | ||
<button type="submit" class="button mzp-c-button mzp-t-product ">Send</button> | ||
</div> | ||
</div> | ||
</form>`; | ||
document.body.insertAdjacentHTML('beforeend', form); | ||
expect( | ||
FormUtils.serializeToJson( | ||
document.querySelector('.send-to-device-form') | ||
) | ||
).toEqual({ | ||
newsletters: 'download-firefox-mobile-reco', | ||
'source-url': | ||
'https://www.mozilla.org/en-US/firefox/browsers/mobile/ios/', | ||
email: '[email protected]' | ||
}); | ||
}); | ||
}); | ||
|
||
describe('stripHTML', function () { | ||
it('should strip HTML tags from strings as expected', function () { | ||
expect( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters