<commit_before>import should from 'should';
import sinon from 'sinon';
import { Store, applyMiddleware } from '../src';

// Adapt tests from applyMiddleware spec from Redux
describe('applyMiddleware', function () {
  const portName = 'test';
  // simulates redux-thunk middleware
  const thunk = ({ dispatch, getState }) => next => action =>
    typeof action === 'function' ? action(dispatch, getState) : next(action);

  beforeEach(function () {
    // Mock chrome.runtime API
    global.chrome = {
      runtime: {
        connect() {
          return {
            onMessage: {
              addListener() {
              }
            }
          };
        },
      }
    };
  });

  it('warns when dispatching during middleware setup', () => {
    function dispatchingMiddleware(store) {
      store.dispatch({type:'anything'});
      return next => action => next(action);
    }
    const middleware = [dispatchingMiddleware];

    should.throws(() => {
      applyMiddleware(applyMiddleware(new Store({portName, state: {a: 'a'}}), ...middleware));
    }, Error);
  });

  it('wraps dispatch method with middleware once', () => {
    function test(spyOnMethods) {
      return methods => {
        spyOnMethods(methods);
        return next => action => next(action);
      };
    }

    const spy = sinon.spy();
    const store = applyMiddleware(new Store({portName}), test(spy), thunk);

    store.dispatch(() => ({a: 'a'}));

    spy.calledOnce.should.eql(true);

    spy.args[0][0].should.have.property('getState');
    spy.args[0][0].should.have.property('dispatch');
  });

  it('passes recursive dispatches through the middleware chain', () => {<commit_msg>Remove double application of applyMiddleware<commit_after>import should from 'should';
import sinon from 'sinon';
import { Store, applyMiddleware } from '../src';

// Adapt tests from applyMiddleware spec from Redux
describe('applyMiddleware', function () {
  const portName = 'test';
  // simulates redux-thunk middleware
  const thunk = ({ dispatch, getState }) => next => action =>
    typeof action === 'function' ? action(dispatch, getState) : next(action);

  beforeEach(function () {
    // Mock chrome.runtime API
    global.chrome = {
      runtime: {
        connect() {
          return {
            onMessage: {
              addListener() {
              }
            }
          };
        },
      }
    };
  });

  it('warns when dispatching during middleware setup', () => {
    function dispatchingMiddleware(store) {
      store.dispatch({type:'anything'});
      return next => action => next(action);
    }
    const middleware = [dispatchingMiddleware];

    should.throws(() => {
      applyMiddleware(new Store({portName, state: {a: 'a'}}), ...middleware);
    }, Error);
  });

  it('wraps dispatch method with middleware once', () => {
    function test(spyOnMethods) {
      return methods => {
        spyOnMethods(methods);
        return next => action => next(action);
      };
    }

    const spy = sinon.spy();
    const store = applyMiddleware(new Store({portName}), test(spy), thunk);

    store.dispatch(() => ({a: 'a'}));

    spy.calledOnce.should.eql(true);

    spy.args[0][0].should.have.property('getState');
    spy.args[0][0].should.have.property('dispatch');
  });

  it('passes recursive dispatches through the middleware chain', () => {
{'fraction_non_alphanumeric': 0.15604526503871352, 'fraction_numerical': 0.0023823704586063135, 'mean_word_length': 2.7447045707915274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4712176', 'n_tokens_mistral': 971, 'n_tokens_neox': 968, 'n_words': 226}