indexTest.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var chai = require('chai');
  2. var sinon = require('sinon');
  3. var sinonChai = require('sinon-chai');
  4. var should = chai.should();
  5. var path = require('path');
  6. var fs = require('fs');
  7. var ylt = require('../../lib/index');
  8. chai.use(sinonChai);
  9. describe('index.js', function() {
  10. it('should return a promise', function() {
  11. var promise = ylt();
  12. promise.should.have.property('then').that.is.a('function');
  13. promise.should.have.property('fail').that.is.a('function');
  14. });
  15. it('should fail an undefined url', function(done) {
  16. ylt().fail(function(err) {
  17. err.should.be.a('string').that.equals('URL missing');
  18. done();
  19. });
  20. });
  21. it('should fail with an empty url string', function(done) {
  22. ylt('').fail(function(err) {
  23. err.should.be.a('string').that.equals('URL missing');
  24. done();
  25. });
  26. });
  27. it('should succeed on simple-page.html', function(done) {
  28. this.timeout(15000);
  29. // Check if console.log is called
  30. sinon.spy(console, 'log');
  31. var url = 'http://localhost:8388/simple-page.html';
  32. ylt(url)
  33. .then(function(data) {
  34. data.should.be.an('object');
  35. data.toolsResults.should.be.an('object');
  36. // Test Phantomas
  37. data.toolsResults.phantomas.should.be.an('object');
  38. data.toolsResults.phantomas.should.have.a.property('url').that.equals(url);
  39. data.toolsResults.phantomas.should.have.a.property('metrics').that.is.an('object');
  40. data.toolsResults.phantomas.metrics.should.have.a.property('requests').that.equals(1);
  41. data.toolsResults.phantomas.should.have.a.property('offenders').that.is.an('object');
  42. data.toolsResults.phantomas.offenders.should.have.a.property('DOMelementMaxDepth');
  43. data.toolsResults.phantomas.offenders.DOMelementMaxDepth.should.have.length(2);
  44. data.toolsResults.phantomas.offenders.DOMelementMaxDepth[0].should.equal('body > h1[0]');
  45. data.toolsResults.phantomas.offenders.DOMelementMaxDepth[1].should.equal('body > script[1]');
  46. // Test rules
  47. data.should.have.a.property('rules').that.is.an('object');
  48. data.rules.should.have.a.property('DOMelementMaxDepth').that.is.an('object');
  49. data.rules.DOMelementMaxDepth.should.deep.equal({
  50. policy: {
  51. "tool": "phantomas",
  52. "label": "DOM max depth",
  53. "message": "<p>A deep DOM makes the CSS matching with DOM elements difficult.</p><p>It also slows down JavaScript modifications to the DOM because changing the dimensions of an element makes the browser re-calculate the dimensions of it's parents. Same thing for JavaScript events, that bubble up to the document root.</p>",
  54. "isOkThreshold": 10,
  55. "isBadThreshold": 20,
  56. "isAbnormalThreshold": 28,
  57. "hasOffenders": true
  58. },
  59. "value": 1,
  60. "bad": false,
  61. "abnormal": false,
  62. "score": 100,
  63. "abnormalityScore": 0,
  64. "offendersObj": {
  65. "count": 2,
  66. "tree": {
  67. "body": {
  68. "h1[0]": 1,
  69. "script[1]": 1
  70. }
  71. }
  72. }
  73. });
  74. // Test javascriptExecutionTree
  75. data.toolsResults.phantomas.metrics.should.not.have.a.property('javascriptExecutionTree');
  76. data.toolsResults.phantomas.offenders.should.not.have.a.property('javascriptExecutionTree');
  77. data.should.have.a.property('javascriptExecutionTree').that.is.an('object');
  78. data.javascriptExecutionTree.should.have.a.property('data');
  79. data.javascriptExecutionTree.data.should.have.a.property('type').that.equals('main');
  80. data.javascriptExecutionTree.data.should.have.a.property('domInteractive').that.is.a('number');
  81. data.javascriptExecutionTree.data.should.have.a.property('domContentLoaded').that.is.a('number');
  82. data.javascriptExecutionTree.data.should.have.a.property('domContentLoadedEnd').that.is.a('number');
  83. data.javascriptExecutionTree.data.should.have.a.property('domComplete').that.is.a('number');
  84. /*jshint expr: true*/
  85. console.log.should.not.have.been.called;
  86. console.log.restore();
  87. done();
  88. }).fail(function(err) {
  89. console.log.restore();
  90. done(err);
  91. });
  92. });
  93. it('should succeed on try-catch.html', function(done) {
  94. this.timeout(15000);
  95. var url = 'http://localhost:8388/try-catch.html';
  96. ylt(url)
  97. .then(function(data) {
  98. data.toolsResults.phantomas.metrics.should.have.a.property('jsErrors').that.equals(0);
  99. done();
  100. }).fail(function(err) {
  101. console.log.restore();
  102. done(err);
  103. });
  104. });
  105. it('should take a screenshot', function(done) {
  106. this.timeout(15000);
  107. var url = 'http://localhost:8388/simple-page.html';
  108. var screenshotPath = path.join(__dirname, '../../.tmp/indexTestScreenshot.png');
  109. ylt(url, {screenshot: screenshotPath})
  110. .then(function(data) {
  111. data.params.options.should.have.a.property('screenshot').that.equals(screenshotPath);
  112. data.should.not.have.a.property('screenshotUrl');
  113. fs.existsSync(screenshotPath).should.equal(true);
  114. done();
  115. }).fail(function(err) {
  116. done(err);
  117. });
  118. });
  119. });