/node_modules/cheerio/test/api.attributes.js

https://bitbucket.org/yarov/api-recetario-la-coste-a · JavaScript · 260 lines · 214 code · 43 blank · 3 comment · 0 complexity · ef4a76f41f50c6af5b1688b29e07bab0 MD5 · raw file

  1. var expect = require('expect.js');
  2. var $ = require('../');
  3. var fruits = require('./fixtures').fruits;
  4. var vegetables = require('./fixtures').vegetables;
  5. var inputs = require('./fixtures').inputs;
  6. describe('$(...)', function() {
  7. describe('.attr', function() {
  8. it('() : should get all the attributes', function() {
  9. var attrs = $('ul', fruits).attr();
  10. expect(attrs.id).to.equal('fruits');
  11. });
  12. it('(invalid key) : invalid attr should get undefined', function() {
  13. var attr = $('.apple', fruits).attr('lol');
  14. expect(attr).to.be(undefined);
  15. });
  16. it('(valid key) : valid attr should get value', function() {
  17. var cls = $('.apple', fruits).attr('class');
  18. expect(cls).to.equal('apple');
  19. });
  20. it('(key, value) : should set attr', function() {
  21. var $fruits = $(fruits);
  22. var $pear = $('.pear', $fruits).attr('id', 'pear');
  23. expect($('#pear', $fruits)).to.have.length(1);
  24. expect($pear.cheerio).to.not.be(undefined);
  25. });
  26. it('(map) : object map should set multiple attributes', function() {
  27. var $fruits = $(fruits);
  28. $('.apple', $fruits).attr({
  29. id: 'apple',
  30. style: 'color:red;',
  31. 'data-url': 'http://apple.com'
  32. });
  33. var attrs = $('.apple', $fruits).attr();
  34. expect(attrs.id).to.equal('apple');
  35. expect(attrs.style).to.equal('color:red;');
  36. expect(attrs['data-url']).to.equal('http://apple.com');
  37. });
  38. it('(key, value) : should correctly encode then decode unsafe values', function() {
  39. var $apple = $('.apple', fruits);
  40. $apple.attr('href', 'http://github.com/"><script>alert("XSS!")</script><br');
  41. expect($apple[0].attribs.href).to.equal('http://github.com/&quot;&gt;&lt;script&gt;alert(&quot;XSS!&quot;)&lt;/script&gt;&lt;br');
  42. expect($apple.attr('href')).to.equal('http://github.com/"><script>alert("XSS!")</script><br');
  43. $apple.attr('href', 'http://github.com/"><script>alert("XSS!")</script><br');
  44. expect($apple.html()).to.not.contain('<script>alert("XSS!")</script>');
  45. });
  46. it('(key, value) : should coerce values to a string', function() {
  47. var $apple = $('.apple', fruits);
  48. $apple.attr('data-test', 1);
  49. expect($apple[0].attribs['data-test']).to.equal('1');
  50. expect($apple.attr('data-test')).to.equal('1');
  51. });
  52. });
  53. describe('.val', function() {
  54. it('.val(): on select should get value', function() {
  55. var val = $('select#one', inputs).val();
  56. expect(val).to.equal('option_selected');
  57. });
  58. it('.val(): on text input should get value', function() {
  59. var val = $('input[type="text"]', inputs).val();
  60. expect(val).to.equal('input_text');
  61. });
  62. it('.val(): on checked checkbox should get value', function() {
  63. var val = $('input[name="checkbox_on"]', inputs).val();
  64. expect(val).to.equal('on');
  65. });
  66. it('.val(): on unchecked checkbox should get null', function() {
  67. var val = $('input[name="checkbox_off"]', inputs).val();
  68. expect(val).to.equal(null);
  69. });
  70. it('.val(): on radio should get value', function() {
  71. var val = $('input[type="radio"]', inputs).val();
  72. expect(val).to.equal('on');
  73. });
  74. it('.val(): on multiple select should get an array of values', function() {
  75. var val = $('select#multi', inputs).val();
  76. expect(val).to.have.length(2);
  77. });
  78. it('.val(value): on input text should set value', function() {
  79. var element = $('input[type="text"]', inputs).val('test');
  80. expect(element.val()).to.equal('test');
  81. });
  82. it('.val(value): on select should set value', function() {
  83. var element = $('select#one', inputs).val('option_not_selected');
  84. expect(element.val()).to.equal('option_not_selected');
  85. });
  86. it('.val(value): on radio should set value', function() {
  87. var element = $('input[name="radio"]', inputs).val('off');
  88. expect(element.val()).to.equal('off');
  89. });
  90. it('.val(values): on multiple select should set multiple values', function() {
  91. var element = $('select#multi', inputs).val(['1', '3', '4']);
  92. expect(element.val()).to.have.length(3);
  93. });
  94. });
  95. describe('.removeAttr', function() {
  96. it('(key) : should remove a single attr', function() {
  97. var $fruits = $(fruits);
  98. expect($('ul', $fruits).attr('id')).to.not.be(undefined);
  99. $('ul', $fruits).removeAttr('id');
  100. expect($('ul', $fruits).attr('id')).to.be(undefined);
  101. });
  102. it('should return cheerio object', function() {
  103. var obj = $('ul', fruits).removeAttr('id').cheerio;
  104. expect(obj).to.be.ok();
  105. });
  106. });
  107. describe('.hasClass', function() {
  108. it('(valid class) : should return true', function() {
  109. var $fruits = $(fruits);
  110. var cls = $('.apple', $fruits).hasClass('apple');
  111. expect(cls).to.be.ok();
  112. });
  113. it('(invalid class) : should return false', function() {
  114. var cls = $('#fruits', fruits).hasClass('fruits');
  115. expect(cls).to.not.be.ok();
  116. });
  117. it('should check multiple classes', function() {
  118. var $fruits = $(fruits);
  119. // Add a class
  120. $('.apple', $fruits).addClass('red');
  121. expect($('.apple', $fruits).hasClass('apple')).to.be.ok();
  122. expect($('.apple', $fruits).hasClass('red')).to.be.ok();
  123. // Remove one and test again
  124. $('.apple', $fruits).removeClass('apple');
  125. expect($('li', $fruits).eq(0).hasClass('apple')).to.not.be.ok();
  126. // expect($('li', $fruits).eq(0).hasClass('red')).to.be.ok();
  127. });
  128. });
  129. describe('.addClass', function() {
  130. it('(first class) : should add the class to the element', function() {
  131. var $fruits = $(fruits);
  132. $('#fruits', $fruits).addClass('fruits');
  133. var cls = $('#fruits', $fruits).hasClass('fruits');
  134. expect(cls).to.be.ok();
  135. });
  136. it('(single class) : should add the class to the element', function() {
  137. var $fruits = $(fruits);
  138. $('.apple', $fruits).addClass('fruit');
  139. var cls = $('.apple', $fruits).hasClass('fruit');
  140. expect(cls).to.be.ok();
  141. });
  142. it('(class): adds classes to many selected items', function() {
  143. var $fruits = $(fruits);
  144. $('li', $fruits).addClass('fruit');
  145. expect($('.apple', $fruits).hasClass('fruit')).to.be.ok();
  146. expect($('.orange', $fruits).hasClass('fruit')).to.be.ok();
  147. expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
  148. });
  149. it('(class class class) : should add multiple classes to the element', function() {
  150. var $fruits = $(fruits);
  151. $('.apple', $fruits).addClass('fruit red tasty');
  152. expect($('.apple', $fruits).hasClass('apple')).to.be.ok();
  153. expect($('.apple', $fruits).hasClass('fruit')).to.be.ok();
  154. expect($('.apple', $fruits).hasClass('red')).to.be.ok();
  155. expect($('.apple', $fruits).hasClass('tasty')).to.be.ok();
  156. });
  157. it('(fn) : should add classes returned from the function');
  158. });
  159. describe('.removeClass', function() {
  160. it('() : should remove all the classes', function() {
  161. var $fruits = $(fruits);
  162. $('.pear', $fruits).addClass('fruit');
  163. $('.pear', $fruits).removeClass();
  164. expect($('.pear', $fruits).attr('class')).to.be(undefined);
  165. });
  166. it('(invalid class) : should not remove anything', function() {
  167. var $fruits = $(fruits);
  168. $('.pear', $fruits).removeClass('fruit');
  169. expect($('.pear', $fruits).hasClass('pear')).to.be.ok();
  170. });
  171. it('(no class attribute) : should not throw an exception', function() {
  172. var $vegetables = $(vegetables);
  173. var thrown = null;
  174. expect(function() {
  175. $('li', $vegetables).removeClass('vegetable');
  176. })
  177. .to.not.throwException();
  178. });
  179. it('(single class) : should remove a single class from the element', function() {
  180. var $fruits = $(fruits);
  181. $('.pear', $fruits).addClass('fruit');
  182. expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
  183. $('.pear', $fruits).removeClass('fruit');
  184. expect($('.pear', $fruits).hasClass('fruit')).to.not.be.ok();
  185. expect($('.pear', $fruits).hasClass('pear')).to.be.ok();
  186. });
  187. it('(single class) : should remove a single class from multiple classes on the element', function() {
  188. var $fruits = $(fruits);
  189. $('.pear', $fruits).addClass('fruit green tasty');
  190. expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
  191. expect($('.pear', $fruits).hasClass('green')).to.be.ok();
  192. expect($('.pear', $fruits).hasClass('tasty')).to.be.ok();
  193. $('.pear', $fruits).removeClass('green');
  194. expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
  195. expect($('.pear', $fruits).hasClass('green')).to.not.be.ok();
  196. expect($('.pear', $fruits).hasClass('tasty')).to.be.ok();
  197. });
  198. it('(class class class) : should remove multiple classes from the element', function() {
  199. var $fruits = $(fruits);
  200. $('.apple', $fruits).addClass('fruit red tasty');
  201. expect($('.apple', $fruits).hasClass('apple')).to.be.ok();
  202. expect($('.apple', $fruits).hasClass('fruit')).to.be.ok();
  203. expect($('.apple', $fruits).hasClass('red')).to.be.ok();
  204. expect($('.apple', $fruits).hasClass('tasty')).to.be.ok();
  205. $('.apple', $fruits).removeClass('apple red tasty');
  206. expect($('.fruit', $fruits).hasClass('apple')).to.not.be.ok();
  207. expect($('.fruit', $fruits).hasClass('red')).to.not.be.ok();
  208. expect($('.fruit', $fruits).hasClass('tasty')).to.not.be.ok();
  209. expect($('.fruit', $fruits).hasClass('fruit')).to.be.ok();
  210. });
  211. it('(class) : should remove all occurrences of a class name', function() {
  212. var $div = $('<div class="x x y x z"></div>');
  213. expect($div.removeClass('x').hasClass('x')).to.be(false);
  214. });
  215. it('(fn) : should remove classes returned from the function');
  216. });
  217. });