customPoliciesTest.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. var should = require('chai').should();
  2. var rulesChecker = require('../../lib/rulesChecker');
  3. describe('customPolicies', function() {
  4. var policies = require('../../lib/metadata/policies.js');
  5. var results;
  6. it('should transform DOMelementMaxDepth offenders', function() {
  7. results = rulesChecker.check({
  8. "toolsResults": {
  9. "phantomas": {
  10. "metrics": {
  11. "DOMelementMaxDepth": 3
  12. },
  13. "offenders": {
  14. "DOMelementMaxDepth": [
  15. "body > div#foo > span.bar"
  16. ]
  17. }
  18. }
  19. }
  20. }, policies);
  21. results.should.have.a.property('DOMelementMaxDepth');
  22. results.DOMelementMaxDepth.should.have.a.property('offendersObj').that.deep.equals({
  23. "count": 1,
  24. "tree": {
  25. "body": {
  26. "div#foo": {
  27. "span.bar": 1
  28. }
  29. }
  30. }
  31. });
  32. });
  33. it('should transform DOMidDuplicated offenders', function() {
  34. results = rulesChecker.check({
  35. "toolsResults": {
  36. "phantomas": {
  37. "metrics": {
  38. "DOMidDuplicated": 2
  39. },
  40. "offenders": {
  41. "DOMidDuplicated": [
  42. {
  43. "id": "colorswitch-30883-30865",
  44. "occurrences": 4
  45. }, {
  46. "id": "foo",
  47. "occurrences": 1
  48. }
  49. ]
  50. }
  51. }
  52. }
  53. }, policies);
  54. results.should.have.a.property('DOMidDuplicated');
  55. results.DOMidDuplicated.should.have.a.property('offendersObj').that.deep.equals({
  56. "count": 2,
  57. "list": [
  58. {
  59. "id": "colorswitch-30883-30865",
  60. "occurrences": 4
  61. },
  62. {
  63. "id": "foo",
  64. "occurrences": 1
  65. }
  66. ]
  67. });
  68. });
  69. it('should grade correctly jQuery versions', function() {
  70. var versions = {
  71. '1.7.0': 0,
  72. '1.10.1': 20,
  73. '1.10.3a': 20,
  74. '2.0.0-rc1': 20,
  75. '1.11.1': 30,
  76. '2.1.1-beta1': 30,
  77. '1.12.1': 40,
  78. '2.2.1': 40,
  79. '3.0.1': 50,
  80. '3.1.0': 70,
  81. '3.2.1': 90,
  82. '3.3.1': 100,
  83. '3.5.0': 100
  84. };
  85. for (var version in versions) {
  86. results = rulesChecker.check({
  87. "toolsResults": {
  88. "phantomas": {
  89. "metrics": {
  90. "jQueryVersion": version
  91. },
  92. "offenders": {}
  93. }
  94. }
  95. }, policies);
  96. results.jQueryVersion.score.should.equal(versions[version]);
  97. }
  98. // Unknown jQuery version
  99. results = rulesChecker.check({
  100. "toolsResults": {
  101. "phantomas": {
  102. "metrics": {
  103. "jQueryVersion": "wooot"
  104. },
  105. "offenders": {}
  106. }
  107. }
  108. }, policies);
  109. results.should.not.have.a.property('jQueryVersion');
  110. // If jQueryVersionsLoaded is 0
  111. results = rulesChecker.check({
  112. "toolsResults": {
  113. "phantomas": {
  114. "metrics": {
  115. "jQueryVersion": "1.6.0",
  116. "jQueryVersionsLoaded": 0
  117. },
  118. "offenders": {}
  119. }
  120. }
  121. }, policies);
  122. results.should.not.have.a.property('jQueryVersion');
  123. results.should.have.a.property('jQueryVersionsLoaded');
  124. results.jQueryVersionsLoaded.should.have.a.property('score').that.equals(100);
  125. // If there are more than 1 jQuery version
  126. results = rulesChecker.check({
  127. "toolsResults": {
  128. "phantomas": {
  129. "metrics": {
  130. "jQueryVersion": "1.6.0",
  131. "jQueryVersionsLoaded": 2
  132. },
  133. "offenders": {}
  134. }
  135. }
  136. }, policies);
  137. results.should.not.have.a.property('jQueryVersion');
  138. results.should.have.a.property('jQueryVersionsLoaded');
  139. results.jQueryVersionsLoaded.should.have.a.property('score').that.equals(0);
  140. results.jQueryVersionsLoaded.should.have.a.property('abnormal').that.equals(false);
  141. });
  142. it('should transform cssParsingErrors offenders', function() {
  143. results = rulesChecker.check({
  144. "toolsResults": {
  145. "phantomas": {
  146. "metrics": {
  147. "cssParsingErrors": 2
  148. },
  149. "offenders": {
  150. "cssParsingErrors": [
  151. {
  152. "url": "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/styles.css",
  153. "value": {
  154. "message": "Error: CSS parsing failed: missing '}'",
  155. "position": {
  156. "start": {
  157. "line": 4,
  158. "column": 1
  159. },
  160. "end": {
  161. "line": 4,
  162. "column": 72
  163. }
  164. }
  165. }
  166. },
  167. {
  168. "url": "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/reset.css",
  169. "value": {
  170. "message": "Empty CSS was provided"
  171. }
  172. },
  173. "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/another.css"
  174. ]
  175. }
  176. }
  177. }
  178. }, policies);
  179. results.should.have.a.property('cssParsingErrors');
  180. results.cssParsingErrors.should.have.a.property('offendersObj').that.deep.equals({
  181. "count": 3,
  182. "list": [
  183. {
  184. "error": "Error: CSS parsing failed: missing '}'",
  185. "file": "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/styles.css",
  186. "line": 4,
  187. "column": 1
  188. },
  189. {
  190. "error": "Empty CSS was provided",
  191. "file": "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/reset.css",
  192. "line": null,
  193. "column": null
  194. },
  195. {
  196. "error": "Unknown parsing error. The entire file was ignored. As a result, the other CSS metrics and scores are miscalculated.",
  197. "file": "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/another.css",
  198. "line": null,
  199. "column": null
  200. }
  201. ]
  202. });
  203. });
  204. // Enough for the moment, to be complete...
  205. });