|
| 1 | +import { getByRole } from '@testing-library/dom' |
| 2 | + |
1 | 3 | import initCheckboxes from './checkboxes.js'
|
2 | 4 |
|
3 |
| -describe('Checkboxes module', () => { |
4 |
| - describe('does not throw an error', () => { |
5 |
| - it('if there is no conditional checkboxes container', () => { |
| 5 | +describe('Checkboxes', () => { |
| 6 | + /** @type {HTMLDivElement[]} */ |
| 7 | + let conditionals |
| 8 | + |
| 9 | + /** @type {HTMLInputElement[]} */ |
| 10 | + let inputs |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + document.body.innerHTML = ` |
| 14 | + <form method="post" novalidate> |
| 15 | + <div class="nhsuk-form-group"> |
| 16 | + <fieldset class="nhsuk-fieldset" aria-describedby="contact-hint"> |
| 17 | + <legend class="nhsuk-fieldset__legend nhsuk-fieldset__legend--m"> |
| 18 | + How would you prefer to be contacted? |
| 19 | + </legend> |
| 20 | +
|
| 21 | + <div class="nhsuk-hint" id="contact-hint"> |
| 22 | + Select all options that are relevant to you. |
| 23 | + </div> |
| 24 | +
|
| 25 | + <div class="nhsuk-checkboxes nhsuk-checkboxes--conditional"> |
| 26 | + <div class="nhsuk-checkboxes__item"> |
| 27 | + <input class="nhsuk-checkboxes__input" id="contact-1" name="contact" type="checkbox" value="email" aria-controls="conditional-contact-1" aria-expanded="false"> |
| 28 | + <label class="nhsuk-label nhsuk-checkboxes__label" for="contact-1"> |
| 29 | + Email |
| 30 | + </label> |
| 31 | + </div> |
| 32 | +
|
| 33 | + <div class="nhsuk-checkboxes__conditional nhsuk-checkboxes__conditional--hidden" id="conditional-contact-1"> |
| 34 | + <div class="nhsuk-form-group"> |
| 35 | + <label class="nhsuk-label" for="email"> |
| 36 | + Email address |
| 37 | + </label> |
| 38 | + <input class="nhsuk-input nhsuk-u-width-two-thirds" id="email" name="email" type="text"> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | +
|
| 42 | + <div class="nhsuk-checkboxes__item"> |
| 43 | + <input class="nhsuk-checkboxes__input" id="contact-2" name="contact" type="checkbox" value="phone" aria-controls="conditional-contact-2" aria-expanded="false"> |
| 44 | + <label class="nhsuk-label nhsuk-checkboxes__label" for="contact-2"> |
| 45 | + Phone |
| 46 | + </label> |
| 47 | + </div> |
| 48 | +
|
| 49 | + <div class="nhsuk-checkboxes__conditional nhsuk-checkboxes__conditional--hidden" id="conditional-contact-2"> |
| 50 | + <div class="nhsuk-form-group"> |
| 51 | + <label class="nhsuk-label" for="phone"> |
| 52 | + Phone number |
| 53 | + </label> |
| 54 | + <input class="nhsuk-input nhsuk-u-width-two-thirds" id="phone" name="phone" type="text"> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | +
|
| 58 | + <div class="nhsuk-checkboxes__item"> |
| 59 | + <input class="nhsuk-checkboxes__input" id="contact-3" name="contact" type="checkbox" value="text" aria-controls="conditional-contact-3" aria-expanded="false"> |
| 60 | + <label class="nhsuk-label nhsuk-checkboxes__label" for="contact-3"> |
| 61 | + Text message |
| 62 | + </label> |
| 63 | + </div> |
| 64 | +
|
| 65 | + <div class="nhsuk-checkboxes__conditional nhsuk-checkboxes__conditional--hidden" id="conditional-contact-3"> |
| 66 | + <div class="nhsuk-form-group"> |
| 67 | + <label class="nhsuk-label" for="text"> |
| 68 | + Mobile phone number |
| 69 | + </label> |
| 70 | + <input class="nhsuk-input nhsuk-u-width-two-thirds" id="mobile" name="mobile" type="text"> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + </fieldset> |
| 75 | + </div> |
| 76 | + </form> |
| 77 | + ` |
| 78 | + |
| 79 | + const container = document.querySelector('.nhsuk-checkboxes') |
| 80 | + |
| 81 | + conditionals = [ |
| 82 | + ...container.querySelectorAll('.nhsuk-checkboxes__conditional') |
| 83 | + ] |
| 84 | + |
| 85 | + const input1 = getByRole(container, 'checkbox', { |
| 86 | + name: 'Email' |
| 87 | + }) |
| 88 | + |
| 89 | + const input2 = getByRole(container, 'checkbox', { |
| 90 | + name: 'Phone' |
| 91 | + }) |
| 92 | + |
| 93 | + const input3 = getByRole(container, 'checkbox', { |
| 94 | + name: 'Text message' |
| 95 | + }) |
| 96 | + |
| 97 | + inputs = [input1, input2, input3] |
| 98 | + |
| 99 | + jest.spyOn(input1, 'addEventListener') |
| 100 | + jest.spyOn(input2, 'addEventListener') |
| 101 | + jest.spyOn(input3, 'addEventListener') |
| 102 | + }) |
| 103 | + |
| 104 | + describe('Exports', () => { |
| 105 | + it('should export init function', () => { |
| 106 | + expect(initCheckboxes).toBeInstanceOf(Function) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + describe('Initialisation', () => { |
| 111 | + it('should add event listeners', () => { |
| 112 | + initCheckboxes() |
| 113 | + |
| 114 | + for (const input of inputs) { |
| 115 | + expect(input.addEventListener).toHaveBeenCalledWith( |
| 116 | + 'change', |
| 117 | + expect.any(Function) |
| 118 | + ) |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + it('should not throw with missing checkboxes', () => { |
| 123 | + for (const input of inputs) { |
| 124 | + input.remove() |
| 125 | + } |
| 126 | + |
6 | 127 | expect(() => initCheckboxes()).not.toThrow()
|
7 | 128 | })
|
8 |
| - it('if there are no conditional checkboxes inside the container', () => { |
9 |
| - document.body.innerHTML = '<div class="nhsuk-checkboxes"></div>' |
| 129 | + |
| 130 | + it('should not throw with empty body', () => { |
| 131 | + document.body.innerHTML = '' |
10 | 132 | expect(() => initCheckboxes()).not.toThrow()
|
11 | 133 | })
|
| 134 | + |
| 135 | + it('should not throw with empty scope', () => { |
| 136 | + const scope = document.createElement('div') |
| 137 | + expect(() => initCheckboxes({ scope })).not.toThrow() |
| 138 | + }) |
12 | 139 | })
|
13 | 140 |
|
14 |
| - describe('displays conditional content', () => { |
15 |
| - it('when checking the input', () => { |
16 |
| - document.body.innerHTML = `<form><div class="nhsuk-checkboxes"> |
17 |
| - <input class="nhsuk-checkboxes__input" id="input-1" type="checkbox" aria-controls="conditional-1" aria-expanded="false" /> |
18 |
| - <div class="nhsuk-checkboxes__conditional--hidden" id="conditional-1">Test</div> |
19 |
| - </div></form>` |
20 |
| - const input = document.querySelector('#input-1') |
21 |
| - const conditional = document.querySelector('#conditional-1') |
| 141 | + describe('Conditional content', () => { |
| 142 | + it('should be hidden by default', () => { |
| 143 | + initCheckboxes() |
| 144 | + |
| 145 | + for (const input of inputs) { |
| 146 | + const index = inputs.indexOf(input) |
| 147 | + const conditional = conditionals.at(index) |
| 148 | + |
| 149 | + // Conditional content hidden |
| 150 | + expect(input).toHaveAttribute('aria-expanded', 'false') |
| 151 | + expect(conditional).toHaveClass('nhsuk-checkboxes__conditional--hidden') |
| 152 | + } |
| 153 | + }) |
| 154 | + |
| 155 | + it('should be visible when input is checked', () => { |
22 | 156 | initCheckboxes()
|
23 |
| - input.click() |
24 |
| - expect(conditional).not.toHaveClass( |
25 |
| - 'nhsuk-checkboxes__conditional--hidden' |
26 |
| - ) |
27 |
| - expect(input).toHaveAttribute('aria-expanded', 'true') |
| 157 | + |
| 158 | + for (const input of inputs) { |
| 159 | + const index = inputs.indexOf(input) |
| 160 | + const conditional = conditionals.at(index) |
| 161 | + |
| 162 | + input.click() |
| 163 | + |
| 164 | + // Conditional content visible |
| 165 | + expect(input).toHaveAttribute('aria-expanded', 'true') |
| 166 | + expect(conditional).not.toHaveClass( |
| 167 | + 'nhsuk-checkboxes__conditional--hidden' |
| 168 | + ) |
| 169 | + } |
28 | 170 | })
|
29 |
| - }) |
30 | 171 |
|
31 |
| - describe('hides conditional content', () => { |
32 |
| - it('when unchecking the input', () => { |
33 |
| - document.body.innerHTML = `<form><div class="nhsuk-checkboxes"> |
34 |
| - <input class="nhsuk-checkboxes__input" id="input-1" type="checkbox" aria-controls="conditional-1" aria-expanded="false" /> |
35 |
| - <div class="nhsuk-checkboxes__conditional nhsuk-checkboxes__conditional--hidden" id="conditional-1">Test</div> |
36 |
| - </div></form>` |
37 |
| - const input = document.querySelector('#input-1') |
38 |
| - const conditional = document.querySelector('#conditional-1') |
| 172 | + it('should be hidden when input is unchecked', () => { |
39 | 173 | initCheckboxes()
|
40 |
| - input.click() |
41 |
| - expect(conditional).not.toHaveClass( |
42 |
| - 'nhsuk-checkboxes__conditional--hidden' |
43 |
| - ) |
44 |
| - expect(input).toHaveAttribute('aria-expanded', 'true') |
45 |
| - input.click() |
46 |
| - expect(conditional).toHaveClass('nhsuk-checkboxes__conditional--hidden') |
47 |
| - expect(input).toHaveAttribute('aria-expanded', 'false') |
| 174 | + |
| 175 | + for (const input of inputs) { |
| 176 | + const index = inputs.indexOf(input) |
| 177 | + const conditional = conditionals.at(index) |
| 178 | + |
| 179 | + input.click() |
| 180 | + input.click() |
| 181 | + |
| 182 | + // Conditional content hidden |
| 183 | + expect(input).toHaveAttribute('aria-expanded', 'false') |
| 184 | + expect(conditional).toHaveClass('nhsuk-checkboxes__conditional--hidden') |
| 185 | + } |
48 | 186 | })
|
49 | 187 | })
|
50 | 188 | })
|
0 commit comments