|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +describe('.toHaveAccessibleErrorMessage', () => { |
| 4 | + const input = 'input' |
| 5 | + const errorId = 'error-id' |
| 6 | + const error = 'This field is invalid' |
| 7 | + const strings = {true: String(true), false: String(false)} |
| 8 | + |
| 9 | + describe('Positive Test Cases', () => { |
| 10 | + it("Fails the test if an invalid `id` is provided for the target element's `aria-errormessage`", () => { |
| 11 | + const secondId = 'id2' |
| 12 | + const secondError = 'LISTEN TO ME!!!' |
| 13 | + |
| 14 | + const {queryByTestId} = render(` |
| 15 | + <div> |
| 16 | + <${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId} ${secondId}" /> |
| 17 | + <div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div> |
| 18 | + <div data-testid="${secondId}" id="${secondId}" role="alert">${secondError}</div> |
| 19 | + </div> |
| 20 | + `) |
| 21 | + |
| 22 | + const field = queryByTestId('input') |
| 23 | + expect(() => expect(field).toHaveAccessibleErrorMessage()) |
| 24 | + .toThrowErrorMatchingInlineSnapshot(` |
| 25 | + <dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</> |
| 26 | +
|
| 27 | + Expected element's \`aria-errormessage\` attribute to be empty or a single, valid ID: |
| 28 | +
|
| 29 | + Received: |
| 30 | + <red> aria-errormessage="error-id id2"</> |
| 31 | + `) |
| 32 | + |
| 33 | + // Assume the remaining error messages are the EXACT same as above |
| 34 | + expect(() => |
| 35 | + expect(field).toHaveAccessibleErrorMessage(new RegExp(error[0])), |
| 36 | + ).toThrow() |
| 37 | + |
| 38 | + expect(() => expect(field).toHaveAccessibleErrorMessage(error)).toThrow() |
| 39 | + expect(() => |
| 40 | + expect(field).toHaveAccessibleErrorMessage(secondError), |
| 41 | + ).toThrow() |
| 42 | + |
| 43 | + expect(() => |
| 44 | + expect(field).toHaveAccessibleErrorMessage(new RegExp(secondError[0])), |
| 45 | + ).toThrow() |
| 46 | + }) |
| 47 | + |
| 48 | + it('Fails the test if the target element is valid according to the WAI-ARIA spec', () => { |
| 49 | + const noAriaInvalidAttribute = 'no-aria-invalid-attribute' |
| 50 | + const validFieldState = 'false' |
| 51 | + const invalidFieldStates = [ |
| 52 | + 'true', |
| 53 | + '', |
| 54 | + 'grammar', |
| 55 | + 'spelling', |
| 56 | + 'asfdafbasdfasa', |
| 57 | + ] |
| 58 | + |
| 59 | + function renderFieldWithState(state) { |
| 60 | + return render(` |
| 61 | + <div> |
| 62 | + <${input} data-testid="${input}" aria-invalid="${state}" aria-errormessage="${errorId}" /> |
| 63 | + <div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div> |
| 64 | + |
| 65 | + <input data-testid="${noAriaInvalidAttribute}" aria-errormessage="${errorId}" /> |
| 66 | + </div> |
| 67 | + `) |
| 68 | + } |
| 69 | + |
| 70 | + // Success Cases |
| 71 | + invalidFieldStates.forEach(invalidState => { |
| 72 | + const {queryByTestId} = renderFieldWithState(invalidState) |
| 73 | + const field = queryByTestId('input') |
| 74 | + |
| 75 | + expect(field).toHaveAccessibleErrorMessage() |
| 76 | + expect(field).toHaveAccessibleErrorMessage(error) |
| 77 | + }) |
| 78 | + |
| 79 | + // Failure Case |
| 80 | + const {queryByTestId} = renderFieldWithState(validFieldState) |
| 81 | + const field = queryByTestId('input') |
| 82 | + const fieldWithoutAttribute = queryByTestId(noAriaInvalidAttribute) |
| 83 | + |
| 84 | + expect(() => expect(fieldWithoutAttribute).toHaveAccessibleErrorMessage()) |
| 85 | + .toThrowErrorMatchingInlineSnapshot(` |
| 86 | + <dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</> |
| 87 | +
|
| 88 | + Expected element to be marked as invalid with attribute: |
| 89 | + <green> aria-invalid="true"</> |
| 90 | + Received: |
| 91 | + <red> null</> |
| 92 | + `) |
| 93 | + |
| 94 | + expect(() => expect(field).toHaveAccessibleErrorMessage()) |
| 95 | + .toThrowErrorMatchingInlineSnapshot(` |
| 96 | + <dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</> |
| 97 | +
|
| 98 | + Expected element to be marked as invalid with attribute: |
| 99 | + <green> aria-invalid="true"</> |
| 100 | + Received: |
| 101 | + <red> aria-invalid="false</> |
| 102 | + `) |
| 103 | + |
| 104 | + // Assume the remaining error messages are the EXACT same as above |
| 105 | + expect(() => expect(field).toHaveAccessibleErrorMessage(error)).toThrow() |
| 106 | + expect(() => |
| 107 | + expect(field).toHaveAccessibleErrorMessage(new RegExp(error, 'i')), |
| 108 | + ).toThrow() |
| 109 | + }) |
| 110 | + |
| 111 | + it('Passes the test if the target element has ANY recognized, non-empty error message', () => { |
| 112 | + const {queryByTestId} = render(` |
| 113 | + <div> |
| 114 | + <${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" /> |
| 115 | + <div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div> |
| 116 | + </div> |
| 117 | + `) |
| 118 | + |
| 119 | + const field = queryByTestId(input) |
| 120 | + expect(field).toHaveAccessibleErrorMessage() |
| 121 | + }) |
| 122 | + |
| 123 | + it('Fails the test if NO recognized, non-empty error message was found for the target element', () => { |
| 124 | + const empty = 'empty' |
| 125 | + const emptyErrorId = 'empty-error' |
| 126 | + const missing = 'missing' |
| 127 | + |
| 128 | + const {queryByTestId} = render(` |
| 129 | + <div> |
| 130 | + <input data-testid="${empty}" aria-invalid="${strings.true}" aria-errormessage="${emptyErrorId}" /> |
| 131 | + <div data-testid="${emptyErrorId}" id="${emptyErrorId}" role="alert"></div> |
| 132 | +
|
| 133 | + <input data-testid="${missing}" aria-invalid="${strings.true}" aria-errormessage="${missing}-error" /> |
| 134 | + </div> |
| 135 | + `) |
| 136 | + |
| 137 | + const fieldWithEmptyError = queryByTestId(empty) |
| 138 | + const fieldMissingError = queryByTestId(missing) |
| 139 | + |
| 140 | + expect(() => expect(fieldWithEmptyError).toHaveAccessibleErrorMessage()) |
| 141 | + .toThrowErrorMatchingInlineSnapshot(` |
| 142 | + <dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</> |
| 143 | +
|
| 144 | + Expected element to have accessible error message: |
| 145 | +
|
| 146 | + Received: |
| 147 | +
|
| 148 | + `) |
| 149 | + |
| 150 | + expect(() => expect(fieldMissingError).toHaveAccessibleErrorMessage()) |
| 151 | + .toThrowErrorMatchingInlineSnapshot(` |
| 152 | + <dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</> |
| 153 | +
|
| 154 | + Expected element to have accessible error message: |
| 155 | +
|
| 156 | + Received: |
| 157 | +
|
| 158 | + `) |
| 159 | + }) |
| 160 | + |
| 161 | + it('Passes the test if the target element has the error message that was SPECIFIED', () => { |
| 162 | + const {queryByTestId} = render(` |
| 163 | + <div> |
| 164 | + <${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" /> |
| 165 | + <div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div> |
| 166 | + </div> |
| 167 | + `) |
| 168 | + |
| 169 | + const field = queryByTestId(input) |
| 170 | + const halfOfError = error.slice(0, Math.floor(error.length * 0.5)) |
| 171 | + |
| 172 | + expect(field).toHaveAccessibleErrorMessage(error) |
| 173 | + expect(field).toHaveAccessibleErrorMessage(new RegExp(halfOfError), 'i') |
| 174 | + expect(field).toHaveAccessibleErrorMessage( |
| 175 | + expect.stringContaining(halfOfError), |
| 176 | + ) |
| 177 | + expect(field).toHaveAccessibleErrorMessage( |
| 178 | + expect.stringMatching(new RegExp(halfOfError), 'i'), |
| 179 | + ) |
| 180 | + }) |
| 181 | + |
| 182 | + it('Fails the test if the target element DOES NOT have the error message that was SPECIFIED', () => { |
| 183 | + const {queryByTestId} = render(` |
| 184 | + <div> |
| 185 | + <${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" /> |
| 186 | + <div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div> |
| 187 | + </div> |
| 188 | + `) |
| 189 | + |
| 190 | + const field = queryByTestId(input) |
| 191 | + const msg = 'asdflkje2984fguyvb bnafdsasfa;lj' |
| 192 | + |
| 193 | + expect(() => expect(field).toHaveAccessibleErrorMessage('')) |
| 194 | + .toThrowErrorMatchingInlineSnapshot(` |
| 195 | + <dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</> |
| 196 | +
|
| 197 | + Expected element to have accessible error message: |
| 198 | +
|
| 199 | + Received: |
| 200 | + <red> This field is invalid</> |
| 201 | + `) |
| 202 | + |
| 203 | + // Assume this error is SIMILAR to the error above |
| 204 | + expect(() => expect(field).toHaveAccessibleErrorMessage(msg)).toThrow() |
| 205 | + expect(() => |
| 206 | + expect(field).toHaveAccessibleErrorMessage( |
| 207 | + error.slice(0, Math.floor(error.length * 0.5)), |
| 208 | + ), |
| 209 | + ).toThrow() |
| 210 | + |
| 211 | + expect(() => |
| 212 | + expect(field).toHaveAccessibleErrorMessage(new RegExp(msg), 'i'), |
| 213 | + ).toThrowErrorMatchingInlineSnapshot(` |
| 214 | + <dim>expect(</><red>element</><dim>).toHaveAccessibleErrorMessage(</><green>expected</><dim>)</> |
| 215 | +
|
| 216 | + Expected element to have accessible error message: |
| 217 | + <green> /asdflkje2984fguyvb bnafdsasfa;lj/</> |
| 218 | + Received: |
| 219 | + <red> This field is invalid</> |
| 220 | + `) |
| 221 | + }) |
| 222 | + |
| 223 | + it('Normalizes the whitespace of the received error message', () => { |
| 224 | + const {queryByTestId} = render(` |
| 225 | + <div> |
| 226 | + <${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId}" /> |
| 227 | + <div data-testid="${errorId}" id="${errorId}" role="alert"> |
| 228 | + Step |
| 229 | + 1 |
| 230 | + of |
| 231 | + 9000 |
| 232 | + </div> |
| 233 | + </div> |
| 234 | + `) |
| 235 | + |
| 236 | + const field = queryByTestId(input) |
| 237 | + expect(field).toHaveAccessibleErrorMessage('Step 1 of 9000') |
| 238 | + }) |
| 239 | + }) |
| 240 | + |
| 241 | + // These tests for the `.not` use cases will help us cover our bases and complete test coverage |
| 242 | + describe('Negated Test Cases', () => { |
| 243 | + it("Passes the test if an invalid `id` is provided for the target element's `aria-errormessage`", () => { |
| 244 | + const secondId = 'id2' |
| 245 | + const secondError = 'LISTEN TO ME!!!' |
| 246 | + |
| 247 | + const {queryByTestId} = render(` |
| 248 | + <div> |
| 249 | + <${input} data-testid="${input}" aria-invalid="${strings.true}" aria-errormessage="${errorId} ${secondId}" /> |
| 250 | + <div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div> |
| 251 | + <div data-testid="${secondId}" id="${secondId}" role="alert">${secondError}</div> |
| 252 | + </div> |
| 253 | + `) |
| 254 | + |
| 255 | + const field = queryByTestId('input') |
| 256 | + expect(field).not.toHaveAccessibleErrorMessage() |
| 257 | + expect(field).not.toHaveAccessibleErrorMessage(error) |
| 258 | + expect(field).not.toHaveAccessibleErrorMessage(new RegExp(error[0])) |
| 259 | + expect(field).not.toHaveAccessibleErrorMessage(secondError) |
| 260 | + expect(field).not.toHaveAccessibleErrorMessage(new RegExp(secondError[0])) |
| 261 | + }) |
| 262 | + |
| 263 | + it('Passes the test if the target element is valid according to the WAI-ARIA spec', () => { |
| 264 | + const {queryByTestId} = render(` |
| 265 | + <div> |
| 266 | + <${input} data-testid="${input}" aria-errormessage="${errorId}" /> |
| 267 | + <div data-testid="${errorId}" id="${errorId}" role="alert">${error}</div> |
| 268 | + </div> |
| 269 | + `) |
| 270 | + |
| 271 | + const field = queryByTestId(input) |
| 272 | + expect(field).not.toHaveAccessibleErrorMessage() |
| 273 | + expect(field).not.toHaveAccessibleErrorMessage(error) |
| 274 | + expect(field).not.toHaveAccessibleErrorMessage(new RegExp(error[0])) |
| 275 | + }) |
| 276 | + }) |
| 277 | +}) |
0 commit comments