mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-25 08:44:23 -04:00
Merge pull request #3413 from alphagov/delay-initial-ajax-call
Delay the initial AJAX call by 2 seconds
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
var queues = {};
|
var queues = {};
|
||||||
var dd = new global.diffDOM();
|
var dd = new global.diffDOM();
|
||||||
|
var defaultInterval = 2000;
|
||||||
var interval = 0;
|
var interval = 0;
|
||||||
|
|
||||||
var calculateBackoff = responseTime => parseInt(Math.max(
|
var calculateBackoff = responseTime => parseInt(Math.max(
|
||||||
@@ -54,11 +55,14 @@
|
|||||||
|
|
||||||
global.GOVUK.Modules.UpdateContent = function() {
|
global.GOVUK.Modules.UpdateContent = function() {
|
||||||
|
|
||||||
this.start = component => poll(
|
this.start = component => setTimeout(
|
||||||
getRenderer($(component)),
|
() => poll(
|
||||||
$(component).data('resource'),
|
getRenderer($(component)),
|
||||||
getQueue($(component).data('resource')),
|
$(component).data('resource'),
|
||||||
$(component).data('form')
|
getQueue($(component).data('resource')),
|
||||||
|
$(component).data('form')
|
||||||
|
),
|
||||||
|
defaultInterval
|
||||||
);
|
);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ describe('Update content', () => {
|
|||||||
|
|
||||||
// start the module
|
// start the module
|
||||||
window.GOVUK.modules.start();
|
window.GOVUK.modules.start();
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
|
|
||||||
expect($.ajax.mock.calls[0][0]).toEqual(resourceURL);
|
expect($.ajax.mock.calls[0][0]).toEqual(resourceURL);
|
||||||
|
|
||||||
@@ -122,6 +123,7 @@ describe('Update content', () => {
|
|||||||
|
|
||||||
// start the module
|
// start the module
|
||||||
window.GOVUK.modules.start();
|
window.GOVUK.modules.start();
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
|
|
||||||
// check the right DOM node is updated
|
// check the right DOM node is updated
|
||||||
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("0");
|
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("0");
|
||||||
@@ -135,6 +137,7 @@ describe('Update content', () => {
|
|||||||
|
|
||||||
// start the module
|
// start the module
|
||||||
window.GOVUK.modules.start();
|
window.GOVUK.modules.start();
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
|
|
||||||
// check the right DOM node is updated
|
// check the right DOM node is updated
|
||||||
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("1");
|
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("1");
|
||||||
@@ -152,19 +155,26 @@ describe('Update content', () => {
|
|||||||
|
|
||||||
test("It should use the GET HTTP method", () => {
|
test("It should use the GET HTTP method", () => {
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
expect($.ajax.mock.calls[0][1].method).toEqual('get');
|
expect($.ajax.mock.calls[0][1].method).toEqual('get');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("It shouldn't send any data as part of the requests", () => {
|
test("It shouldn't send any data as part of the requests", () => {
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
expect($.ajax.mock.calls[0][1].data).toEqual({});
|
expect($.ajax.mock.calls[0][1].data).toEqual({});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("It should request updates with a dynamic interval", () => {
|
test("It should request updates with a dynamic interval", () => {
|
||||||
|
|
||||||
// First call happens straight away
|
// First call doesn’t happen in the first 2000ms
|
||||||
|
jest.advanceTimersByTime(1999);
|
||||||
|
expect($.ajax).toHaveBeenCalledTimes(0);
|
||||||
|
|
||||||
|
// But it happens after 2000ms by default
|
||||||
|
jest.advanceTimersByTime(1);
|
||||||
expect($.ajax).toHaveBeenCalledTimes(1);
|
expect($.ajax).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
// It took the server 1000ms to respond to the first call so we
|
// It took the server 1000ms to respond to the first call so we
|
||||||
@@ -213,12 +223,14 @@ describe('Update content', () => {
|
|||||||
|
|
||||||
test("requests should use the same HTTP method as the form", () => {
|
test("requests should use the same HTTP method as the form", () => {
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
expect($.ajax.mock.calls[0][1].method).toEqual('post');
|
expect($.ajax.mock.calls[0][1].method).toEqual('post');
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test("requests should use the data from the form", () => {
|
test("requests should use the data from the form", () => {
|
||||||
|
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
expect($.ajax.mock.calls[0][1].data).toEqual(helpers.getFormDataFromPairs([
|
expect($.ajax.mock.calls[0][1].data).toEqual(helpers.getFormDataFromPairs([
|
||||||
['serviceName', 'Buckhurst surgery'],
|
['serviceName', 'Buckhurst surgery'],
|
||||||
['serviceNumber', serviceNumber]
|
['serviceNumber', serviceNumber]
|
||||||
|
|||||||
Reference in New Issue
Block a user