mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-11 10:28:41 -04:00
Make updateContent persist specified classNames
Wrap the code that updates the HTML with changes from the server with code that stores and re-applies specified classes. This is to allow other JS to add classes which change the visual state of the HTML without them being considered by the code that diffs our in-page HTML against that from the server. They are called classesToPersist because this should make the visual state they create persist between updates. Includes the addition of tests for updateContent that cover the addition/deletion of elements so we can write a test for classNames persisting through updates. The existing tests only cover updates that change the content of elements. Just adding the test for these changes to those would simulate a scenario that doesn't exist in the app. Writing extra tests for the kind of updates these changes act on keeps them in line with the app code.
This commit is contained in:
@@ -11,10 +11,37 @@
|
|||||||
1000
|
1000
|
||||||
));
|
));
|
||||||
|
|
||||||
var getRenderer = $component => response => morphdom(
|
// Methods to ensure the DOM fragment is clean of classes added by JS before diffing
|
||||||
|
// and that they are replaced afterwards.
|
||||||
|
var classesToPersist = {
|
||||||
|
classNames: [],
|
||||||
|
$els: [],
|
||||||
|
remove: function () {
|
||||||
|
this.classNames.forEach(className => {
|
||||||
|
var $elsWithClassName = $('.' + className).removeClass(className);
|
||||||
|
|
||||||
|
// store elements for that className at the same index
|
||||||
|
this.$els.push($elsWithClassName);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
replace: function () {
|
||||||
|
this.classNames.forEach((className, index) => {
|
||||||
|
this.$els[index].addClass(className);
|
||||||
|
});
|
||||||
|
|
||||||
|
// remove references to elements
|
||||||
|
this.$els = [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var getRenderer = $component => response => {
|
||||||
|
classesToPersist.remove();
|
||||||
|
morphdom(
|
||||||
$component.get(0),
|
$component.get(0),
|
||||||
$(response[$component.data('key')]).get(0)
|
$(response[$component.data('key')]).get(0)
|
||||||
);
|
);
|
||||||
|
classesToPersist.replace();
|
||||||
|
};
|
||||||
|
|
||||||
var getQueue = resource => (
|
var getQueue = resource => (
|
||||||
queues[resource] = queues[resource] || []
|
queues[resource] = queues[resource] || []
|
||||||
@@ -55,15 +82,26 @@
|
|||||||
|
|
||||||
global.GOVUK.Modules.UpdateContent = function() {
|
global.GOVUK.Modules.UpdateContent = function() {
|
||||||
|
|
||||||
this.start = component => setTimeout(
|
this.start = component => {
|
||||||
|
var $component = $(component);
|
||||||
|
|
||||||
|
// store any classes that should persist through updates
|
||||||
|
if ($contents.data('classesToPersist') !== undefined) {
|
||||||
|
$contents.data('classesToPersist')
|
||||||
|
.split(' ')
|
||||||
|
.forEach(className => classesToPersist.classNames.push(className));
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(
|
||||||
() => poll(
|
() => poll(
|
||||||
getRenderer($(component)),
|
getRenderer($component),
|
||||||
$(component).data('resource'),
|
$component.data('resource'),
|
||||||
getQueue($(component).data('resource')),
|
getQueue($component.data('resource')),
|
||||||
$(component).data('form')
|
$component.data('form')
|
||||||
),
|
),
|
||||||
defaultInterval
|
defaultInterval
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,8 @@ describe('Update content', () => {
|
|||||||
let HTMLString;
|
let HTMLString;
|
||||||
let initialHTMLString;
|
let initialHTMLString;
|
||||||
|
|
||||||
|
describe('When updating the contents of DOM nodes', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
||||||
// store HTML in string to allow use in AJAX responses
|
// store HTML in string to allow use in AJAX responses
|
||||||
@@ -103,18 +105,6 @@ describe('Update 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-resource attribute", () => {
|
test("It should make requests to the URL specified in the data-resource attribute", () => {
|
||||||
|
|
||||||
// start the module
|
// start the module
|
||||||
@@ -134,7 +124,7 @@ describe('Update content', () => {
|
|||||||
window.GOVUK.modules.start();
|
window.GOVUK.modules.start();
|
||||||
jest.advanceTimersByTime(2000);
|
jest.advanceTimersByTime(2000);
|
||||||
|
|
||||||
// check the right DOM node is updated
|
// check a sample DOM node is unchanged
|
||||||
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("0");
|
expect(document.querySelectorAll('.big-number-number')[0].textContent.trim()).toEqual("0");
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -250,3 +240,249 @@ describe('Update content', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("When adding or removing DOM nodes", () => {
|
||||||
|
var 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>
|
||||||
|
<a class="file-list-filename-large govuk-link govuk-link--no-visited-state" href="/services/7597847f-ad8e-4600-8faf-c42a647d8dee/current-alerts/b9e53cda-54f9-47bc-9fb2-b78a11eda6a9">${content.title}</a>
|
||||||
|
</h2>
|
||||||
|
<div class="govuk-grid-row">
|
||||||
|
<div class="govuk-grid-column-one-half">
|
||||||
|
<span class="file-list-hint-large govuk-!-margin-bottom-2">
|
||||||
|
${content.hint}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="govuk-grid-column-one-half file-list-status">
|
||||||
|
<p class="govuk-body govuk-!-margin-bottom-0 govuk-hint">
|
||||||
|
${content.status}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<ul class="area-list">
|
||||||
|
${areas}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>`;
|
||||||
|
};
|
||||||
|
|
||||||
|
var getHTMLString = items => {
|
||||||
|
|
||||||
|
var itemsHTMLString = '';
|
||||||
|
|
||||||
|
items.forEach(item => itemsHTMLString += "\n" + getItemHTMLString(item));
|
||||||
|
|
||||||
|
return `<div class="ajax-block-container">
|
||||||
|
${itemsHTMLString};
|
||||||
|
<div class="keyline-block"></div>
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
test("If the response contains no changes, the DOM should stay the same", () => {
|
||||||
|
|
||||||
|
// store HTML in string to allow use in AJAX responses
|
||||||
|
HTMLString = getHTMLString([
|
||||||
|
{
|
||||||
|
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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
initialHTMLString = `<div data-module="update-content" data-resource="${resourceURL}" data-key="${updateKey}" aria-live="polite">
|
||||||
|
${HTMLString}
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
document.body.innerHTML = initialHTMLString;
|
||||||
|
|
||||||
|
// make the response have an extra item
|
||||||
|
responseObj[updateKey] = HTMLString;
|
||||||
|
|
||||||
|
// start the module
|
||||||
|
window.GOVUK.modules.start();
|
||||||
|
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");
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
test("If the response adds a node, the DOM should contain that node", () => {
|
||||||
|
|
||||||
|
// store HTML in string to allow use in AJAX responses
|
||||||
|
HTMLString = getHTMLString([
|
||||||
|
{
|
||||||
|
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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
initialHTMLString = `<div data-module="update-content" data-resource="${resourceURL}" data-key="${updateKey}" aria-live="polite">
|
||||||
|
${HTMLString}
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
document.body.innerHTML = initialHTMLString;
|
||||||
|
|
||||||
|
var updatedHTMLString = getHTMLString([
|
||||||
|
{
|
||||||
|
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"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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 the response have an extra item
|
||||||
|
responseObj[updateKey] = updatedHTMLString;
|
||||||
|
|
||||||
|
// start the module
|
||||||
|
window.GOVUK.modules.start();
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
test("If the response removes a node, the DOM should not contain that node", () => {
|
||||||
|
|
||||||
|
// store HTML in string to allow use in AJAX responses
|
||||||
|
HTMLString = getHTMLString([
|
||||||
|
{
|
||||||
|
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"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
initialHTMLString = `<div data-module="update-content" data-resource="${resourceURL}" data-key="${updateKey}" aria-live="polite">
|
||||||
|
${HTMLString}
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
document.body.innerHTML = initialHTMLString;
|
||||||
|
|
||||||
|
var updatedHTMLString = getHTMLString([
|
||||||
|
{
|
||||||
|
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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
// default the response to match the content inside div[data-module]
|
||||||
|
responseObj[updateKey] = updatedHTMLString;
|
||||||
|
|
||||||
|
// start the module
|
||||||
|
window.GOVUK.modules.start();
|
||||||
|
jest.advanceTimersByTime(2000);
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
test("If other scripts have added classes to the DOM, they should persist through updates", () => {
|
||||||
|
|
||||||
|
// store HTML in string to allow use in AJAX responses
|
||||||
|
HTMLString = getHTMLString([
|
||||||
|
{
|
||||||
|
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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
initialHTMLString = `<div data-module="update-content" data-resource="${resourceURL}" data-key="${updateKey}" aria-live="polite">
|
||||||
|
${HTMLString}
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
document.body.innerHTML = initialHTMLString;
|
||||||
|
|
||||||
|
// 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');
|
||||||
|
|
||||||
|
// make the response match the initial HTML to emulate a response with no changes
|
||||||
|
responseObj[updateKey] = HTMLString;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user