import assert from 'power-assert';
import {
  assertThat, 
  promiseThat, is, fulfilled, rejected,
  allOf, truthy
} from 'hamjest';
import {convertOneFile} from '../../scripts/minify-json-files';
import fs from 'fs';
import path from 'path';

describe('convert one file', () => {
  
  describe('existing file `stars.json`', () => {
  
    const destFile = path.join(__dirname, '../../dist/data/stars.json');
    const sourceFile = path.join(__dirname, '../../data/stars.json');
    let promise; 
  
    beforeEach(() => { 
      if (fs.existsSync(destFile)) fs.unlinkSync(destFile);
      promise = convertOneFile(sourceFile, destFile);
    });
  
    it('returns a promise that fulfills', () => {
      return promiseThat(promise, is(fulfilled()));
    });
  
    it('writes the destination file', () => {
      return promise.then(() => assert(fs.existsSync(destFile), 'Destination file missing.'));
    });
  
    it('is minified', () => {
      return promise.then(() => {
        const jsonFile = fs.readFileSync(destFile, 'utf8');
        assertThat(jsonFile, is(JSON.stringify(JSON.parse(jsonFile))))
      });
    });
    
  });

  describe('fails', () => {
  
    const validJsonFile = path.join(__dirname, '../../data/stars.json');
    const notExistingFile = path.join(__dirname, '../../not-an-existing-file');
    const nonJsonFile = path.join(__dirname, '../../README.md');
    const invalidDestFile = path.join(__dirname, '../../dist/data/not-a-directory/stars.json');
    let promise; 
  
    it('for a not existing file', () => {
      const promise = convertOneFile(notExistingFile, '');
      return promiseThat(promise, is(rejected()));
    });
  
    it('for a non-JSON file', () => {
      const promise = convertOneFile(nonJsonFile, '');
      return promiseThat(promise, is(rejected()));
    });
  
    it('for an invalid destination path', () => {
      const promise = convertOneFile(validJsonFile, invalidDestFile);
      return promiseThat(promise, is(rejected()));
    });
  
  });
  
});

@@ -107,16 +107,45 @@
 , truthy
+, everyItem,%0A  FeatureMatcher
 %0A%7D from 
@@ -2036,12 +2036,1452 @@
  %7D);%0A  %0A%7D);%0A
+%0Adescribe('convert multiple files', () =%3E %7B%0A  %0A  const fromPath = path.join(__dirname, '../../data');%0A  const destPath = path.join(__dirname, '../../dist/data');%0A  const jsonFiles = %5B'stars.json', 'people.json'%5D;%0A  %0A  let promise;%0A  describe('all JSON files', () =%3E %7B%0A%0A    beforeEach(() =%3E %7B%0A      jsonFiles.forEach(fileName =%3E %7B%0A        const destFile = path.join(destPath, fileName);%0A        if (fs.existsSync(destFile)) fs.unlinkSync(destFile);%0A      %7D);%0A      promise = convertManyFiles(fromPath, jsonFiles, destPath);%0A    %7D);%0A    %0A    it('fulfills', () =%3E %7B%0A      return promiseThat(promise, is(fulfilled()));%0A    %7D);%0A    %0A    it.only('fulfills when all are copied', () =%3E %7B%0A      const fileInDestPath = matcherOrValue =%3E %7B%0A        return new FeatureMatcher(matcherOrValue, 'file in destPath', 'is file', fileName =%3E %7B%0A          return fs.existsSync(path.join(destPath, fileName));%0A        %7D)%0A      %7D;%0A      return promise.then(() =%3E %7B%0A        assertThat(jsonFiles, everyItem(fileInDestPath(is(true))));%0A      %7D);%0A    %7D);%0A%0A  %7D);%0A    %0A%7D);%0A%0Afunction convertManyFiles(fromPath, fileNames, destPath) %7B%0A  const fromFileName = fileName =%3E path.join(fromPath, fileName);%0A  const toFileName = fileName =%3E path.join(destPath, fileName);%0A  const allFiles = %5B%0A    convertOneFile(fromFileName(fileNames%5B0%5D), toFileName(fileNames%5B0%5D)),%0A    convertOneFile(fromFileName(fileNames%5B1%5D), toFileName(fileNames%5B1%5D))%0A  %5D;%0A  return Promise.all(allFiles);%0A%7D%0A

Convert two files and wait until both are done.