indexTest.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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": 12,
  55. "isBadThreshold": 22,
  56. "isAbnormalThreshold": 30,
  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. /*jshint expr: true*/
  75. console.log.should.not.have.been.called;
  76. console.log.restore();
  77. done();
  78. }).fail(function(err) {
  79. console.log.restore();
  80. done(err);
  81. });
  82. });
  83. it('should succeed on try-catch.html', function(done) {
  84. this.timeout(15000);
  85. var url = 'http://localhost:8388/try-catch.html';
  86. ylt(url)
  87. .then(function(data) {
  88. data.toolsResults.phantomas.metrics.should.have.a.property('jsErrors').that.equals(0);
  89. done();
  90. }).fail(function(err) {
  91. console.log.restore();
  92. done(err);
  93. });
  94. });
  95. it('should take a screenshot', function(done) {
  96. this.timeout(15000);
  97. var url = 'http://localhost:8388/simple-page.html';
  98. var screenshotPath = path.join(__dirname, '../../.tmp/indexTestScreenshot.png');
  99. ylt(url, {screenshot: screenshotPath})
  100. .then(function(data) {
  101. data.params.options.should.have.a.property('screenshot').that.equals(screenshotPath);
  102. data.should.not.have.a.property('screenshotUrl');
  103. fs.existsSync(screenshotPath).should.equal(true);
  104. done();
  105. }).fail(function(err) {
  106. done(err);
  107. });
  108. });
  109. });