var path = require('path');

const webpack = require('webpack');

const CONTEXT = path.resolve(__dirname);
const SCSS_DIR = path.resolve(CONTEXT, 'scss');
const PROJECT_DIR = path.resolve(CONTEXT, 'src');
const TESTS_DIR = path.resolve(CONTEXT, 'tests');

const ENTRY_POINT = path.resolve(TESTS_DIR, '__entry__.js');

module.exports = function (config) {
  config.set({
    basePath: '.',
    browsers: ['jsdom'],
    frameworks: ['jasmine'],
    files: [ENTRY_POINT],
    junitReporter: {
      outputDir: '',
      outputFile: 'test-results.xml',
      suite: 'homehub_frontend',
      useBrowserName: false,
    },
    mochaReporter: {
      ignoreSkipped: true,
    },
    preprocessors: {
      [ENTRY_POINT]: ['webpack', 'sourcemap'],
    },
    reporters: ['mocha'],
    singleRun: false,
    webpack: {
      mode: 'development',
      devtool: 'inline-source-map',
      module: {
        rules: [
          {
            test: /\.js?/,
            include: PROJECT_DIR,
            use: ['babel-loader'],
          },
          {
            test: /\.js$/,
            include: TESTS_DIR,
            use: ['babel-loader'],
          },
          {
            test: /\.scss$/,
            use: ['null-loader'],
          },
          {
            test: /\.(png)$/,
            use: ['null-loader'],
          },
          {
            test: /\.(svg)$/,
            include: PROJECT_DIR,
            use: [
              {
                loader: 'babel-loader',
              },
              {
                loader: 'react-svg-loader',
                options: {
                  jsx: true,
                },
              },
            ],
          },
        ],
      },
      resolve: {
        alias: {
          'scss': SCSS_DIR,
          'src': PROJECT_DIR,
          'tests': TESTS_DIR,
          react: path.resolve('node_modules', 'react'),
          'react-dom': path.resolve('node_modules', 'react-dom'),
        },
      },
      externals: {
        'cheerio': 'window',
        'react/addons': 'react',
        'react/lib/ExecutionEnvironment': 'react',
        'react/lib/ReactContext': 'react',
      },
      plugins: [
        new webpack.DefinePlugin({
          HOMEHUB_APP_VERSION: JSON.stringify('1.0.0'),
        }),
      ],
    },
  });
};
