import sinon from 'sinon';
import chai from 'chai';
const expect = chai.expect;
import * as fetch from '../../../../src/functions/fetch.js';
import checkMinifiedJs from '../../../../src/html/js/minify';

describe('html', function() {
  describe('test minify promise', function() {
    let sandbox;

    beforeEach(function() {
      sandbox = sinon.sandbox.create();
    });

    afterEach(function() {
      sandbox.restore();
    });

    it('should return original URL even if JS is not minified', () => {
      const result = {
        body: 'function something(text) { console.log("not minified javascript file"); }',
      };

      sandbox.stub(fetch, 'fetch').returns(result);

      return new Promise((resolve, reject) => {
          resolve('https://www.juffalow.com/jsfile.js');
        })
        .then(checkMinifiedJs)
        .then((url) => {
          expect(url).to.equal('https://www.juffalow.com/jsfile.js');
        });
    });
  });
});

import sinon from 'sinon';
import chai from 'chai';
const expect = chai.expect;
import * as fetch from '../../../../src/functions/fetch.js';
import checkMinifiedJs from '../../../../src/html/js/minify';

describe('html', function() {
  describe('test minify promise', function() {
    let sandbox;

    beforeEach(function() {
      sandbox = sinon.sandbox.create();
    });

    afterEach(function() {
      sandbox.restore();
    });

    it('should return original URL even if JS is not minified', () => {
      const html = {
        body: '<html><head><link rel="stylesheet" href="https://juffalow.com/cssfile.css" /></head><body><p>Text</p><a href="#">link</a><script src="https://juffalow.com/jsfile.js"></script></body></html>',
      };

      const result = {
        body: 'function something(text) { console.log("not minified javascript file"); }',
        response: {
          statusCode: 200,
        },
      };

      sandbox.stub(fetch, 'fetch').callsFake((url) => {
        switch (url) {
          case 'https://juffalow.com/':
            return html;
          case 'https://juffalow.com/jsfile.js':
            return result;
        }
      });

      return checkMinifiedJs('https://juffalow.com/')
        .then((results) => {
          expect(results.length).to.equal(1);
          expect(results[0].messages.length).to.equal(0);
          expect(results[0].errors.length).to.equal(1);
        });
    });
  });
});

Fix js minify promise test