indexTest.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(1);
  44. data.toolsResults.phantomas.offenders.DOMelementMaxDepth[0].should.equal('body > h1[1]');
  45. // Test rules
  46. data.should.have.a.property('rules').that.is.an('object');
  47. data.rules.should.have.a.property('DOMelementMaxDepth').that.is.an('object');
  48. data.rules.DOMelementMaxDepth.should.deep.equal({
  49. policy: {
  50. "tool": "phantomas",
  51. "label": "DOM max depth",
  52. "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>",
  53. "isOkThreshold": 10,
  54. "isBadThreshold": 20,
  55. "isAbnormalThreshold": 28,
  56. "hasOffenders": true
  57. },
  58. "value": 1,
  59. "bad": false,
  60. "abnormal": false,
  61. "score": 100,
  62. "abnormalityScore": 0,
  63. "offendersObj": {
  64. "count": 1,
  65. "tree": {
  66. "body": {
  67. "h1[1]": 1
  68. }
  69. }
  70. }
  71. });
  72. // Test javascriptExecutionTree
  73. data.toolsResults.phantomas.metrics.should.not.have.a.property('javascriptExecutionTree');
  74. data.toolsResults.phantomas.offenders.should.not.have.a.property('javascriptExecutionTree');
  75. data.should.have.a.property('javascriptExecutionTree').that.is.an('object');
  76. data.javascriptExecutionTree.should.have.a.property('data');
  77. data.javascriptExecutionTree.data.should.have.a.property('type').that.equals('main');
  78. /*jshint expr: true*/
  79. console.log.should.not.have.been.called;
  80. done();
  81. }).fail(function(err) {
  82. done(err);
  83. });
  84. });
  85. it('should take a screenshot', function(done) {
  86. this.timeout(15000);
  87. var url = 'http://localhost:8388/simple-page.html';
  88. var screenshotPath = path.join(__dirname, '../../.tmp/indexTestScreenshot.png');
  89. ylt(url, {screenshot: screenshotPath})
  90. .then(function(data) {
  91. data.params.options.should.have.a.property('screenshot').that.equals(screenshotPath);
  92. data.should.not.have.a.property('screenshotUrl');
  93. fs.existsSync(screenshotPath).should.equal(true);
  94. done();
  95. }).fail(function(err) {
  96. done(err);
  97. });
  98. });
  99. });