<commit_before>import test from 'tape'

import { runSaga } from '../src'
import { fork, take, put, select } from '../src/effects'
import { emitter } from '../src/internal/channel'
import { runSyncDispatchTest } from './scheduler'

function storeLike(reducer, state) {
  const em = emitter()

  return {
    subscribe: em.subscribe,
    dispatch: (action) => {
      state = reducer(state, action)
      em.emit(action)
      return action
    },
    getState: () => state
  }
}

test('runSaga', assert => {
  assert.plan(1)

  let actual = []
  function reducer(state = {}, action) {
    return action
  }
  const store = storeLike(reducer, {})
  const typeSelector = a => a.type
  const task = runSaga(root(), store)

  store.dispatch({type: 'ACTION-1'})
  store.dispatch({type: 'ACTION-2'})

  function* root() {
    yield [fork(fnA), fork(fnB)]
  }

  function* fnA() {
    actual.push( yield take('ACTION-1') )
    actual.push( yield select(typeSelector) )
    actual.push( yield take('ACTION-2') )
    actual.push( yield select(typeSelector) )
    yield put({type: 'ACTION-3'})
  }

  function* fnB() {
    actual.push( yield take('ACTION-3') )
    actual.push( yield select(typeSelector) )
  }

  const expected = [
    {type: 'ACTION-1'}, 'ACTION-1',
    {type: 'ACTION-2'}, 'ACTION-2',
    {type: 'ACTION-3'}, 'ACTION-3'
  ]

  task.done.then(() =>
    assert.deepEqual(actual, expected,
      'runSaga must connect the provided iterator to the store, and run it'
    )<commit_msg>add test case<commit_after>import test from 'tape'

import { runSaga } from '../src'
import { fork, take, put, select } from '../src/effects'
import { emitter } from '../src/internal/channel'
import { runSyncDispatchTest } from './scheduler'

function storeLike(reducer, state, middleware) {
  const em = emitter()

  return {
    subscribe: em.subscribe,
    dispatch: (action) => {
      state = reducer(state, action)
      em.emit(action)
      return action
    },
    getState: () => state,
    middleware
  }
}

test('runSaga', assert => {
  assert.plan(1)

  let actual = []
  function reducer(state = {}, action) {
    return action
  }
  const forkA = fork(fnA);
  const forkB = fork(fnB);
  const forkThunk = fork(thunk);
  const middleware = (effect) => {
    if (effect === forkThunk) {
      return forkB;
    }
    return effect;
  }
  const store = storeLike(reducer, {}, middleware)
  const typeSelector = a => a.type
  const task = runSaga(root(), store)

  store.dispatch({type: 'ACTION-1'})
  store.dispatch({type: 'ACTION-2'})

  function* root() {
    yield [forkA, forkThunk]
  }

  function* fnA() {
    actual.push( yield take('ACTION-1') )
    actual.push( yield select(typeSelector) )
    actual.push( yield take('ACTION-2') )
    actual.push( yield select(typeSelector) )
    yield put({type: 'ACTION-3'})
  }

  function* fnB() {
    actual.push( yield take('ACTION-3') )
    actual.push( yield select(typeSelector) )
  }

  function thunk() {

  }

  const expected = [
    {type: 'ACTION-1'}, 'ACTION-1',
    {type: 'ACTION-2'}, 'ACTION-2',
    {type: 'ACTION-3'}, 'ACTION-3'
  ]

  task.done.then(() =>
    assert.deepEqual(actual, expected,
      'runSaga must connect the provided iterator to the store, and run it'
    )
{'fraction_non_alphanumeric': 0.16060419235511714, 'fraction_numerical': 0.008014796547472256, 'mean_word_length': 3.0613266583229035, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '2590339', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1079, 'n_words': 272}