Delay AJAX calls if the server is slow to respond
By default our AJAX calls were 2 seconds. Then they were 5 seconds
because someone reckoned 2 seconds was putting too much load on the
system. Then we made them 10 seconds while we were having an incident.
Then we made them 20 seconds for the heaviest pages, but back to 5
seconds or 2 seconds for the rest of the pages.
This is not a good situation because:
- it slows all services down equally, no matter how much traffic they
have, or which features they have switched on
- it slows everything down by the same amount, no matter how much load
the platform is under
- the values are set based on our worst performance, until we manually
remember to switch them back
- we spend time during incidents deploying changes to slow down the
dashboard refresh time because it’s a nothing-to-lose change that
might relieve some symptoms, when we could be spending time digging
into the underlying cause
This pull request makes the Javascript smarter about how long it waits
until it makes another AJAX call. It bases the delay on how long the
server takes to respond (as a proxy for how much load the server is
under).
It’s based on the square root of the response time, so is more sensitive
to slow downs early on, and less sensitive to slow downs later on. This
helps us give a more pronounced difference in delay between an AJAX call
that is fast (for example the page for a single notification) and one
that is slow (for example a dashboard for a service with lots of
traffic).
*Some examples of what this would mean for various pages*
Page | Response time | Wait until next AJAX call
---|---|---
Check a reply to address | 130ms | 1,850ms
Brand new service dashboard | 229ms | 2,783ms
HM Passport Office dashboard | 634ms | 5,294ms
NHS Coronavirus Service dashboard | 779ms | 5,977ms
_Example of the kind of slowness we’ve seen during an incident_ | 6,000ms | 18,364ms
GOV.UK email dashboard | `HTTP 504` | 😬
2020-04-08 17:55:53 +01:00
|
|
|
|
const each = require('jest-each').default;
|
|
|
|
|
|
|
2019-09-02 14:27:46 +01:00
|
|
|
|
const helpers = require('./support/helpers.js');
|
|
|
|
|
|
|
|
|
|
|
|
const serviceNumber = '6658542f-0cad-491f-bec8-ab8457700ead';
|
|
|
|
|
|
const resourceURL = `/services/${serviceNumber}/notifications/email.json?status=sending%2Cdelivered%2Cfailed`;
|
|
|
|
|
|
const updateKey = 'counts';
|
|
|
|
|
|
|
|
|
|
|
|
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 => {
|
Delay AJAX calls if the server is slow to respond
By default our AJAX calls were 2 seconds. Then they were 5 seconds
because someone reckoned 2 seconds was putting too much load on the
system. Then we made them 10 seconds while we were having an incident.
Then we made them 20 seconds for the heaviest pages, but back to 5
seconds or 2 seconds for the rest of the pages.
This is not a good situation because:
- it slows all services down equally, no matter how much traffic they
have, or which features they have switched on
- it slows everything down by the same amount, no matter how much load
the platform is under
- the values are set based on our worst performance, until we manually
remember to switch them back
- we spend time during incidents deploying changes to slow down the
dashboard refresh time because it’s a nothing-to-lose change that
might relieve some symptoms, when we could be spending time digging
into the underlying cause
This pull request makes the Javascript smarter about how long it waits
until it makes another AJAX call. It bases the delay on how long the
server takes to respond (as a proxy for how much load the server is
under).
It’s based on the square root of the response time, so is more sensitive
to slow downs early on, and less sensitive to slow downs later on. This
helps us give a more pronounced difference in delay between an AJAX call
that is fast (for example the page for a single notification) and one
that is slow (for example a dashboard for a service with lots of
traffic).
*Some examples of what this would mean for various pages*
Page | Response time | Wait until next AJAX call
---|---|---
Check a reply to address | 130ms | 1,850ms
Brand new service dashboard | 229ms | 2,783ms
HM Passport Office dashboard | 634ms | 5,294ms
NHS Coronavirus Service dashboard | 779ms | 5,977ms
_Example of the kind of slowness we’ve seen during an incident_ | 6,000ms | 18,364ms
GOV.UK email dashboard | `HTTP 504` | 😬
2020-04-08 17:55:53 +01:00
|
|
|
|
// The server takes 1 second to respond
|
2022-10-27 11:12:39 -04:00
|
|
|
|
jest.advanceTimersByTime(1000);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
callback(responseObj);
|
|
|
|
|
|
return jqueryAJAXReturnObj;
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: () => {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$.ajax.mockImplementation(() => jqueryAJAXReturnObj);
|
|
|
|
|
|
|
2022-01-27 10:44:20 +00:00
|
|
|
|
// RollupJS assigns our bundled module code, including morphdom, to window.GOVUK.
|
|
|
|
|
|
// morphdom is assigned to its vendor property so we need to copy that here for the updateContent
|
|
|
|
|
|
// code to pick it up.
|
|
|
|
|
|
window.GOVUK.vendor = {
|
|
|
|
|
|
morphdom: require('morphdom')
|
|
|
|
|
|
};
|
2019-09-02 14:27:46 +01:00
|
|
|
|
require('../../app/assets/javascripts/updateContent.js');
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
|
|
require('./support/teardown.js');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('Update content', () => {
|
|
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
const getInitialHTMLString = partial => `
|
|
|
|
|
|
<div data-module="update-content" data-resource="${resourceURL}" data-key="${updateKey}">
|
|
|
|
|
|
${partial}
|
|
|
|
|
|
</div>`;
|
2021-09-14 16:57:52 +01:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
describe("All variations", () => {
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
// Intentionally basic example because we're not testing changes to the partial
|
|
|
|
|
|
document.body.innerHTML = getInitialHTMLString(`<p class="notification-status">Sending</p>`);
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
// default the response to match the content inside div[data-module]
|
2022-02-16 11:25:50 +00:00
|
|
|
|
responseObj[updateKey] = `<p class="notification-status">Sending</p>`;
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
describe("By default", () => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
beforeEach(() => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
test("It should use the GET HTTP method", () => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
expect($.ajax.mock.calls[0][1].method).toEqual('get');
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
test("It shouldn't send any data as part of the requests", () => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
expect($.ajax.mock.calls[0][1].data).toEqual({});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
test("It should request updates with a dynamic interval", () => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// First call doesn’t happen in the first 2000ms
|
|
|
|
|
|
jest.advanceTimersByTime(1999);
|
|
|
|
|
|
expect($.ajax).toHaveBeenCalledTimes(0);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// But it happens after 2000ms by default
|
|
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
|
expect($.ajax).toHaveBeenCalledTimes(1);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// It took the server 1000ms to respond to the first call so we
|
|
|
|
|
|
// will back off – the next call shouldn’t happen in the next 6904ms
|
|
|
|
|
|
jest.advanceTimersByTime(6904);
|
|
|
|
|
|
expect($.ajax).toHaveBeenCalledTimes(1);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// But it should happen after 6905ms
|
|
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
|
|
expect($.ajax).toHaveBeenCalledTimes(2);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
each([
|
|
|
|
|
|
[1000, 0],
|
|
|
|
|
|
[1500, 100],
|
|
|
|
|
|
[4590, 500],
|
|
|
|
|
|
[6905, 1000],
|
|
|
|
|
|
[24000, 10000],
|
|
|
|
|
|
]).test('It calculates a delay of %dms if the API responds in %dms', (waitTime, responseTime) => {
|
|
|
|
|
|
expect(
|
|
|
|
|
|
window.GOVUK.Modules.UpdateContent.calculateBackoff(responseTime)
|
|
|
|
|
|
).toBe(
|
|
|
|
|
|
waitTime
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
describe("If a form is used as a source for data, referenced in the data-form attribute", () => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
beforeEach(() => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
// Add a form to the page
|
2022-02-08 11:33:13 +00:00
|
|
|
|
document.body.innerHTML += `
|
|
|
|
|
|
<form method="post" id="service">
|
|
|
|
|
|
<input type="hidden" name="serviceName" value="Buckhurst surgery" />
|
|
|
|
|
|
<input type="hidden" name="serviceNumber" value="${serviceNumber}" />
|
|
|
|
|
|
</form>`;
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
// Link the component to the form
|
2022-02-08 11:33:13 +00:00
|
|
|
|
document.querySelector('[data-module=update-content]').setAttribute('data-form', 'service');
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
2020-04-16 13:21:24 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
test("requests should use the same HTTP method as the form", () => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
expect($.ajax.mock.calls[0][1].method).toEqual('post');
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
test("requests should use the data from the form", () => {
|
|
|
|
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
expect($.ajax.mock.calls[0][1].data).toEqual(helpers.getFormDataFromPairs([
|
|
|
|
|
|
['serviceName', 'Buckhurst surgery'],
|
|
|
|
|
|
['serviceNumber', serviceNumber]
|
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
|
|
})
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
Delay AJAX calls if the server is slow to respond
By default our AJAX calls were 2 seconds. Then they were 5 seconds
because someone reckoned 2 seconds was putting too much load on the
system. Then we made them 10 seconds while we were having an incident.
Then we made them 20 seconds for the heaviest pages, but back to 5
seconds or 2 seconds for the rest of the pages.
This is not a good situation because:
- it slows all services down equally, no matter how much traffic they
have, or which features they have switched on
- it slows everything down by the same amount, no matter how much load
the platform is under
- the values are set based on our worst performance, until we manually
remember to switch them back
- we spend time during incidents deploying changes to slow down the
dashboard refresh time because it’s a nothing-to-lose change that
might relieve some symptoms, when we could be spending time digging
into the underlying cause
This pull request makes the Javascript smarter about how long it waits
until it makes another AJAX call. It bases the delay on how long the
server takes to respond (as a proxy for how much load the server is
under).
It’s based on the square root of the response time, so is more sensitive
to slow downs early on, and less sensitive to slow downs later on. This
helps us give a more pronounced difference in delay between an AJAX call
that is fast (for example the page for a single notification) and one
that is slow (for example a dashboard for a service with lots of
traffic).
*Some examples of what this would mean for various pages*
Page | Response time | Wait until next AJAX call
---|---|---
Check a reply to address | 130ms | 1,850ms
Brand new service dashboard | 229ms | 2,783ms
HM Passport Office dashboard | 634ms | 5,294ms
NHS Coronavirus Service dashboard | 779ms | 5,977ms
_Example of the kind of slowness we’ve seen during an incident_ | 6,000ms | 18,364ms
GOV.UK email dashboard | `HTTP 504` | 😬
2020-04-08 17:55:53 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2019-09-02 14:27:46 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
describe('When updating the contents of DOM nodes', () => {
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
let partialData;
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
const getPartial = items => {
|
|
|
|
|
|
let pillsHTML = '';
|
|
|
|
|
|
|
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
|
pillsHTML += `
|
|
|
|
|
|
<li ${item.selected ? 'aria-selected="true"' : ''} role="tab">
|
|
|
|
|
|
<div ${item.selected ? 'class="pill-selected-item" tabindex="0"' : ''}>
|
|
|
|
|
|
<div class="big-number-smaller">
|
|
|
|
|
|
<div class="big-number-number">${item.count}</div>
|
2022-02-08 11:33:13 +00:00
|
|
|
|
</div>
|
2022-02-16 11:25:50 +00:00
|
|
|
|
<div class="pill-label">${item.label}</div>
|
2022-02-08 11:33:13 +00:00
|
|
|
|
</div>
|
2022-02-16 11:25:50 +00:00
|
|
|
|
</li>`;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return `
|
2023-08-14 10:35:11 -04:00
|
|
|
|
<div class="tabs ajax-block-container">
|
2022-02-16 11:25:50 +00:00
|
|
|
|
<ul role="tablist" class="pill">
|
|
|
|
|
|
${pillsHTML}
|
|
|
|
|
|
</ul>
|
2022-02-08 11:33:13 +00:00
|
|
|
|
</div>`;
|
|
|
|
|
|
};
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
|
|
|
|
|
|
partialData = [
|
|
|
|
|
|
{
|
|
|
|
|
|
count: 0,
|
|
|
|
|
|
label: 'total',
|
|
|
|
|
|
selected: true
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
count: 0,
|
|
|
|
|
|
label: 'sending',
|
|
|
|
|
|
selected: false
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
count: 0,
|
|
|
|
|
|
label: 'delivered',
|
|
|
|
|
|
selected: false
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
count: 0,
|
|
|
|
|
|
label: 'failed',
|
|
|
|
|
|
selected: false
|
|
|
|
|
|
}
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = getInitialHTMLString(getPartial(partialData));
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test("It should replace the original HTML with that of the partial, to match that returned from AJAX responses", () => {
|
|
|
|
|
|
|
|
|
|
|
|
// default the response to match the content inside div[data-module]
|
|
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
|
|
|
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
|
|
|
|
|
|
expect(document.querySelector('.ajax-block-container').parentNode.hasAttribute('data-resource')).toBe(false);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test("It should make requests to the URL specified in the data-resource attribute", () => {
|
|
|
|
|
|
|
|
|
|
|
|
// default the response to match the content inside div[data-module]
|
|
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
|
|
|
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
|
|
|
|
|
|
expect($.ajax.mock.calls[0][0]).toEqual(resourceURL);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test("If the response contains no changes, the DOM should stay the same", () => {
|
|
|
|
|
|
|
|
|
|
|
|
// send the done callback a response with updates included
|
|
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
|
|
|
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
|
|
|
|
|
|
// check a sample DOM node is unchanged
|
|
|
|
|
|
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("0");
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test("If the response contains changes, it should update the DOM with them", () => {
|
|
|
|
|
|
|
|
|
|
|
|
partialData[0].count = 1;
|
|
|
|
|
|
|
|
|
|
|
|
// send the done callback a response with updates included
|
|
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
|
|
|
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
|
|
|
|
|
|
// check the right DOM node is updated
|
|
|
|
|
|
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("1");
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe("When adding or removing DOM nodes", () => {
|
|
|
|
|
|
|
|
|
|
|
|
let partialData;
|
|
|
|
|
|
|
|
|
|
|
|
const getPartial = items => {
|
|
|
|
|
|
|
|
|
|
|
|
const getItemHTMLString = content => {
|
|
|
|
|
|
var areas = '';
|
|
|
|
|
|
|
|
|
|
|
|
content.areas.forEach(area =>
|
|
|
|
|
|
areas += "\n" + `<li class="area-list-item area-list-item--unremoveable area-list-item--smaller">${area}</li>`
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return `
|
|
|
|
|
|
<div class="keyline-block">
|
|
|
|
|
|
<div class="file-list govuk-!-margin-bottom-2">
|
|
|
|
|
|
<h2>
|
2023-08-08 16:19:17 -04:00
|
|
|
|
<a class="file-list-filename-large usa-link" href="/services/7597847f-ad8e-4600-8faf-c42a647d8dee/current-alerts/b9e53cda-54f9-47bc-9fb2-b78a11eda6a9">${content.title}</a>
|
2022-02-16 11:25:50 +00:00
|
|
|
|
</h2>
|
|
|
|
|
|
<div class="govuk-grid-row">
|
2023-08-28 12:12:21 -04:00
|
|
|
|
<div class="grid-col-6">
|
2022-02-16 11:25:50 +00:00
|
|
|
|
<span class="file-list-hint-large govuk-!-margin-bottom-2">
|
|
|
|
|
|
${content.hint}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
2023-08-28 12:12:21 -04:00
|
|
|
|
<div class="grid-col-6 file-list-status">
|
2023-08-25 11:49:13 -04:00
|
|
|
|
<p class="usa-body govuk-!-margin-bottom-0 usa-hint">
|
2022-02-16 11:25:50 +00:00
|
|
|
|
${content.status}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<ul class="area-list">
|
|
|
|
|
|
${areas}
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>`;
|
|
|
|
|
|
};
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
var itemsHTMLString = '';
|
|
|
|
|
|
|
|
|
|
|
|
items.forEach(item => itemsHTMLString += "\n" + getItemHTMLString(item));
|
|
|
|
|
|
|
|
|
|
|
|
return `<div class="ajax-block-container">
|
|
|
|
|
|
${itemsHTMLString};
|
|
|
|
|
|
<div class="keyline-block"></div>
|
|
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
beforeEach(() => {
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
partialData = [
|
2022-02-08 11:33:13 +00:00
|
|
|
|
{
|
|
|
|
|
|
title: "Gas leak",
|
|
|
|
|
|
hint: "There's a gas leak in the local area. Residents should vacate until further notice.",
|
|
|
|
|
|
status: "Waiting for approval",
|
|
|
|
|
|
areas: [
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi B",
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi C"
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
2022-02-16 11:25:50 +00:00
|
|
|
|
];
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test("If the response contains no changes, the DOM should stay the same", () => {
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
document.body.innerHTML = getInitialHTMLString(getPartial(partialData));
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-11 16:36:49 +00:00
|
|
|
|
// make a response with no changes
|
2022-02-16 11:25:50 +00:00
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
2022-02-08 11:33:13 +00:00
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
|
|
|
|
|
|
// check it has the same number of items
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list').length).toEqual(1);
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list h2 a')[0].textContent.trim()).toEqual("Gas leak");
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
test("If the response adds a node, the DOM should contain that node", () => {
|
|
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
document.body.innerHTML = getInitialHTMLString(getPartial(partialData));
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
partialData.push({
|
|
|
|
|
|
title: "Reservoir flooding template",
|
|
|
|
|
|
hint: "The local reservoir has flooded. All people within 5 miles should move to a safer location.",
|
|
|
|
|
|
status: "Waiting for approval",
|
|
|
|
|
|
areas: [
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi A",
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi D"
|
|
|
|
|
|
]
|
|
|
|
|
|
});
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
// make the response have an extra item
|
2022-02-16 11:25:50 +00:00
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
2020-04-16 13:21:24 +01:00
|
|
|
|
jest.advanceTimersByTime(2000);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// check the node has been added
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list').length).toEqual(2);
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list h2 a')[0].textContent.trim()).toEqual("Gas leak");
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list h2 a')[1].textContent.trim()).toEqual("Reservoir flooding template");
|
|
|
|
|
|
|
|
|
|
|
|
});
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
test("If the response removes a node, the DOM should not contain that node", () => {
|
|
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
// add another item so we start with 2
|
|
|
|
|
|
partialData.push({
|
|
|
|
|
|
title: "Reservoir flooding template",
|
|
|
|
|
|
hint: "The local reservoir has flooded. All people within 5 miles should move to a safer location.",
|
|
|
|
|
|
status: "Waiting for approval",
|
|
|
|
|
|
areas: [
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi A",
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi D"
|
|
|
|
|
|
]
|
|
|
|
|
|
});
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
document.body.innerHTML = getInitialHTMLString(getPartial(partialData));
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
// remove the last item
|
|
|
|
|
|
partialData.pop();
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
// default the response to match the content inside div[data-module]
|
2022-02-16 11:25:50 +00:00
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
2020-04-16 13:21:24 +01:00
|
|
|
|
jest.advanceTimersByTime(2000);
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
// check the node has been removed
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list').length).toEqual(1);
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list h2 a')[0].textContent.trim()).toEqual("Gas leak");
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-16 15:56:52 +00:00
|
|
|
|
test("If other scripts have added classes to the DOM, they should persist through updates to a single component", () => {
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
document.body.innerHTML = getInitialHTMLString(getPartial(partialData));
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
// mark classes to persist on the partial
|
|
|
|
|
|
document.querySelector('.ajax-block-container').setAttribute('data-classes-to-persist', 'js-child-has-focus');
|
|
|
|
|
|
|
|
|
|
|
|
// Add class to indicate focus state of link on parent heading
|
|
|
|
|
|
document.querySelectorAll('.file-list h2')[0].classList.add('js-child-has-focus');
|
|
|
|
|
|
|
2022-02-16 11:25:50 +00:00
|
|
|
|
// Add an item to trigger an update
|
|
|
|
|
|
partialData.push({
|
|
|
|
|
|
title: "Reservoir flooding template",
|
|
|
|
|
|
hint: "The local reservoir has flooded. All people within 5 miles should move to a safer location.",
|
|
|
|
|
|
status: "Waiting for approval",
|
|
|
|
|
|
areas: [
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi A",
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi D"
|
|
|
|
|
|
]
|
|
|
|
|
|
});
|
2022-02-11 16:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
// make the response have an extra item
|
2022-02-16 11:25:50 +00:00
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
2022-02-08 11:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
|
|
|
|
|
|
// check the class is still there
|
|
|
|
|
|
expect(document.querySelectorAll('.file-list h2')[0].classList.contains('js-child-has-focus')).toBe(true);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-16 15:56:52 +00:00
|
|
|
|
test("If other scripts have added classes to the DOM, they should persist through updates to multiple components", () => {
|
|
|
|
|
|
|
|
|
|
|
|
// Create duplicate components in the page
|
|
|
|
|
|
document.body.innerHTML = getInitialHTMLString(getPartial(partialData)) + "\n" + getInitialHTMLString(getPartial(partialData));
|
|
|
|
|
|
|
|
|
|
|
|
var partialsInPage = document.querySelectorAll('.ajax-block-container');
|
|
|
|
|
|
|
|
|
|
|
|
// Mark classes to persist on the partials (2nd is made up)
|
|
|
|
|
|
partialsInPage[0].setAttribute('data-classes-to-persist', 'js-child-has-focus');
|
|
|
|
|
|
partialsInPage[1].setAttribute('data-classes-to-persist', 'js-2nd-child-has-focus');
|
|
|
|
|
|
|
|
|
|
|
|
// Add examples of those classes on each partial (2nd is made up)
|
|
|
|
|
|
partialsInPage[0].querySelectorAll('.file-list h2')[0].classList.add('js-child-has-focus');
|
|
|
|
|
|
partialsInPage[1].querySelectorAll('.file-list h2')[0].classList.add('js-2nd-child-has-focus');
|
|
|
|
|
|
|
|
|
|
|
|
// Add an item to trigger an update
|
|
|
|
|
|
partialData.push({
|
|
|
|
|
|
title: "Reservoir flooding template",
|
|
|
|
|
|
hint: "The local reservoir has flooded. All people within 5 miles should move to a safer location.",
|
|
|
|
|
|
status: "Waiting for approval",
|
|
|
|
|
|
areas: [
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi A",
|
|
|
|
|
|
"Santa Claus Village, Rovaniemi D"
|
|
|
|
|
|
]
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// make all responses have an extra item
|
|
|
|
|
|
responseObj[updateKey] = getPartial(partialData);
|
|
|
|
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
jest.advanceTimersByTime(2000);
|
|
|
|
|
|
|
|
|
|
|
|
// re-select in case nodes in partialsInPage have changed
|
|
|
|
|
|
partialsInPage = document.querySelectorAll('.ajax-block-container');
|
|
|
|
|
|
|
|
|
|
|
|
// check the classes are still there
|
|
|
|
|
|
expect(partialsInPage[0].querySelectorAll('.file-list h2')[0].classList.contains('js-child-has-focus')).toBe(true);
|
|
|
|
|
|
expect(partialsInPage[1].querySelectorAll('.file-list h2')[0].classList.contains('js-2nd-child-has-focus')).toBe(true);
|
|
|
|
|
|
|
2022-02-18 11:55:13 +00:00
|
|
|
|
// check each heading only has the classes assigned to it before updates occurred
|
|
|
|
|
|
expect(partialsInPage[0].querySelectorAll('.file-list h2')[0].classList.contains('js-2nd-child-has-focus')).toBe(false);
|
|
|
|
|
|
expect(partialsInPage[1].querySelectorAll('.file-list h2')[0].classList.contains('js-child-has-focus')).toBe(false);
|
|
|
|
|
|
|
2022-02-16 15:56:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2019-09-02 14:27:46 +01:00
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|