indexTest.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var chai = require('chai');
  2. var sinon = require('sinon');
  3. var sinonChai = require('sinon-chai');
  4. var should = chai.should();
  5. var ylt = require('../../lib/index');
  6. chai.use(sinonChai);
  7. describe('index.js', function() {
  8. it('should return a promise', function() {
  9. var promise = ylt();
  10. promise.should.have.property('then').that.is.a('function');
  11. promise.should.have.property('fail').that.is.a('function');
  12. });
  13. it('should fail an undefined url', function(done) {
  14. ylt().fail(function(err) {
  15. err.should.be.a('string').that.equals('URL missing');
  16. done();
  17. });
  18. });
  19. it('should fail with an empty url string', function(done) {
  20. ylt('').fail(function(err) {
  21. err.should.be.a('string').that.equals('URL missing');
  22. done();
  23. });
  24. });
  25. it('should succeed on simple-page.html', function(done) {
  26. this.timeout(15000);
  27. // Check if console.log is called
  28. sinon.spy(console, 'log');
  29. var url = 'http://localhost:8388/simple-page.html';
  30. ylt(url)
  31. .then(function(data) {
  32. data.should.be.an('object');
  33. data.toolsResults.should.be.an('object');
  34. // Test Phantomas
  35. data.toolsResults.phantomas.should.be.an('object');
  36. data.toolsResults.phantomas.should.have.a.property('url').that.equals(url);
  37. data.toolsResults.phantomas.should.have.a.property('metrics').that.is.an('object');
  38. data.toolsResults.phantomas.metrics.should.have.a.property('requests').that.equals(1);
  39. data.toolsResults.phantomas.should.have.a.property('offenders').that.is.an('object');
  40. data.toolsResults.phantomas.offenders.should.have.a.property('DOMelementMaxDepth');
  41. data.toolsResults.phantomas.offenders.DOMelementMaxDepth.should.have.length(1);
  42. data.toolsResults.phantomas.offenders.DOMelementMaxDepth[0].should.equal('body > h1[1]');
  43. // Test rules
  44. data.should.have.a.property('rules').that.is.an('object');
  45. data.rules.should.have.a.property('DOMelementMaxDepth').that.is.an('object');
  46. data.rules.DOMelementMaxDepth.should.deep.equal({
  47. policy: {
  48. "tool": "phantomas",
  49. "label": "DOM max depth",
  50. "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>",
  51. "isOkThreshold": 10,
  52. "isBadThreshold": 20,
  53. "isAbnormalThreshold": 28
  54. },
  55. "value": 1,
  56. "bad": false,
  57. "abnormal": false,
  58. "score": 100,
  59. "abnormalityScore": 0,
  60. "offenders": "<div class=\"domTree\"><div><span>body</span><div><span>h1[1]</span></div></div></div>",
  61. "offendersCount": 1
  62. });
  63. // Test javascriptExecutionTree
  64. data.toolsResults.phantomas.metrics.should.not.have.a.property('javascriptExecutionTree');
  65. data.toolsResults.phantomas.offenders.should.not.have.a.property('javascriptExecutionTree');
  66. data.should.have.a.property('javascriptExecutionTree').that.is.an('object');
  67. data.javascriptExecutionTree.should.have.a.property('data');
  68. data.javascriptExecutionTree.data.should.have.a.property('type').that.equals('main');
  69. /*jshint expr: true*/
  70. console.log.should.not.have.been.called;
  71. done();
  72. }).fail(function(err) {
  73. done(err);
  74. });
  75. });
  76. });