Skip to content

Commit b66cbd1

Browse files
Increase component test coverage
1 parent d66e189 commit b66cbd1

File tree

11 files changed

+826
-151
lines changed

11 files changed

+826
-151
lines changed

.eslintrc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ module.exports = {
150150
},
151151
{
152152
// Configure ESLint in test files
153-
files: ['**/*.test.{cjs,js,mjs}', 'jest.config.*', 'jest.setup.*'],
153+
files: [
154+
'**/*.test.{cjs,js,mjs}',
155+
'jest?(.*).config.*',
156+
'jest?(.*).setup.*'
157+
],
154158
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
155159
env: {
156160
browser: true,

jest.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ module.exports = {
4141
{
4242
...config,
4343
displayName: 'JavaScript behaviour tests',
44-
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
44+
setupFilesAfterEnv: [
45+
'<rootDir>/shared/helpers/jest/environment/jest.jsdom.setup.js'
46+
],
4547
testEnvironment: 'jsdom',
4648
testMatch: ['<rootDir>/**/*.jsdom.test.{js,mjs}']
4749
},
4850
{
4951
...config,
5052
displayName: 'JavaScript component tests',
51-
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
53+
setupFilesAfterEnv: [
54+
'<rootDir>/shared/helpers/jest/environment/jest.puppeteer.setup.js'
55+
],
5256
testEnvironment: 'jest-environment-puppeteer',
5357
testMatch: ['<rootDir>/**/*.puppeteer.test.{js,mjs}'],
5458

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
1+
import { getByRole } from '@testing-library/dom'
2+
13
import initButton from './button.js'
24

3-
describe('mis-instantiation', () => {
4-
it('does not prevent further JavaScript from running', () => {
5-
expect(() => {
6-
// `undefined` simulates the element being missing,
7-
// from an unchecked `document.querySelector` for example
8-
initButton(undefined)
9-
}).not.toThrow()
5+
describe('Button', () => {
6+
/** @type {HTMLButtonElement} */
7+
let button
8+
9+
beforeEach(() => {
10+
document.body.innerHTML = `
11+
<button class="nhsuk-button" data-module="nhsuk-button" type="submit">
12+
Save and continue
13+
</button>
14+
`
15+
16+
button = getByRole(document.body, 'button')
17+
18+
jest.spyOn(button, 'addEventListener')
19+
})
20+
21+
describe('Exports', () => {
22+
it('should export init function', () => {
23+
expect(initButton).toBeInstanceOf(Function)
24+
})
25+
})
26+
27+
describe('Initialisation', () => {
28+
it('should add event listeners', () => {
29+
initButton()
30+
31+
expect(button.addEventListener).toHaveBeenNthCalledWith(
32+
1,
33+
'keydown',
34+
expect.any(Function)
35+
)
36+
37+
expect(button.addEventListener).toHaveBeenNthCalledWith(
38+
2,
39+
'click',
40+
expect.any(Function)
41+
)
42+
})
43+
44+
it('should not throw with missing button', () => {
45+
button.remove()
46+
expect(() => initButton()).not.toThrow()
47+
})
48+
49+
it('should not throw with empty body', () => {
50+
document.body.innerHTML = ''
51+
expect(() => initButton()).not.toThrow()
52+
})
53+
54+
it('should not throw with empty scope', () => {
55+
const scope = document.createElement('div')
56+
expect(() => initButton({ scope })).not.toThrow()
57+
})
1058
})
1159
})
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { getByRole } from '@testing-library/dom'
2+
3+
import initCharacterCount from './character-count.js'
4+
5+
describe('Character count', () => {
6+
/** @type {HTMLTextAreaElement} */
7+
let textarea
8+
9+
beforeEach(() => {
10+
document.body.innerHTML = `
11+
<div class="nhsuk-character-count" data-module="nhsuk-character-count" data-maxlength="10">
12+
<div class="nhsuk-form-group">
13+
<label class="nhsuk-label" for="more-detail">
14+
Can you provide more detail?
15+
</label>
16+
<div class="nhsuk-hint" id="more-detail-hint">
17+
Don't include personal or financial information, eg your National Insurance number or credit card details.
18+
</div>
19+
<textarea class="nhsuk-textarea nhsuk-js-character-count" id="more-detail" name="more-detail" rows="5" aria-describedby="more-detail-hint"></textarea>
20+
</div>
21+
<div class="nhsuk-hint nhsuk-character-count__message" id="more-detail-info">
22+
You can enter up to 10 characters
23+
</div>
24+
</div>
25+
`
26+
27+
const container = document.querySelector('.nhsuk-character-count')
28+
29+
textarea = getByRole(container, 'textbox', {
30+
name: 'Can you provide more detail?'
31+
})
32+
33+
jest.spyOn(textarea, 'addEventListener')
34+
})
35+
36+
describe('Exports', () => {
37+
it('should export init function', () => {
38+
expect(initCharacterCount).toBeInstanceOf(Function)
39+
})
40+
})
41+
42+
describe('Initialisation', () => {
43+
it('should add event listeners', () => {
44+
initCharacterCount()
45+
46+
expect(textarea.addEventListener).toHaveBeenCalledWith(
47+
'keyup',
48+
expect.any(Function)
49+
)
50+
51+
expect(textarea.addEventListener).toHaveBeenCalledWith(
52+
'focus',
53+
expect.any(Function)
54+
)
55+
56+
expect(textarea.addEventListener).toHaveBeenCalledWith(
57+
'blur',
58+
expect.any(Function)
59+
)
60+
})
61+
62+
it('should not throw with missing textarea', () => {
63+
textarea.remove()
64+
expect(() => initCharacterCount()).not.toThrow()
65+
})
66+
67+
it('should not throw with empty body', () => {
68+
document.body.innerHTML = ''
69+
expect(() => initCharacterCount()).not.toThrow()
70+
})
71+
72+
it('should not throw with empty scope', () => {
73+
const scope = document.createElement('div')
74+
expect(() => initCharacterCount({ scope })).not.toThrow()
75+
})
76+
})
77+
})

0 commit comments

Comments
 (0)