| 1 | /* eslint-env mocha */
|
|---|
| 2 | import assert from 'assert';
|
|---|
| 3 | import { getOpeningElement, setParserName } from '../helper';
|
|---|
| 4 | import hasProp, { hasAnyProp, hasEveryProp } from '../../src/hasProp';
|
|---|
| 5 |
|
|---|
| 6 | describe('hasProp', () => {
|
|---|
| 7 | beforeEach(() => {
|
|---|
| 8 | setParserName('babel');
|
|---|
| 9 | });
|
|---|
| 10 | it('should export a function', () => {
|
|---|
| 11 | const expected = 'function';
|
|---|
| 12 | const actual = typeof hasProp;
|
|---|
| 13 |
|
|---|
| 14 | assert.equal(actual, expected);
|
|---|
| 15 | });
|
|---|
| 16 |
|
|---|
| 17 | it('should return false if no arguments are provided', () => {
|
|---|
| 18 | const expected = false;
|
|---|
| 19 | const actual = hasProp();
|
|---|
| 20 |
|
|---|
| 21 | assert.equal(actual, expected);
|
|---|
| 22 | });
|
|---|
| 23 |
|
|---|
| 24 | it('should return false if the prop is absent', () => {
|
|---|
| 25 | const code = '<div />';
|
|---|
| 26 | const node = getOpeningElement(code);
|
|---|
| 27 | const { attributes: props } = node;
|
|---|
| 28 | const prop = 'id';
|
|---|
| 29 |
|
|---|
| 30 | const expected = false;
|
|---|
| 31 | const actual = hasProp(props, prop);
|
|---|
| 32 |
|
|---|
| 33 | assert.equal(actual, expected);
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | it('should return true if the prop exists', () => {
|
|---|
| 37 | const code = '<div id="foo" />';
|
|---|
| 38 | const node = getOpeningElement(code);
|
|---|
| 39 | const { attributes: props } = node;
|
|---|
| 40 | const prop = 'id';
|
|---|
| 41 |
|
|---|
| 42 | const expected = true;
|
|---|
| 43 | const actual = hasProp(props, prop);
|
|---|
| 44 |
|
|---|
| 45 | assert.equal(actual, expected);
|
|---|
| 46 | });
|
|---|
| 47 |
|
|---|
| 48 | it('should return true if the prop may exist in spread loose mode', () => {
|
|---|
| 49 | const code = '<div {...props} />';
|
|---|
| 50 | const node = getOpeningElement(code);
|
|---|
| 51 | const { attributes: props } = node;
|
|---|
| 52 | const prop = 'id';
|
|---|
| 53 | const options = {
|
|---|
| 54 | spreadStrict: false,
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | const expected = true;
|
|---|
| 58 | const actual = hasProp(props, prop, options);
|
|---|
| 59 |
|
|---|
| 60 | assert.equal(actual, expected);
|
|---|
| 61 | });
|
|---|
| 62 |
|
|---|
| 63 | it('should return false if the prop is considered absent in case-sensitive mode', () => {
|
|---|
| 64 | const code = '<div ID="foo" />';
|
|---|
| 65 | const node = getOpeningElement(code);
|
|---|
| 66 | const { attributes: props } = node;
|
|---|
| 67 | const prop = 'id';
|
|---|
| 68 | const options = {
|
|---|
| 69 | ignoreCase: false,
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | const expected = false;
|
|---|
| 73 | const actual = hasProp(props, prop, options);
|
|---|
| 74 |
|
|---|
| 75 | assert.equal(actual, expected);
|
|---|
| 76 | });
|
|---|
| 77 | });
|
|---|
| 78 |
|
|---|
| 79 | describe('hasAnyProp tests', () => {
|
|---|
| 80 | it('should export a function', () => {
|
|---|
| 81 | const expected = 'function';
|
|---|
| 82 | const actual = typeof hasAnyProp;
|
|---|
| 83 |
|
|---|
| 84 | assert.equal(actual, expected);
|
|---|
| 85 | });
|
|---|
| 86 |
|
|---|
| 87 | it('should return false if no arguments are provided', () => {
|
|---|
| 88 | const expected = false;
|
|---|
| 89 | const actual = hasAnyProp();
|
|---|
| 90 |
|
|---|
| 91 | assert.equal(actual, expected);
|
|---|
| 92 | });
|
|---|
| 93 |
|
|---|
| 94 | it('should return false if the prop is absent', () => {
|
|---|
| 95 | const code = '<div />';
|
|---|
| 96 | const node = getOpeningElement(code);
|
|---|
| 97 | const { attributes: props } = node;
|
|---|
| 98 | const prop = 'id';
|
|---|
| 99 |
|
|---|
| 100 | const expected = false;
|
|---|
| 101 | const actual = hasAnyProp(props, prop);
|
|---|
| 102 |
|
|---|
| 103 | assert.equal(actual, expected);
|
|---|
| 104 | });
|
|---|
| 105 |
|
|---|
| 106 | it('should return false if all props are absent in array', () => {
|
|---|
| 107 | const code = '<div />';
|
|---|
| 108 | const node = getOpeningElement(code);
|
|---|
| 109 | const { attributes: props } = node;
|
|---|
| 110 | const propsToCheck = ['id', 'className'];
|
|---|
| 111 |
|
|---|
| 112 | const expected = false;
|
|---|
| 113 | const actual = hasAnyProp(props, propsToCheck);
|
|---|
| 114 |
|
|---|
| 115 | assert.equal(actual, expected);
|
|---|
| 116 | });
|
|---|
| 117 |
|
|---|
| 118 | it('should return false if all props are absent in space delimited string', () => {
|
|---|
| 119 | const code = '<div />';
|
|---|
| 120 | const node = getOpeningElement(code);
|
|---|
| 121 | const { attributes: props } = node;
|
|---|
| 122 | const propsToCheck = 'id className';
|
|---|
| 123 |
|
|---|
| 124 | const expected = false;
|
|---|
| 125 | const actual = hasAnyProp(props, propsToCheck);
|
|---|
| 126 |
|
|---|
| 127 | assert.equal(actual, expected);
|
|---|
| 128 | });
|
|---|
| 129 |
|
|---|
| 130 | it('should return true if the prop exists', () => {
|
|---|
| 131 | const code = '<div id="foo" />';
|
|---|
| 132 | const node = getOpeningElement(code);
|
|---|
| 133 | const { attributes: props } = node;
|
|---|
| 134 | const prop = 'id';
|
|---|
| 135 |
|
|---|
| 136 | const expected = true;
|
|---|
| 137 | const actual = hasAnyProp(props, prop);
|
|---|
| 138 |
|
|---|
| 139 | assert.equal(actual, expected);
|
|---|
| 140 | });
|
|---|
| 141 |
|
|---|
| 142 | it('should return true if any prop exists in array', () => {
|
|---|
| 143 | const code = '<div id="foo" />';
|
|---|
| 144 | const node = getOpeningElement(code);
|
|---|
| 145 | const { attributes: props } = node;
|
|---|
| 146 | const prop = ['className', 'id'];
|
|---|
| 147 |
|
|---|
| 148 | const expected = true;
|
|---|
| 149 | const actual = hasAnyProp(props, prop);
|
|---|
| 150 |
|
|---|
| 151 | assert.equal(actual, expected);
|
|---|
| 152 | });
|
|---|
| 153 |
|
|---|
| 154 | it('should return true if any prop exists in space delimited string', () => {
|
|---|
| 155 | const code = '<div id="foo" />';
|
|---|
| 156 | const node = getOpeningElement(code);
|
|---|
| 157 | const { attributes: props } = node;
|
|---|
| 158 | const prop = 'className id';
|
|---|
| 159 |
|
|---|
| 160 | const expected = true;
|
|---|
| 161 | const actual = hasAnyProp(props, prop);
|
|---|
| 162 |
|
|---|
| 163 | assert.equal(actual, expected);
|
|---|
| 164 | });
|
|---|
| 165 |
|
|---|
| 166 | it('should return true if the prop may exist in spread loose mode', () => {
|
|---|
| 167 | const code = '<div {...props} />';
|
|---|
| 168 | const node = getOpeningElement(code);
|
|---|
| 169 | const { attributes: props } = node;
|
|---|
| 170 | const prop = 'id';
|
|---|
| 171 | const options = {
|
|---|
| 172 | spreadStrict: false,
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | const expected = true;
|
|---|
| 176 | const actual = hasAnyProp(props, prop, options);
|
|---|
| 177 |
|
|---|
| 178 | assert.equal(actual, expected);
|
|---|
| 179 | });
|
|---|
| 180 |
|
|---|
| 181 | it('should return true if any prop may exist in spread loose mode', () => {
|
|---|
| 182 | const code = '<div {...props} />';
|
|---|
| 183 | const node = getOpeningElement(code);
|
|---|
| 184 | const { attributes: props } = node;
|
|---|
| 185 | const prop = ['id', 'className'];
|
|---|
| 186 | const options = {
|
|---|
| 187 | spreadStrict: false,
|
|---|
| 188 | };
|
|---|
| 189 |
|
|---|
| 190 | const expected = true;
|
|---|
| 191 | const actual = hasAnyProp(props, prop, options);
|
|---|
| 192 |
|
|---|
| 193 | assert.equal(actual, expected);
|
|---|
| 194 | });
|
|---|
| 195 |
|
|---|
| 196 | it('should return false if the prop is considered absent in case-sensitive mode', () => {
|
|---|
| 197 | const code = '<div ID="foo" />';
|
|---|
| 198 | const node = getOpeningElement(code);
|
|---|
| 199 | const { attributes: props } = node;
|
|---|
| 200 | const prop = 'id';
|
|---|
| 201 | const options = {
|
|---|
| 202 | ignoreCase: false,
|
|---|
| 203 | };
|
|---|
| 204 |
|
|---|
| 205 | const expected = false;
|
|---|
| 206 | const actual = hasAnyProp(props, prop, options);
|
|---|
| 207 |
|
|---|
| 208 | assert.equal(actual, expected);
|
|---|
| 209 | });
|
|---|
| 210 |
|
|---|
| 211 | it('should return false if all props are considered absent in case-sensitive mode', () => {
|
|---|
| 212 | const code = '<div ID="foo" />';
|
|---|
| 213 | const node = getOpeningElement(code);
|
|---|
| 214 | const { attributes: props } = node;
|
|---|
| 215 | const prop = ['id', 'iD', 'className'];
|
|---|
| 216 | const options = {
|
|---|
| 217 | ignoreCase: false,
|
|---|
| 218 | };
|
|---|
| 219 |
|
|---|
| 220 | const expected = false;
|
|---|
| 221 | const actual = hasAnyProp(props, prop, options);
|
|---|
| 222 |
|
|---|
| 223 | assert.equal(actual, expected);
|
|---|
| 224 | });
|
|---|
| 225 | });
|
|---|
| 226 |
|
|---|
| 227 | describe('hasEveryProp tests', () => {
|
|---|
| 228 | it('should export a function', () => {
|
|---|
| 229 | const expected = 'function';
|
|---|
| 230 | const actual = typeof hasEveryProp;
|
|---|
| 231 |
|
|---|
| 232 | assert.equal(actual, expected);
|
|---|
| 233 | });
|
|---|
| 234 |
|
|---|
| 235 | it('should return true if no arguments are provided', () => {
|
|---|
| 236 | const expected = true;
|
|---|
| 237 | const actual = hasEveryProp();
|
|---|
| 238 |
|
|---|
| 239 | assert.equal(actual, expected);
|
|---|
| 240 | });
|
|---|
| 241 |
|
|---|
| 242 | it('should return false if the prop is absent', () => {
|
|---|
| 243 | const code = '<div />';
|
|---|
| 244 | const node = getOpeningElement(code);
|
|---|
| 245 | const { attributes: props } = node;
|
|---|
| 246 | const prop = 'id';
|
|---|
| 247 |
|
|---|
| 248 | const expected = false;
|
|---|
| 249 | const actual = hasEveryProp(props, prop);
|
|---|
| 250 |
|
|---|
| 251 | assert.equal(actual, expected);
|
|---|
| 252 | });
|
|---|
| 253 |
|
|---|
| 254 | it('should return false if any props are absent in array', () => {
|
|---|
| 255 | const code = '<div id="foo" />';
|
|---|
| 256 | const node = getOpeningElement(code);
|
|---|
| 257 | const { attributes: props } = node;
|
|---|
| 258 | const propsToCheck = ['id', 'className'];
|
|---|
| 259 |
|
|---|
| 260 | const expected = false;
|
|---|
| 261 | const actual = hasEveryProp(props, propsToCheck);
|
|---|
| 262 |
|
|---|
| 263 | assert.equal(actual, expected);
|
|---|
| 264 | });
|
|---|
| 265 |
|
|---|
| 266 | it('should return false if all props are absent in array', () => {
|
|---|
| 267 | const code = '<div />';
|
|---|
| 268 | const node = getOpeningElement(code);
|
|---|
| 269 | const { attributes: props } = node;
|
|---|
| 270 | const propsToCheck = ['id', 'className'];
|
|---|
| 271 |
|
|---|
| 272 | const expected = false;
|
|---|
| 273 | const actual = hasEveryProp(props, propsToCheck);
|
|---|
| 274 |
|
|---|
| 275 | assert.equal(actual, expected);
|
|---|
| 276 | });
|
|---|
| 277 |
|
|---|
| 278 | it('should return false if any props are absent in space delimited string', () => {
|
|---|
| 279 | const code = '<div id="foo" />';
|
|---|
| 280 | const node = getOpeningElement(code);
|
|---|
| 281 | const { attributes: props } = node;
|
|---|
| 282 | const propsToCheck = 'id className';
|
|---|
| 283 |
|
|---|
| 284 | const expected = false;
|
|---|
| 285 | const actual = hasEveryProp(props, propsToCheck);
|
|---|
| 286 |
|
|---|
| 287 | assert.equal(actual, expected);
|
|---|
| 288 | });
|
|---|
| 289 |
|
|---|
| 290 | it('should return false if all props are absent in space delimited string', () => {
|
|---|
| 291 | const code = '<div />';
|
|---|
| 292 | const node = getOpeningElement(code);
|
|---|
| 293 | const { attributes: props } = node;
|
|---|
| 294 | const propsToCheck = 'id className';
|
|---|
| 295 |
|
|---|
| 296 | const expected = false;
|
|---|
| 297 | const actual = hasEveryProp(props, propsToCheck);
|
|---|
| 298 |
|
|---|
| 299 | assert.equal(actual, expected);
|
|---|
| 300 | });
|
|---|
| 301 |
|
|---|
| 302 | it('should return true if the prop exists', () => {
|
|---|
| 303 | const code = '<div id="foo" />';
|
|---|
| 304 | const node = getOpeningElement(code);
|
|---|
| 305 | const { attributes: props } = node;
|
|---|
| 306 | const prop = 'id';
|
|---|
| 307 |
|
|---|
| 308 | const expected = true;
|
|---|
| 309 | const actual = hasEveryProp(props, prop);
|
|---|
| 310 |
|
|---|
| 311 | assert.equal(actual, expected);
|
|---|
| 312 | });
|
|---|
| 313 |
|
|---|
| 314 | it('should return true if all props exist in array', () => {
|
|---|
| 315 | const code = '<div id="foo" className="box" />';
|
|---|
| 316 | const node = getOpeningElement(code);
|
|---|
| 317 | const { attributes: props } = node;
|
|---|
| 318 | const prop = ['className', 'id'];
|
|---|
| 319 |
|
|---|
| 320 | const expected = true;
|
|---|
| 321 | const actual = hasEveryProp(props, prop);
|
|---|
| 322 |
|
|---|
| 323 | assert.equal(actual, expected);
|
|---|
| 324 | });
|
|---|
| 325 |
|
|---|
| 326 | it('should return true if all props exist in space delimited string', () => {
|
|---|
| 327 | const code = '<div id="foo" className="box" />';
|
|---|
| 328 | const node = getOpeningElement(code);
|
|---|
| 329 | const { attributes: props } = node;
|
|---|
| 330 | const prop = 'className id';
|
|---|
| 331 |
|
|---|
| 332 | const expected = true;
|
|---|
| 333 | const actual = hasEveryProp(props, prop);
|
|---|
| 334 |
|
|---|
| 335 | assert.equal(actual, expected);
|
|---|
| 336 | });
|
|---|
| 337 |
|
|---|
| 338 | it('should return true if the props may exist in spread loose mode', () => {
|
|---|
| 339 | const code = '<div {...props} />';
|
|---|
| 340 | const node = getOpeningElement(code);
|
|---|
| 341 | const { attributes: props } = node;
|
|---|
| 342 | const prop = 'id';
|
|---|
| 343 | const options = {
|
|---|
| 344 | spreadStrict: false,
|
|---|
| 345 | };
|
|---|
| 346 |
|
|---|
| 347 | const expected = true;
|
|---|
| 348 | const actual = hasEveryProp(props, prop, options);
|
|---|
| 349 |
|
|---|
| 350 | assert.equal(actual, expected);
|
|---|
| 351 | });
|
|---|
| 352 |
|
|---|
| 353 | it('should return true if all props may exist in spread loose mode', () => {
|
|---|
| 354 | const code = '<div {...props} />';
|
|---|
| 355 | const node = getOpeningElement(code);
|
|---|
| 356 | const { attributes: props } = node;
|
|---|
| 357 | const prop = ['id', 'className'];
|
|---|
| 358 | const options = {
|
|---|
| 359 | spreadStrict: false,
|
|---|
| 360 | };
|
|---|
| 361 |
|
|---|
| 362 | const expected = true;
|
|---|
| 363 | const actual = hasEveryProp(props, prop, options);
|
|---|
| 364 |
|
|---|
| 365 | assert.equal(actual, expected);
|
|---|
| 366 | });
|
|---|
| 367 |
|
|---|
| 368 | it('should return false if the prop is considered absent in case-sensitive mode', () => {
|
|---|
| 369 | const code = '<div ID="foo" />';
|
|---|
| 370 | const node = getOpeningElement(code);
|
|---|
| 371 | const { attributes: props } = node;
|
|---|
| 372 | const prop = 'id';
|
|---|
| 373 | const options = {
|
|---|
| 374 | ignoreCase: false,
|
|---|
| 375 | };
|
|---|
| 376 |
|
|---|
| 377 | const expected = false;
|
|---|
| 378 | const actual = hasEveryProp(props, prop, options);
|
|---|
| 379 |
|
|---|
| 380 | assert.equal(actual, expected);
|
|---|
| 381 | });
|
|---|
| 382 |
|
|---|
| 383 | it('should return false if all props are considered absent in case-sensitive mode', () => {
|
|---|
| 384 | const code = '<div ID="foo" />';
|
|---|
| 385 | const node = getOpeningElement(code);
|
|---|
| 386 | const { attributes: props } = node;
|
|---|
| 387 | const prop = ['id', 'iD', 'className'];
|
|---|
| 388 | const options = {
|
|---|
| 389 | ignoreCase: false,
|
|---|
| 390 | };
|
|---|
| 391 |
|
|---|
| 392 | const expected = false;
|
|---|
| 393 | const actual = hasEveryProp(props, prop, options);
|
|---|
| 394 |
|
|---|
| 395 | assert.equal(actual, expected);
|
|---|
| 396 | });
|
|---|
| 397 |
|
|---|
| 398 | it('should return true if all props are considered present in case-sensitive mode', () => {
|
|---|
| 399 | const code = '<div ID="foo" className="box" />';
|
|---|
| 400 | const node = getOpeningElement(code);
|
|---|
| 401 | const { attributes: props } = node;
|
|---|
| 402 | const prop = ['ID', 'className'];
|
|---|
| 403 | const options = {
|
|---|
| 404 | ignoreCase: false,
|
|---|
| 405 | };
|
|---|
| 406 |
|
|---|
| 407 | const expected = true;
|
|---|
| 408 | const actual = hasEveryProp(props, prop, options);
|
|---|
| 409 |
|
|---|
| 410 | assert.equal(actual, expected);
|
|---|
| 411 | });
|
|---|
| 412 | });
|
|---|