hasProp-test.js 11 KB

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