2020-09-15 21:23:49 +01:00
|
|
|
const helpers = require('./support/helpers.js');
|
|
|
|
|
|
2019-04-12 12:10:05 +01:00
|
|
|
beforeAll(() => {
|
|
|
|
|
require('../../app/assets/javascripts/autofocus.js');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
2019-05-21 10:56:38 +01:00
|
|
|
require('./support/teardown.js');
|
2019-04-12 12:10:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('Autofocus', () => {
|
|
|
|
|
|
2020-09-15 21:23:49 +01:00
|
|
|
const labelText = 'Search by name';
|
2019-04-12 12:10:05 +01:00
|
|
|
let focusHandler;
|
|
|
|
|
let search;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
2020-09-15 21:23:49 +01:00
|
|
|
document.title = 'Find services by name - GOV.UK Notify';
|
|
|
|
|
|
2019-04-12 12:10:05 +01:00
|
|
|
// set up DOM
|
|
|
|
|
document.body.innerHTML =
|
2021-11-04 11:28:31 +00:00
|
|
|
`<div id="wrapper">
|
2019-04-12 12:10:05 +01:00
|
|
|
<label class="form-label" for="search">
|
2020-09-15 21:23:49 +01:00
|
|
|
${labelText}
|
2019-04-12 12:10:05 +01:00
|
|
|
</label>
|
2020-08-27 08:49:36 +01:00
|
|
|
<input autocomplete="off" class="form-control form-control-1-1" id="search" name="search" type="search" value="" data-module="autofocus">
|
2019-04-12 12:10:05 +01:00
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
focusHandler = jest.fn();
|
|
|
|
|
search = document.getElementById('search');
|
|
|
|
|
search.addEventListener('focus', focusHandler, false);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
|
search.removeEventListener('focus', focusHandler);
|
|
|
|
|
focusHandler = null;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('is focused when modules start', () => {
|
|
|
|
|
|
|
|
|
|
// start module
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
|
|
|
|
expect(focusHandler).toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-04 11:28:31 +00:00
|
|
|
test('is focused when attribute is set on outer element', () => {
|
|
|
|
|
|
|
|
|
|
document.getElementById('search').removeAttribute('data-module');
|
|
|
|
|
document.getElementById('wrapper').setAttribute('data-module', 'autofocus');
|
|
|
|
|
|
|
|
|
|
// start module
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
|
|
|
|
expect(focusHandler).toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-12 12:10:05 +01:00
|
|
|
test('is not focused if the window has scrolled', () => {
|
|
|
|
|
|
|
|
|
|
// mock the window being scrolled 25px
|
|
|
|
|
$.prototype.scrollTop = jest.fn(() => 25);
|
|
|
|
|
|
|
|
|
|
// start module
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
|
|
|
|
expect(focusHandler).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('is focused if the window has scrolled but the force-focus flag is set', () => {
|
|
|
|
|
|
|
|
|
|
// mock the window being scrolled 25px
|
|
|
|
|
$.prototype.scrollTop = jest.fn(() => 25);
|
|
|
|
|
|
|
|
|
|
// set the force-focus flag
|
2020-08-27 08:49:36 +01:00
|
|
|
document.querySelector('#search').setAttribute('data-force-focus', true);
|
2019-04-12 12:10:05 +01:00
|
|
|
|
|
|
|
|
// start module
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
|
|
|
|
expect(focusHandler).toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|