<commit_before>import chai, { expect } from 'chai'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
import compose from 'lodash/fp/compose'
import map from 'lodash/fp/map'

import promiseBindMiddleware from '../src/index'

chai.use(sinonChai)

describe('#Redux Promise Bind Middleware', () => {
  let promiseMockSuccess
  let promiseMockError

  const mocks = {}
  mocks.doNext = () => null
  mocks.doDispatch = () => mocks.doNext
  mocks.promise = () => ({
    then: (success, error) => {
      promiseMockSuccess = success
      promiseMockError = error
    },
  })

  const nextSpy = sinon.spy(mocks, 'doNext')
  const dispatchSpy = sinon.spy(mocks, 'doDispatch')
  const promiseSpy = sinon.spy(mocks, 'promise')

  const dispatchAction = promiseBindMiddleware({ dispatch: mocks.doDispatch })(mocks.doNext)

  beforeEach(() => {
    nextSpy.reset()
    dispatchSpy.reset()
    promiseSpy.reset()
  })

  it('should trigger next middleware if promise doesn\'t exist', () => {
    dispatchAction({
      type: 'MY_ACTION',
    })

    expect(nextSpy).to.have.been.calledOnce
    expect(nextSpy).to.have.been.calledWith({
      type: 'MY_ACTION',
    })
  })

  it('should trigger promise function', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
      promiseArg: ['test1', 'test2'],
    })

    expect(promiseSpy).to.have.been.calledOnce
    expect(promiseSpy).to.have.been.calledWith('test1', 'test2')
  })

  it('should trigger promise function with bind', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise.bind(null, 'test3', 'test4'),
    })

    expect(promiseSpy).to.have.been.calledOnce
    expect(promiseSpy).to.have.been.calledWith('test3', 'test4')
  })

  it('should dispatch proper actions on success', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
      metadata: { foo: 'bar' },
    })

    expect(dispatchSpy).to.have.been.calledOnce
    expect(dispatchSpy).to.have.been.calledWith({
      type: 'MY_ACTION_START',
      metadata: { foo: 'bar' },
    })

    promiseMockSuccess({ data: 'test' })

    expect(dispatchSpy).to.have.been.calledTwice
    expect(dispatchSpy.secondCall).to.have.been.calledWith({
      type: 'MY_ACTION_SUCCESS',
      metadata: { foo: 'bar' },
      data: { data: 'test' },
    })
  })

  it('should dispatch proper actions on error', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
      metadata: { foo: 'bar' },
    })

    expect(dispatchSpy).to.have.been.calledOnce
    expect(dispatchSpy).to.have.been.calledWith({
      type: 'MY_ACTION_START',
      metadata: { foo: 'bar' },
    })

    promiseMockError({ data: 'test' })

    expect(dispatchSpy).to.have.been.calledTwice
    expect(dispatchSpy.secondCall).to.have.been.calledWith({
      type: 'MY_ACTION_ERROR',
      metadata: { foo: 'bar' },
      data: { data: 'test' },
    })
  })

  xit('should chain promises', () => {
    // .then() after dispatch
  })

  xit('should trigger promises in predefined queue', () => {
    promiseBindMiddleware({
      type: 'MY_ACTION1',
      promise: mocks.promise.bind(null, 'myParam1'),
      group: 'myGroup',
      logic: 'inQueue',
    })

    promiseBindMiddleware({
      type: 'MY_ACTION2',
      promise: mocks.promise.bind(null, 'myParam2'),
      group: 'myGroup',
      logic: 'inQueue',<commit_msg>Chaining promise after dispatch<commit_after>import chai, { expect } from 'chai'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
import compose from 'lodash/fp/compose'
import map from 'lodash/fp/map'

import promiseBindMiddleware from '../src/index'

chai.use(sinonChai)

describe('#Redux Promise Bind Middleware', () => {
  let promiseMockSuccess
  let promiseMockError

  const mocks = {}
  mocks.doNext = () => null
  mocks.doDispatch = () => mocks.doNext
  mocks.promise = () => ({
    then: (success, error) => {
      let successCallback = () => null
      let errorCallback = () => null

      promiseMockSuccess = (...attr) => {
        successCallback(success(...attr))
      }
      promiseMockError = (...attr) => {
        errorCallback(error(...attr))
      }

      return { then: (cb, ex) => { successCallback = cb, errorCallback = ex } }
    },
  })

  const nextSpy = sinon.spy(mocks, 'doNext')
  const dispatchSpy = sinon.spy(mocks, 'doDispatch')
  const promiseSpy = sinon.spy(mocks, 'promise')

  const dispatchAction = promiseBindMiddleware({ dispatch: mocks.doDispatch })(mocks.doNext)

  beforeEach(() => {
    nextSpy.reset()
    dispatchSpy.reset()
    promiseSpy.reset()
  })

  it('should trigger next middleware if promise doesn\'t exist', () => {
    dispatchAction({
      type: 'MY_ACTION',
    })

    expect(nextSpy).to.have.been.calledOnce
    expect(nextSpy).to.have.been.calledWith({
      type: 'MY_ACTION',
    })
  })

  it('should trigger promise function', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
      promiseArg: ['test1', 'test2'],
    })

    expect(promiseSpy).to.have.been.calledOnce
    expect(promiseSpy).to.have.been.calledWith('test1', 'test2')
  })

  it('should trigger promise function with bind', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise.bind(null, 'test3', 'test4'),
    })

    expect(promiseSpy).to.have.been.calledOnce
    expect(promiseSpy).to.have.been.calledWith('test3', 'test4')
  })

  it('should dispatch proper actions on success', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
      metadata: { foo: 'bar' },
    })

    expect(dispatchSpy).to.have.been.calledOnce
    expect(dispatchSpy).to.have.been.calledWith({
      type: 'MY_ACTION_START',
      metadata: { foo: 'bar' },
    })

    promiseMockSuccess({ data: 'test' })

    expect(dispatchSpy).to.have.been.calledTwice
    expect(dispatchSpy.secondCall).to.have.been.calledWith({
      type: 'MY_ACTION_SUCCESS',
      metadata: { foo: 'bar' },
      data: { data: 'test' },
    })
  })

  it('should dispatch proper actions on error', () => {
    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
      metadata: { foo: 'bar' },
    })

    expect(dispatchSpy).to.have.been.calledOnce
    expect(dispatchSpy).to.have.been.calledWith({
      type: 'MY_ACTION_START',
      metadata: { foo: 'bar' },
    })

    promiseMockError({ data: 'test' })

    expect(dispatchSpy).to.have.been.calledTwice
    expect(dispatchSpy.secondCall).to.have.been.calledWith({
      type: 'MY_ACTION_ERROR',
      metadata: { foo: 'bar' },
      data: { data: 'test' },
    })
  })

  it('should chain promises after success', () => {
    const thenSpy = sinon.spy()

    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
    }).then(thenSpy)

    promiseMockSuccess({ data: 'test' })

    expect(thenSpy).to.have.been.calledOnce
    expect(thenSpy).to.have.been.calledWith({ data: 'test' })
  })

  it('should chain promises after failure', () => {
    const thenSpy = sinon.spy()

    dispatchAction({
      type: 'MY_ACTION',
      promise: mocks.promise,
    }).then(null, thenSpy)

    promiseMockError('error!')

    expect(thenSpy).to.have.been.calledOnce
    expect(thenSpy).to.have.been.calledWith('error!')
  })

  xit('should trigger promises in predefined queue', () => {
    promiseBindMiddleware({
      type: 'MY_ACTION1',
      promise: mocks.promise.bind(null, 'myParam1'),
      group: 'myGroup',
      logic: 'inQueue',
    })

    promiseBindMiddleware({
      type: 'MY_ACTION2',
      promise: mocks.promise.bind(null, 'myParam2'),
      group: 'myGroup',
      logic: 'inQueue',
{'fraction_non_alphanumeric': 0.15786057380241464, 'fraction_numerical': 0.0031156692197844994, 'mean_word_length': 3.0654353562005277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '1956495', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2580, 'n_words': 483}