customPoliciesTest.js 12 KB

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