const each = require('jest-each').default; const jestDateMock = require('jest-date-mock'); const helpers = require('./support/helpers.js'); const serviceNumber = '6658542f-0cad-491f-bec8-ab8457700ead'; const updatesURL = `/services/${serviceNumber}/templates/count-sms-length`; let responseObj = {}; let jqueryAJAXReturnObj; beforeAll(() => { // ensure all timers go through Jest jest.useFakeTimers(); // mock the bits of jQuery used jest.spyOn(window.$, 'ajax'); // set up the object returned from $.ajax so it responds with whatever responseObj is set to jqueryAJAXReturnObj = { done: callback => { // For these tests the server responds immediately callback(responseObj); return jqueryAJAXReturnObj; }, fail: () => {} }; $.ajax.mockImplementation(() => jqueryAJAXReturnObj); require('../../app/assets/javascripts/updateStatus.js'); }); afterAll(() => { require('./support/teardown.js'); }); describe('Update content', () => { beforeEach(() => { document.body.innerHTML = `
Initial content
`; }); afterEach(() => { document.body.innerHTML = ''; // tidy up record of mocked AJAX calls $.ajax.mockClear(); // ensure any timers set by continually starting the module are cleared jest.clearAllTimers(); }); test("It should make requests to the URL specified in the data-updates-url attribute", () => { window.GOVUK.modules.start(); expect($.ajax.mock.calls[0][0]).toEqual(updatesURL); expect($.ajax.mock.calls[0]).toEqual([ updatesURL, { "data": "csrf_token=abc123&template_content=Content%20of%20message", "method": "post" } ]); }); test("It should replace the content of the div with the returned HTML", () => { responseObj = {'html': 'Updated content'} expect( document.querySelectorAll('[data-module=update-status]')[0].textContent.trim() ).toEqual( "Initial content" ); window.GOVUK.modules.start(); expect( document.querySelectorAll('[data-module=update-status]')[0].textContent.trim() ).toEqual( "Updated content" ); }); test("It should fire when the content of the textarea changes", () => { let textarea = document.getElementById('template_content'); // Initial update triggered window.GOVUK.modules.start(); expect($.ajax.mock.calls.length).toEqual(1); helpers.triggerEvent(textarea, 'input'); expect($.ajax.mock.calls.length).toEqual(2); }); });