const assert = require('assert');
const configMergeLoader = require('../index.js');
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const rimraf = require('rimraf');

describe('Config Merge Loader', function() {
  it('should return a function', function() {
    assert.equal(typeof configMergeLoader, 'function');
  });

  describe('when called via webpack', function() {
    beforeEach(function(done) {
      rimraf(path.resolve(__dirname, 'dist/entry.js'), function(err) {
        if (err) { return done(err); }

        done();
      });
    });

    const options = {
      entry: path.resolve(__dirname, 'cases/test.js'),
      target: 'node',
      output: {
        filename: 'entry.js',
        path: path.resolve(__dirname, 'dist'),
        libraryTarget: 'commonjs2'
      },
      resolveLoader: {
        alias: {
          'config-merge-loader': path.resolve(__dirname, '../', 'index.js')
        }
      },
      module: {
        rules: [
          {
            test: /\.json$/, use: 'json-loader'
          },
          {
            test: /base\.json$/,
            use: [
              {
                loader: 'config-merge-loader',
                query: {
                  override: 'override.json'
                }
              }
            ]
          }
        ]
      }
    };

    const compile = webpack(options);

    it('should generate an entry.js file', function(done) {
      compile.run(function(err, stats) {
        if(err) return done(err);

        assert.ok(fs.existsSync(path.resolve(__dirname, 'dist/entry.js')));
        done();
      });
    });

    it('should compile without errors', function(done) {
      compile.run(function(err, stats) {
        if(err) return done(err);

        assert.ok(stats.hasErrors() === false);
        done();
      });
    });
  });
});

@@ -1846,16 +1846,642 @@
 %0A    %7D);
+%0A%0A    it('base.js should provide properly merged results', function(done) %7B%0A      compile.run(function(err, stats) %7B%0A        if(err) return done(err);%0A%0A        const modules = stats.toJson('normal').modules;%0A        const moduleIndex = modules.findIndex(function(module) %7B%0A          return module.name === './test/cases/lib/base.json';%0A        %7D);%0A        const moduleSource = modules%5BmoduleIndex%5D.source;%0A        const expectedSource = 'module.exports = %7B%5Cn%5Ct%22a%22: 2,%5Cn%5Ct%22b%22: ' +%0A          '%7B%5Cn%5Ct%5Ct%22a%22: 2,%5Cn%5Ct%5Ct%22b%22: 1%5Cn%5Ct%7D,%5Cn%5Ct%22c%22: 1%5Cn%7D;';%0A%0A        assert.equal(moduleSource, expectedSource);%0A        done();%0A      %7D);%0A    %7D);
 %0A  %7D);%0A%7D

Add test to ensure loader returns a properly formatted json object