

/* globals describe */
/* globals it */

describe('JS Resource', function() {

  var expect = require('chai').expect;
  var node_path = require('path');
  var fs = require('fs');
  var JS = require('../lib/resource/JS');

  var testData = node_path.join(__dirname, '..', '__test_data__', 'JS');

  it('should have a js type', function() {
    var js = new JS('a');
    expect(js.type).to.equal('js');
  });

  it('should have a relative path', function() {
    var js = new JS('a');
    expect(js.path).to.deep.equal('a');
  });

  it('should have a null id', function() {
    var js = new JS('a');
    expect(js.id).to.be.null;
  });

  it('should can retrieve file content', function() {
    var js = new JS(node_path.join(testData, 'empty.js'));
    expect(js.getContent()).to.deep.equal('');
    js = new JS(node_path.join(testData, 'plain.js'));
    expect(js.getContent()).to.deep.equal('var str = \'Hello World\';');
  });

  it('should can set file content', function() {
    var cssContent = 'html, body { border: 0 }';
    var js = new JS(node_path.join(testData, 'empty.js'));
    js.setContent(cssContent);
    expect(js.getContent()).to.deep.equal(cssContent);
    js.setContent('');
    expect(js.getContent()).to.deep.equal('');
  });

  it('should flush content to destination', function(done) {
    var js = new JS(node_path.join(testData, 'empty.js'));
    var cssContent = 'var str = \'Hello World\';';
    js.setContent(cssContent);

    var distp = node_path.join(testData, 'dist.js');
    js.flush(distp, function() {
      var js = new JS(distp);
      expect(js.getContent()).to.deep.equal(cssContent);
      fs.unlinkSync(distp);
      done();
    });
  });
});
