|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +const { RuleTester } = require('eslint'); |
| 11 | +const rule = require('./require_include_in_check_a11y'); |
| 12 | +const dedent = require('dedent'); |
| 13 | + |
| 14 | +const ruleTester = new RuleTester({ |
| 15 | + parser: require.resolve('@typescript-eslint/parser'), |
| 16 | + parserOptions: { |
| 17 | + sourceType: 'module', |
| 18 | + ecmaVersion: 2018, |
| 19 | + }, |
| 20 | +}); |
| 21 | + |
| 22 | +const MESSAGE = |
| 23 | + 'We recommend running checkA11y with the include parameter set to the root element you are testing. This makes the tests more isolated and reduces the time required to analyze the DOM structure.'; |
| 24 | + |
| 25 | +ruleTester.run('@kbn/eslint/require_include_in_check_a11y', rule, { |
| 26 | + valid: [ |
| 27 | + { |
| 28 | + code: dedent` |
| 29 | + page.checkA11y({ include: '#root' }); |
| 30 | + `, |
| 31 | + }, |
| 32 | + { |
| 33 | + code: dedent` |
| 34 | + page.checkA11y({ include: rootEl }); |
| 35 | + `, |
| 36 | + }, |
| 37 | + { |
| 38 | + code: dedent` |
| 39 | + something.checkA11y({ include: '#app', exclude: ['#legacy'] }); |
| 40 | + `, |
| 41 | + }, |
| 42 | + { |
| 43 | + code: dedent` |
| 44 | + page['checkA11y']({ include: '#root' }); |
| 45 | + `, |
| 46 | + }, |
| 47 | + { |
| 48 | + code: dedent` |
| 49 | + page.checkA11y({ include: '#root', other: true }); |
| 50 | + `, |
| 51 | + }, |
| 52 | + { |
| 53 | + code: dedent` |
| 54 | + page.checkA11y({ ['include']: '#root' }); |
| 55 | + `, |
| 56 | + }, |
| 57 | + ], |
| 58 | + |
| 59 | + invalid: [ |
| 60 | + { |
| 61 | + code: dedent` |
| 62 | + page.checkA11y(); |
| 63 | + `, |
| 64 | + errors: [{ line: 1, message: MESSAGE }], |
| 65 | + }, |
| 66 | + { |
| 67 | + code: dedent` |
| 68 | + page.checkA11y({}); |
| 69 | + `, |
| 70 | + errors: [{ line: 1, message: MESSAGE }], |
| 71 | + }, |
| 72 | + { |
| 73 | + code: dedent` |
| 74 | + page.checkA11y({ foo: 1 }); |
| 75 | + `, |
| 76 | + errors: [{ line: 1, message: MESSAGE }], |
| 77 | + }, |
| 78 | + { |
| 79 | + code: dedent` |
| 80 | + page.checkA11y({ 'include ': '#root' }); |
| 81 | + `, |
| 82 | + errors: [{ line: 1, message: MESSAGE }], |
| 83 | + }, |
| 84 | + { |
| 85 | + code: dedent` |
| 86 | + page.checkA11y(config); |
| 87 | + `, |
| 88 | + errors: [{ line: 1, message: MESSAGE }], |
| 89 | + }, |
| 90 | + { |
| 91 | + code: dedent` |
| 92 | + page['checkA11y'](); |
| 93 | + `, |
| 94 | + errors: [{ line: 1, message: MESSAGE }], |
| 95 | + }, |
| 96 | + { |
| 97 | + // include only in second arg (rule checks first arg) |
| 98 | + code: dedent` |
| 99 | + page.checkA11y({}, { include: '#root' }); |
| 100 | + `, |
| 101 | + errors: [{ line: 1, message: MESSAGE }], |
| 102 | + }, |
| 103 | + { |
| 104 | + // Non-object first argument |
| 105 | + code: dedent` |
| 106 | + page.checkA11y('not an object'); |
| 107 | + `, |
| 108 | + errors: [{ line: 1, message: MESSAGE }], |
| 109 | + }, |
| 110 | + ], |
| 111 | +}); |
0 commit comments