From 2e558744a5e50d4bb61ec2ff83c1d60c6f58de7b Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Fri, 12 Apr 2019 11:51:51 +0100 Subject: [PATCH 1/4] Add Jest and peer dependencies --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index be991f89a..a00485f38 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,11 @@ "gulp-jshint": "2.1.0", "gulp-prettyerror": "1.2.1", "gulp-sass-lint": "1.4.0", + "jest": "24.7.1", "jshint": "2.10.2", "jshint-stylish": "2.2.1" + }, + "peerDependencies": { + "request": "2.88.0" } } From 00c1ebf02a613737101771c9b95612f62d882254 Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Fri, 12 Apr 2019 12:10:05 +0100 Subject: [PATCH 2/4] Add test for autofocus.js One of the simplest of our JavaScript files to test how difficult this is. Answer is not too bad and includes the file needing a DOM to operate on and jQuery in the global scope. --- tests/javascripts/autofocus.test.js | 84 +++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/javascripts/autofocus.test.js diff --git a/tests/javascripts/autofocus.test.js b/tests/javascripts/autofocus.test.js new file mode 100644 index 000000000..8419b2d62 --- /dev/null +++ b/tests/javascripts/autofocus.test.js @@ -0,0 +1,84 @@ +beforeAll(() => { + // set up jQuery + window.jQuery = require('jquery'); + $ = window.jQuery; + + // load module code + require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); + require('../../app/assets/javascripts/autofocus.js'); +}); + +afterAll(() => { + window.jQuery = null; + $ = null; + + delete window.GOVUK; +}); + +describe('Autofocus', () => { + + let focusHandler; + let search; + + beforeEach(() => { + + // set up DOM + document.body.innerHTML = + `
+ + +
`; + + 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(); + + }); + + 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 + document.querySelector('div').setAttribute('data-force-focus', true); + + // start module + window.GOVUK.modules.start(); + + expect(focusHandler).toHaveBeenCalled(); + + }); + +}); From 1f948cc0b4d9e7a61798864d377197efc3f3b207 Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Tue, 16 Apr 2019 12:23:28 +0100 Subject: [PATCH 3/4] Run Jest along with other JS tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a00485f38..6bdb4fd42 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "node": "10.15.3" }, "scripts": { - "test": "gulp lint", + "test": "gulp lint && jest tests/javascripts", "build": "gulp", "watch": "gulp watch" }, From b11ab2c384abdb169b9940aa55d77fcbb723720f Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Wed, 24 Apr 2019 11:26:55 +0100 Subject: [PATCH 4/4] Remove peer dependencies Peer dependencies are defined by projects intended to be used by other projects (ie, libraries). This doesn't apply to this project so removing the peer dependencies field. I'm also not sure why the `request` dependency was there as it isn't a peer dependency of any of the packages that use it in this project's dependency tree. --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 6bdb4fd42..71c11b281 100644 --- a/package.json +++ b/package.json @@ -46,8 +46,5 @@ "jest": "24.7.1", "jshint": "2.10.2", "jshint-stylish": "2.2.1" - }, - "peerDependencies": { - "request": "2.88.0" } }