customPoliciesTest.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. "colorswitch-30883-30865: 4 occurrences",
  43. "foo: 1 occurrences"
  44. ]
  45. }
  46. }
  47. }
  48. }, policies);
  49. results.should.have.a.property('DOMidDuplicated');
  50. results.DOMidDuplicated.should.have.a.property('offendersObj').that.deep.equals({
  51. "count": 2,
  52. "list": [
  53. {
  54. "id": "colorswitch-30883-30865",
  55. "occurrences": 4
  56. },
  57. {
  58. "id": "foo",
  59. "occurrences": 1
  60. }
  61. ]
  62. });
  63. });
  64. it('should transform DOMqueriesAvoidable offenders', function() {
  65. results = rulesChecker.check({
  66. "toolsResults": {
  67. "phantomas": {
  68. "metrics": {
  69. "DOMqueriesAvoidable": 2
  70. },
  71. "offenders": {
  72. "DOMqueriesDuplicated": [
  73. "id \"#j2t-top-cart\" with getElementById (in context #document): 4 queries",
  74. "class \".listingResult\" with getElementsByClassName (in context body > div#Global > div#Listing): 4 queries"
  75. ]
  76. }
  77. }
  78. }
  79. }, policies);
  80. results.should.have.a.property('DOMqueriesAvoidable');
  81. results.DOMqueriesAvoidable.should.have.a.property('offendersObj').that.deep.equals({
  82. "count": 2,
  83. "list": [
  84. {
  85. "query": "#j2t-top-cart",
  86. "context": {
  87. "type": "document"
  88. },
  89. "fn": "getElementById ",
  90. "count": 4
  91. },
  92. {
  93. "query": ".listingResult",
  94. "context": {
  95. "type": "domElement",
  96. "element": "div#Listing",
  97. "tree": {
  98. "body": {
  99. "div#Global": {
  100. "div#Listing": 1
  101. }
  102. }
  103. }
  104. },
  105. "fn": "getElementsByClassName ",
  106. "count": 4
  107. }
  108. ]
  109. });
  110. });
  111. it('should transform jsErrors offenders', function() {
  112. results = rulesChecker.check({
  113. "toolsResults": {
  114. "phantomas": {
  115. "metrics": {
  116. "jsErrors": 2
  117. },
  118. "offenders": {
  119. "jsErrors": [
  120. "TypeError: 'undefined' is not a function (evaluating 'this.successfullyCollected.bind(this)') - http://asset.easydmp.net/js/collect.js:1160 / callCollecte http://asset.easydmp.net/js/collect.js:1203 / callbackUpdateParams http://asset.easydmp.net/js/collect.js:1135 / http://asset.easydmp.net/js/collect.js:1191",
  121. "TypeError: 'undefined' is not an object (evaluating 'd.readyState') - http://me.hunkal.com/p/:3"
  122. ]
  123. }
  124. }
  125. }
  126. }, policies);
  127. results.should.have.a.property('jsErrors');
  128. results.jsErrors.should.have.a.property('offendersObj').that.deep.equals({
  129. "count": 2,
  130. "list": [
  131. {
  132. "error": "TypeError: 'undefined' is not a function (evaluating 'this.successfullyCollected.bind(this)')",
  133. "backtrace": [
  134. {
  135. "file": "http://asset.easydmp.net/js/collect.js",
  136. "line": 1160
  137. },
  138. {
  139. "file": "http://asset.easydmp.net/js/collect.js",
  140. "line": 1203,
  141. "functionName": "callCollecte"
  142. },
  143. {
  144. "file": "http://asset.easydmp.net/js/collect.js",
  145. "line": 1135,
  146. "functionName": "callbackUpdateParams"
  147. },
  148. {
  149. "file": "http://asset.easydmp.net/js/collect.js",
  150. "line": 1191
  151. }
  152. ]
  153. },
  154. {
  155. "error": "TypeError: 'undefined' is not an object (evaluating 'd.readyState')",
  156. "backtrace": [
  157. {
  158. "file": "http://me.hunkal.com/p/",
  159. "line": 3
  160. }
  161. ]
  162. }
  163. ]
  164. });
  165. });
  166. it('should grade correctly jQuery versions', function() {
  167. var versions = {
  168. '1.7.0': 0,
  169. '1.10.1': 20,
  170. '1.10.3a': 20,
  171. '2.0.0-rc1': 20,
  172. '1.11.1': 30,
  173. '2.1.1-beta1': 30,
  174. '1.12.1': 40,
  175. '2.2.1': 40,
  176. '3.0.1': 50,
  177. '3.1.0': 70,
  178. '3.2.1': 90,
  179. '3.3.1': 100,
  180. '3.5.0': 100
  181. };
  182. for (var version in versions) {
  183. results = rulesChecker.check({
  184. "toolsResults": {
  185. "phantomas": {
  186. "metrics": {
  187. "jQueryVersion": version
  188. }
  189. }
  190. }
  191. }, policies);
  192. results.jQueryVersion.score.should.equal(versions[version]);
  193. }
  194. // Unknown jQuery version
  195. results = rulesChecker.check({
  196. "toolsResults": {
  197. "phantomas": {
  198. "metrics": {
  199. "jQueryVersion": "wooot"
  200. }
  201. }
  202. }
  203. }, policies);
  204. results.should.deep.equals({});
  205. // If jQueryVersionsLoaded is 0
  206. results = rulesChecker.check({
  207. "toolsResults": {
  208. "phantomas": {
  209. "metrics": {
  210. "jQueryVersion": "1.6.0",
  211. "jQueryVersionsLoaded": 0
  212. }
  213. }
  214. }
  215. }, policies);
  216. results.should.not.have.a.property('jQueryVersion');
  217. results.should.have.a.property('jQueryVersionsLoaded');
  218. results.jQueryVersionsLoaded.should.have.a.property('score').that.equals(100);
  219. // If there are more than 1 jQuery version
  220. results = rulesChecker.check({
  221. "toolsResults": {
  222. "phantomas": {
  223. "metrics": {
  224. "jQueryVersion": "1.6.0",
  225. "jQueryVersionsLoaded": 2
  226. }
  227. }
  228. }
  229. }, policies);
  230. results.should.not.have.a.property('jQueryVersion');
  231. results.should.have.a.property('jQueryVersionsLoaded');
  232. results.jQueryVersionsLoaded.should.have.a.property('score').that.equals(0);
  233. results.jQueryVersionsLoaded.should.have.a.property('abnormal').that.equals(true);
  234. });
  235. it('should transform cssParsingErrors offenders', function() {
  236. results = rulesChecker.check({
  237. "toolsResults": {
  238. "phantomas": {
  239. "metrics": {
  240. "cssParsingErrors": 2
  241. },
  242. "offenders": {
  243. "cssParsingErrors": [
  244. "<http://www.sudexpress.com/skin/frontend/sudexpress/default/css/styles.css> (Error: CSS parsing failed: missing '}' @ 4:1)",
  245. "<http://www.sudexpress.com/skin/frontend/sudexpress/default/css/reset.css> (Empty CSS was provided)"
  246. ]
  247. }
  248. }
  249. }
  250. }, policies);
  251. results.should.have.a.property('cssParsingErrors');
  252. results.cssParsingErrors.should.have.a.property('offendersObj').that.deep.equals({
  253. "count": 2,
  254. "list": [
  255. {
  256. "error": "Error: CSS parsing failed: missing '}'",
  257. "file": "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/styles.css",
  258. "line": 4,
  259. "column": 1
  260. },
  261. {
  262. "error": "Empty CSS was provided",
  263. "file": "http://www.sudexpress.com/skin/frontend/sudexpress/default/css/reset.css",
  264. "line": null,
  265. "column": null
  266. }
  267. ]
  268. });
  269. });
  270. // Enough for the moment, to be complete...
  271. });