import test from 'ava';
import mix from '../src/index';
import sinon from 'sinon';
import FileCollection from '../src/FileCollection';
import fs from 'fs-extra';
import eol from 'eol';

let stubsDir = path.resolve(__dirname, 'stubs');

test.before(t => fs.ensureDirSync(stubsDir));

test.afterEach(t => {
    fs.emptyDirSync(stubsDir);
});

test('that it can get the underlying files', t => {
    let files = ['path/to/file.js'];

    t.deepEqual(files, new FileCollection(files).get());
});

test.cb('that it can merge multiple files into one.', t => {
    let files = [
        path.resolve(stubsDir, 'one.js'),
        path.resolve(stubsDir, 'two.js')
    ];

    new File(files[0]).write('class Foo {}');
    new File(files[1]).write('class Bar {}');

    let output = new File(path.resolve(stubsDir, 'merged.js'));

    new FileCollection(files).merge(output).then(() => {
        t.true(File.exists(output.path()));
        t.is('class Foo {}\n\nclass Bar {}\n', eol.lf(output.read()));
        t.end();
    });
});

test.cb('that it can merge JS files and apply Babel compilation.', t => {
    let files = [
        path.resolve(stubsDir, 'one.js'),
        path.resolve(stubsDir, 'two.js')
    ];

    new File(files[0]).write('class Foo {}');
    new File(files[1]).write('class Bar {}');

    let collection = new FileCollection(files);

    sinon.stub(collection, 'babelify').callsFake(() => 'fake minified output');

    let output = new File(path.resolve(stubsDir, 'merged.js'));
    let useBabel = true;
    collection.merge(output, useBabel).then(() => {
        t.true(File.exists(output.path()));
        t.is(eol.auto('fake minified output\n'), output.read());
        t.end();
    });
});

@@ -1681,28 +1681,529 @@
        t.end();%0A    %7D);%0A%7D);%0A
+%0Atest.cb(%22that it throw an error if a file doesn't exist.%22, t =%3E %7B%0A    let files = %5Bpath.resolve(stubsDir, 'fileThatDoesntExist.js')%5D;%0A    let output = new File(path.resolve(stubsDir, 'merged.js'));%0A    let mergeFiles = () =%3E new FileCollection(files).merge(output);%0A    let expectedMessage = %60ENOENT: no such file or directory, open '$%7B%0A        files%5B0%5D%0A    %7D'%60;%0A%0A    t.throwsAsync(mergeFiles(), expectedMessage).then(() =%3E %7B%0A        t.false(File.exists(output.path()));%0A        t.end();%0A    %7D);%0A%7D);%0A

Add test for FileCollection.merge should throw if file not found