You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
14 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. var expect = window.chai.expect;
  2. function createSelect() {
  3. $('<select />', { 'id':'rating', 'name':'rating' }).appendTo('body');
  4. for (var i = 1; i <= 10; i++) {
  5. var attributes = (i == 5) ?
  6. attributes = { 'value':i, 'selected':'selected' } :
  7. attributes = { 'value':i };
  8. $('<option />', attributes).appendTo('#rating').html('rating-text-'+i);
  9. }
  10. }
  11. function destroySelect() {
  12. $('#rating').remove();
  13. }
  14. describe('bar rating plugin on init with custom options', function () {
  15. it('should update defaults', function () {
  16. var BarRating;
  17. BarRating = new $.fn.barrating.BarRating();
  18. BarRating.init({
  19. showValues: false
  20. });
  21. expect(BarRating.options).to.be.a('object');
  22. expect(BarRating.options.theme).to.equal('');
  23. expect(BarRating.options.initialRating).to.equal(null);
  24. expect(BarRating.options.allowEmpty).to.equal(null);
  25. expect(BarRating.options.emptyValue).to.equal('');
  26. expect(BarRating.options.showValues).to.equal(false);
  27. expect(BarRating.options.showSelectedRating).to.equal(true);
  28. expect(BarRating.options.deselectable).to.equal(true);
  29. expect(BarRating.options.reverse).to.equal(false);
  30. expect(BarRating.options.readonly).to.equal(false);
  31. expect(BarRating.options.fastClicks).to.equal(true);
  32. expect(BarRating.options.hoverState).to.equal(true);
  33. expect(BarRating.options.silent).to.equal(false);
  34. });
  35. });
  36. describe('bar rating plugin on show', function () {
  37. before(function () {
  38. createSelect();
  39. $('#rating').barrating('show');
  40. });
  41. after(function () {
  42. $('#rating').barrating('destroy');
  43. destroySelect();
  44. });
  45. it('should have data', function () {
  46. expect($('#rating').data('barrating')).to.be.a('object');
  47. });
  48. it('should wrap the select field into a wrapper div', function () {
  49. expect($('.br-widget').parent().hasClass('br-wrapper')).to.equal(true);
  50. });
  51. it('should transform the select field into a rating widget', function () {
  52. expect($('.br-widget a')).to.have.length(10);
  53. });
  54. it('should store rating values in data attributes', function () {
  55. expect($('.br-widget a:first').attr('data-rating-value')).to.equal('1');
  56. expect($('.br-widget a:nth-child(8)').attr('data-rating-value')).to.equal('8');
  57. expect($('.br-widget a:first').attr('data-rating-text')).to.equal('rating-text-1');
  58. expect($('.br-widget a:nth-child(8)').attr('data-rating-text')).to.equal('rating-text-8');
  59. });
  60. it('should read the selected rating from the select field', function () {
  61. expect($('#rating').data('barrating').ratingValue).to.equal('5');
  62. expect($('#rating').data('barrating').ratingText).to.equal('rating-text-5');
  63. });
  64. it('should set correct class', function () {
  65. expect($('.br-widget a:nth-child(4)').hasClass('br-selected')).to.equal(true);
  66. expect($('.br-widget a:nth-child(5)').hasClass('br-selected br-current')).to.equal(true);
  67. expect($('.br-widget a:nth-child(6)').hasClass('br-selected')).to.equal(false);
  68. });
  69. it('should append a rating div', function () {
  70. expect($('div.br-current-rating')).to.have.length(1);
  71. });
  72. it('should display a correct rating', function () {
  73. expect($('div.br-current-rating').html()).to.equal(
  74. $('#rating').data('barrating').ratingText
  75. );
  76. });
  77. it('should hide the select field', function () {
  78. expect($('#rating').css('display')).to.equal('none');
  79. });
  80. });
  81. describe('bar rating plugin on set fractional value', function () {
  82. before(function () {
  83. createSelect();
  84. $('#rating')
  85. .barrating('show', { initialRating: 3.3 });
  86. });
  87. after(function () {
  88. $('#rating').barrating('destroy');
  89. destroySelect();
  90. });
  91. it('should set .br-half class', function () {
  92. expect($('.br-widget a:nth-child(4)').hasClass('br-fractional')).to.equal(true);
  93. expect($('.br-widget a:nth-child(4)').hasClass('br-fractional-30')).to.equal(true);
  94. });
  95. });
  96. describe('bar rating plugin on set fractional value < 1', function () {
  97. before(function () {
  98. createSelect();
  99. $('#rating')
  100. .barrating('show', { initialRating: 0.99 });
  101. });
  102. after(function () {
  103. $('#rating').barrating('destroy');
  104. destroySelect();
  105. });
  106. it('should set .br-half class', function () {
  107. expect($('.br-widget a:first').hasClass('br-fractional')).to.equal(true);
  108. expect($('.br-widget a:first').hasClass('br-fractional-90')).to.equal(true);
  109. });
  110. });
  111. describe('bar rating themes', function() {
  112. before(function () {
  113. createSelect();
  114. });
  115. after(function () {
  116. $('#rating').barrating('destroy');
  117. destroySelect();
  118. });
  119. it('should set the theme class', function() {
  120. $('#rating').barrating({
  121. theme: 'bootstrap-stars'
  122. });
  123. expect($('.br-wrapper').hasClass('br-theme-bootstrap-stars')).to.be.true;
  124. });
  125. });
  126. describe('bar rating plugin on show and rating selected', function () {
  127. var valuesFromCallback = [];
  128. before(function () {
  129. createSelect();
  130. $('#rating').barrating('show', {
  131. onSelect:function (value, text, event) {
  132. valuesFromCallback.push(value, text, event);
  133. }
  134. });
  135. $('.br-widget a:nth-child(2)').trigger('click');
  136. });
  137. after(function () {
  138. $('#rating').barrating('destroy');
  139. destroySelect();
  140. });
  141. it('should update data', function () {
  142. expect($('#rating').data('barrating').ratingValue).to.equal('2');
  143. expect($('#rating').data('barrating').ratingText).to.equal('rating-text-2');
  144. });
  145. it('should set correct class', function () {
  146. expect($('.br-widget a:nth-child(1)').hasClass('br-selected')).to.equal(true);
  147. expect($('.br-widget a:nth-child(2)').hasClass('br-selected br-current')).to.equal(true);
  148. expect($('.br-widget a:nth-child(3)').hasClass('br-selected')).to.equal(false);
  149. });
  150. it('should display a correct rating', function () {
  151. expect($('div.br-current-rating').html()).to.equal(
  152. $('#rating').data('barrating').ratingText
  153. );
  154. });
  155. it('should pass correct values to a callback', function () {
  156. expect(valuesFromCallback[0]).to.equal('2');
  157. expect(valuesFromCallback[1]).to.equal('rating-text-2');
  158. expect(valuesFromCallback[2]).to.be.a('object');
  159. });
  160. });
  161. describe('bar rating plugin on show and empty ratings are allowed', function () {
  162. before(function () {
  163. createSelect();
  164. $('#rating').barrating('show', {
  165. allowEmpty: true,
  166. emptyValue: '-- not defined --'
  167. });
  168. });
  169. after(function () {
  170. $('#rating').barrating('destroy');
  171. destroySelect();
  172. });
  173. it('should update data', function () {
  174. expect($('#rating').data('barrating').allowEmpty).to.equal(true);
  175. expect($('#rating').data('barrating').emptyRatingValue).to.equal('-- not defined --');
  176. expect($('#rating').data('barrating').emptyRatingText).to.equal('');
  177. });
  178. it('should set correct class', function () {
  179. expect($('#rating option').first().val()).to.equal('-- not defined --');
  180. });
  181. });
  182. describe('bar rating plugin reversed', function () {
  183. before(function () {
  184. createSelect();
  185. $('#rating').barrating('show', {
  186. reverse:true
  187. });
  188. });
  189. after(function () {
  190. $('#rating').barrating('destroy');
  191. destroySelect();
  192. });
  193. it('should set correct widget class', function () {
  194. expect($('.br-widget').hasClass('br-reverse')).to.equal(true);
  195. });
  196. it('should set correct class', function () {
  197. expect($('.br-widget a:nth-child(4)').hasClass('br-selected')).to.equal(false);
  198. expect($('.br-widget a:nth-child(5)').hasClass('br-selected br-current')).to.equal(true);
  199. expect($('.br-widget a:nth-child(6)').hasClass('br-selected')).to.equal(true);
  200. });
  201. });
  202. describe('bar rating plugin read-only', function () {
  203. before(function () {
  204. createSelect();
  205. $('#rating').barrating('show', {
  206. readonly:true
  207. });
  208. $('.br-widget a:nth-child(6)').trigger('click');
  209. });
  210. after(function () {
  211. $('#rating').barrating('destroy');
  212. destroySelect();
  213. });
  214. it('should set correct class', function () {
  215. expect($('.br-widget a:nth-child(4)').hasClass('br-selected')).to.equal(true);
  216. expect($('.br-widget a:nth-child(5)').hasClass('br-selected br-current')).to.equal(true);
  217. expect($('.br-widget a:nth-child(6)').hasClass('br-selected')).to.equal(false);
  218. });
  219. it('should ignore user input', function () {
  220. expect($('#rating').data('barrating').ratingValue).to.equal('5');
  221. expect($('#rating').data('barrating').ratingText).to.equal('rating-text-5');
  222. });
  223. });
  224. describe('bar rating plugin on deselect', function () {
  225. before(function () {
  226. createSelect();
  227. // prepend empty OPTION to test deselectable ratings
  228. $('#rating').prepend($('<option />', { 'value':'' }));
  229. $('#rating').barrating('show');
  230. // deselect rating
  231. $('.br-widget a:nth-child(5)').trigger('click');
  232. });
  233. after(function () {
  234. $('#rating').barrating('destroy');
  235. destroySelect();
  236. });
  237. it('should successfully deselect rating', function () {
  238. expect($('#rating').data('barrating').ratingValue).to.equal('');
  239. expect($('#rating').data('barrating').ratingText).to.equal('');
  240. });
  241. });
  242. describe('bar rating plugin on clear', function () {
  243. var valuesFromCallback = [];
  244. before(function () {
  245. createSelect();
  246. $('#rating').barrating('show', {
  247. onClear:function (value, text) {
  248. valuesFromCallback.push(value, text);
  249. }
  250. });
  251. $('.br-widget a:nth-child(6)').trigger('click');
  252. $('#rating').barrating('clear');
  253. });
  254. after(function () {
  255. $('#rating').barrating('destroy');
  256. destroySelect();
  257. });
  258. it('should restore original rating', function () {
  259. expect($('#rating').data('barrating').ratingValue).to.equal('5');
  260. expect($('#rating').data('barrating').ratingText).to.equal('rating-text-5');
  261. });
  262. it('should reset select field', function () {
  263. expect($('#rating').val()).to.equal('5');
  264. });
  265. it('should set correct class', function () {
  266. expect($('.br-widget a:nth-child(4)').hasClass('br-selected')).to.equal(true);
  267. expect($('.br-widget a:nth-child(5)').hasClass('br-selected br-current')).to.equal(true);
  268. expect($('.br-widget a:nth-child(6)').hasClass('br-selected')).to.equal(false);
  269. });
  270. it('should pass correct values to a callback', function () {
  271. expect(valuesFromCallback[0]).to.equal('5');
  272. expect(valuesFromCallback[1]).to.equal('rating-text-5');
  273. });
  274. });
  275. describe('bar rating plugin on destroy', function () {
  276. var valuesFromCallback = [];
  277. before(function () {
  278. createSelect();
  279. $('#rating').barrating('show', {
  280. onDestroy:function (value, text) {
  281. valuesFromCallback.push(value, text);
  282. }
  283. });
  284. $('#rating').barrating('destroy');
  285. });
  286. after(function () {
  287. destroySelect();
  288. });
  289. it('should remove data', function () {
  290. expect($('#rating').data('barrating')).to.equal(undefined);
  291. });
  292. it('should unwrap the select field', function () {
  293. expect($('.br-wrapper').length).to.equal(0);
  294. });
  295. it('should remove the widget', function () {
  296. expect($('.br-widget').length).to.equal(0);
  297. });
  298. it('should show the select field back again', function () {
  299. expect($('#rating').is(":visible")).to.equal(true);
  300. });
  301. it('should pass correct values to a callback', function () {
  302. expect(valuesFromCallback[0]).to.equal('5');
  303. expect(valuesFromCallback[1]).to.equal('rating-text-5');
  304. });
  305. });
  306. describe('bar rating plugin on set value', function () {
  307. var valuesFromCallback = [];
  308. before(function () {
  309. createSelect();
  310. $('#rating').barrating('show', {
  311. onSelect:function (value, text) {
  312. valuesFromCallback.push(value, text);
  313. }
  314. }).barrating('set', 3);
  315. });
  316. after(function () {
  317. destroySelect();
  318. });
  319. it('should set correct value', function () {
  320. expect($('#rating').data('barrating').ratingValue).to.equal(3);
  321. expect($('#rating').data('barrating').ratingText).to.equal('rating-text-3');
  322. });
  323. it('should set correct class', function () {
  324. expect($('.br-widget a:nth-child(3)').hasClass('br-selected')).to.equal(true);
  325. expect($('.br-widget a:nth-child(3)').hasClass('br-current')).to.equal(true);
  326. });
  327. it('should pass correct values to a callback', function () {
  328. expect(valuesFromCallback[0]).to.equal(3);
  329. expect(valuesFromCallback[1]).to.equal('rating-text-3');
  330. });
  331. });
  332. describe('bar rating plugin on set non-existing value', function () {
  333. before(function () {
  334. createSelect();
  335. $('#rating')
  336. .barrating('show', { initialRating: 5 })
  337. .barrating('set', 9999);
  338. });
  339. after(function () {
  340. destroySelect();
  341. });
  342. it('should do nothing', function () {
  343. expect($('#rating').data('barrating').ratingValue).to.equal('5');
  344. expect($('#rating').data('barrating').ratingText).to.equal('rating-text-5');
  345. });
  346. });
  347. describe('bar rating plugin on change read-only state', function () {
  348. before(function () {
  349. createSelect();
  350. $('#rating')
  351. .barrating('show', { initialRating: 1 })
  352. .barrating('readonly', true);
  353. $('.br-widget a:last-child').trigger('click');
  354. });
  355. after(function () {
  356. destroySelect();
  357. });
  358. it('should ignore user input', function () {
  359. expect($('#rating').data('barrating').ratingValue).to.equal('1');
  360. expect($('#rating').data('barrating').ratingText).to.equal('rating-text-1');
  361. });
  362. it('should update data', function () {
  363. expect($('#rating').data('barrating').readOnly).to.equal(true);
  364. });
  365. it('should set correct widget class', function () {
  366. expect($('.br-widget').hasClass('br-readonly')).to.equal(true);
  367. });
  368. });