MochaJS +全局变量+ Redux

时间:2016-09-06 12:36:27

标签: javascript mocha redux

我一直在寻找答案,却找不到答案。

这是我的test.js:

process.env.__ENVIRONMENT__ = 'production'; // This does not work
global.__ENVIRONMENT__ = 'production'; // This does not work, either


import assert from 'assert';
import Product from '../../../../app/modules/products/containers/Product';

console.log(Product);

describe('Array', function() {

  describe('#indexOf()', function() {
    it('should return -1 when the value is not present (suplicate)', function() {
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });
});

这是我的容器/ Product.js

import React, { Component } from 'react';
import Helmet from 'react-helmet';
import { connect } from 'react-redux';
import * as ProductActions from '../actions/products';

// @connect(state => { user: state.user })
class Product extends Component {
  // Normal code
}

问题伴随着动作,这是我的/actions/products.js文件:

import {root_url, request_config} from '../../../config'; // This line here is my problem

export const PRODUCT_INVALID = 'PRODUCT_INVALID';
export const PRODUCT_FETCHING = 'PRODUCT_FETCHING';
export const PRODUCT_FETCHED = 'PRODUCT_FETCHED';
export const PRODUCT_FETCH_FAILED = 'PRODUCT_FETCH_FAILED';

function fetchProduct(slug) {

  return (dispatch) => {

    dispatch({ type: PRODUCT_FETCHING, slug: slug });

    return fetch(`${root_url}/p/${slug}`, request_config)
      .then((response) => {
        return response.json();
      })
      .then(
        (result) => dispatch({ type: PRODUCT_FETCHED, slug: slug, result }),
        (error) => {
          console.log(error);
          dispatch({ type: PRODUCT_FETCH_FAILED, slug: slug, error });
        }
      );
  }
}

function shouldFetchProduct(state, slug) {
  console.log("state", state);
  const product = state.products[slug];

  if (!product ||
    product.readyState === PRODUCT_FETCH_FAILED ||
    product.readyState === PRODUCT_INVALID ) {
    return true;
  }

  return false;
}

export function fetchProductIfNeeded(slug) {
  return (dispatch, getState) => {
    if (shouldFetchProduct(getState(), slug)) {
      return dispatch(fetchProduct(slug));
    }
  }
}

如您所见,我对/config/index.js进行了导入,这是文件的内容:

if (__ENVIRONMENT__ === 'production') {
  module.exports = require('./production')
} else {
  module.exports = require('./default')
}

基本上mocha失败了,因为'____ENVIRONMENT____'没有定义。

解决这个问题的最佳方法是什么?

0 个答案:

没有答案