|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// ------------------------------------------------------------------------------ |
| 4 | +// Requirements |
| 5 | +// ------------------------------------------------------------------------------ |
| 6 | + |
| 7 | +const rule = require('../../../lib/rules/data-testid'); |
| 8 | +const RuleTester = require('eslint').RuleTester; |
| 9 | + |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | +// Tests |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +const parserOptions = { |
| 15 | + ecmaVersion: 2018, |
| 16 | + sourceType: 'module', |
| 17 | + ecmaFeatures: { |
| 18 | + jsx: true, |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +const ruleTester = new RuleTester({ parserOptions }); |
| 23 | +ruleTester.run('data-testid', rule, { |
| 24 | + valid: [ |
| 25 | + { |
| 26 | + code: ` |
| 27 | + import React from 'react'; |
| 28 | + |
| 29 | + const TestComponent = props => { |
| 30 | + return ( |
| 31 | + <div data-testid="cool"> |
| 32 | + Hello |
| 33 | + </div> |
| 34 | + ) |
| 35 | + }; |
| 36 | + `, |
| 37 | + options: [], |
| 38 | + }, |
| 39 | + { |
| 40 | + code: ` |
| 41 | + import React from 'react'; |
| 42 | + |
| 43 | + const TestComponent = props => { |
| 44 | + return ( |
| 45 | + <div data-testid="cool"> |
| 46 | + Hello |
| 47 | + </div> |
| 48 | + ) |
| 49 | + }; |
| 50 | + `, |
| 51 | + options: [{ testIdPattern: 'cool' }], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: ` |
| 55 | + import React from 'react'; |
| 56 | + |
| 57 | + const TestComponent = props => { |
| 58 | + return ( |
| 59 | + <div className="cool"> |
| 60 | + Hello |
| 61 | + </div> |
| 62 | + ) |
| 63 | + }; |
| 64 | + `, |
| 65 | + options: [{ testIdPattern: 'cool' }], |
| 66 | + }, |
| 67 | + { |
| 68 | + code: ` |
| 69 | + import React from 'react'; |
| 70 | + |
| 71 | + const TestComponent = props => { |
| 72 | + return ( |
| 73 | + <div data-testid="Awesome__CoolStuff"> |
| 74 | + Hello |
| 75 | + </div> |
| 76 | + ) |
| 77 | + }; |
| 78 | + `, |
| 79 | + options: [ |
| 80 | + { |
| 81 | + testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', |
| 82 | + }, |
| 83 | + ], |
| 84 | + filename: '/my/cool/file/path/Awesome.js', |
| 85 | + }, |
| 86 | + { |
| 87 | + code: ` |
| 88 | + import React from 'react'; |
| 89 | + |
| 90 | + const TestComponent = props => { |
| 91 | + return ( |
| 92 | + <div data-testid="Awesome"> |
| 93 | + Hello |
| 94 | + </div> |
| 95 | + ) |
| 96 | + }; |
| 97 | + `, |
| 98 | + options: [ |
| 99 | + { |
| 100 | + testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', |
| 101 | + }, |
| 102 | + ], |
| 103 | + filename: '/my/cool/file/path/Awesome.js', |
| 104 | + }, |
| 105 | + { |
| 106 | + code: ` |
| 107 | + import React from 'react'; |
| 108 | + |
| 109 | + const TestComponent = props => { |
| 110 | + return ( |
| 111 | + <div data-testid="Parent"> |
| 112 | + Hello |
| 113 | + </div> |
| 114 | + ) |
| 115 | + }; |
| 116 | + `, |
| 117 | + options: [ |
| 118 | + { |
| 119 | + testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', |
| 120 | + }, |
| 121 | + ], |
| 122 | + filename: '/my/cool/file/Parent/index.js', |
| 123 | + }, |
| 124 | + { |
| 125 | + code: ` |
| 126 | + import React from 'react'; |
| 127 | + |
| 128 | + const TestComponent = props => { |
| 129 | + return ( |
| 130 | + <div data-testid="Parent"> |
| 131 | + Hello |
| 132 | + </div> |
| 133 | + ) |
| 134 | + }; |
| 135 | + `, |
| 136 | + options: [ |
| 137 | + { |
| 138 | + testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', |
| 139 | + excludePaths: ['__tests__'], |
| 140 | + }, |
| 141 | + ], |
| 142 | + filename: '/my/cool/__tests__/Parent/index.js', |
| 143 | + }, |
| 144 | + ], |
| 145 | + invalid: [ |
| 146 | + { |
| 147 | + code: ` |
| 148 | + import React from 'react'; |
| 149 | + |
| 150 | + const TestComponent = props => { |
| 151 | + return ( |
| 152 | + <div data-testid="Awesome__CoolStuff"> |
| 153 | + Hello |
| 154 | + </div> |
| 155 | + ) |
| 156 | + }; |
| 157 | + `, |
| 158 | + options: [{ testIdPattern: 'error' }], |
| 159 | + errors: [ |
| 160 | + { |
| 161 | + message: '`data-testid` "Awesome__CoolStuff" should match `/error/`', |
| 162 | + }, |
| 163 | + ], |
| 164 | + }, |
| 165 | + { |
| 166 | + code: ` |
| 167 | + import React from 'react'; |
| 168 | + |
| 169 | + const TestComponent = props => { |
| 170 | + return ( |
| 171 | + <div data-testid="Nope"> |
| 172 | + Hello |
| 173 | + </div> |
| 174 | + ) |
| 175 | + }; |
| 176 | + `, |
| 177 | + options: [ |
| 178 | + { |
| 179 | + testIdPattern: 'matchMe', |
| 180 | + excludePaths: ['__mocks__'], |
| 181 | + }, |
| 182 | + ], |
| 183 | + filename: '/my/cool/__tests__/Parent/index.js', |
| 184 | + errors: [ |
| 185 | + { |
| 186 | + message: '`data-testid` "Nope" should match `/matchMe/`', |
| 187 | + }, |
| 188 | + ], |
| 189 | + }, |
| 190 | + { |
| 191 | + code: ` |
| 192 | + import React from 'react'; |
| 193 | + |
| 194 | + const TestComponent = props => { |
| 195 | + return ( |
| 196 | + <div data-testid="WrongComponent__cool"> |
| 197 | + Hello |
| 198 | + </div> |
| 199 | + ) |
| 200 | + }; |
| 201 | + `, |
| 202 | + options: [ |
| 203 | + { |
| 204 | + testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$', |
| 205 | + excludePaths: ['__mocks__'], |
| 206 | + }, |
| 207 | + ], |
| 208 | + filename: '/my/cool/__tests__/Parent/index.js', |
| 209 | + errors: [ |
| 210 | + { |
| 211 | + message: |
| 212 | + '`data-testid` "WrongComponent__cool" should match `/^Parent(__([A-Z]+[a-z]*?)+)*$/`', |
| 213 | + }, |
| 214 | + ], |
| 215 | + }, |
| 216 | + ], |
| 217 | +}); |
0 commit comments