/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import FormUtils from '../../../../media/js/newsletter/form-utils.es6'; import CountryForm from '../../../../media/js/newsletter/country.es6'; const TOKEN_MOCK = 'a1a2a3a4-abc1-12ab-a123-12345a12345b'; describe('CountryForm', function () { beforeEach(async function () { const form = `
`; document.body.insertAdjacentHTML('beforeend', form); }); afterEach(function () { const form = document.getElementById('country-newsletter-test-form'); form.parentNode.removeChild(form); }); describe('form submission', function () { let xhr; let xhrRequests = []; beforeEach(function () { spyOn(FormUtils, 'getUserToken').and.returnValue(TOKEN_MOCK); xhr = sinon.useFakeXMLHttpRequest(); xhr.onCreate = (req) => { xhrRequests.push(req); }; }); afterEach(function () { xhr.restore(); xhrRequests = []; }); it('should handle success', function () { spyOn(CountryForm, 'handleFormSuccess').and.callThrough(); CountryForm.init(); document.getElementById('country-newsletter-submit').click(); xhrRequests[0].respond( 200, { 'Content-Type': 'application/json' }, '{"status": "ok"}' ); expect(xhrRequests[0].url).toEqual( `https://basket.mozilla.org/news/user-meta/${TOKEN_MOCK}/` ); expect(CountryForm.handleFormSuccess).toHaveBeenCalled(); expect( document .querySelector('.country-newsletter-content') .classList.contains('hidden') ).toBeTrue(); expect( document .querySelector('.country-newsletter-thanks') .classList.contains('hidden') ).toBeFalse(); }); it('should handle unknown error', function () { spyOn(CountryForm, 'handleFormError').and.callThrough(); CountryForm.init(); document.getElementById('country-newsletter-submit').click(); xhrRequests[0].respond( 400, { 'Content-Type': 'application/json' }, '{"status": "error", "desc": "Unknown non-helpful error"}' ); expect(CountryForm.handleFormError).toHaveBeenCalled(); expect( document .getElementById('country-newsletter-errors') .classList.contains('hidden') ).toBeFalse(); expect( document .querySelector('.error-try-again-later') .classList.contains('hidden') ).toBeFalse(); }); it('should handle failure', function () { spyOn(CountryForm, 'handleFormError').and.callThrough(); CountryForm.init(); document.getElementById('country-newsletter-submit').click(); xhrRequests[0].respond( 500, { 'Content-Type': 'application/json' }, null ); expect(CountryForm.handleFormError).toHaveBeenCalled(); expect( document .getElementById('country-newsletter-errors') .classList.contains('hidden') ).toBeFalse(); expect( document .querySelector('.error-try-again-later') .classList.contains('hidden') ).toBeFalse(); }); }); });