<commit_before>/* @flow */
import test from 'ava';
import { call, fork, put, take } from 'redux-saga/effects';
import { testSaga } from '../src';

const identity = value => value;

function* otherSaga(z) {
  yield put({ type: 'OTHER', payload: 'hi' });
  return z;
}

function* mainSaga(x, y, z) {
  try {
    const action = yield take('HELLO');
    yield put({ type: 'ADD', payload: x + y });
    yield call(identity, action);

    yield [
      call(identity, 'parallel call'),
      put({ type: 'PARALLEL_PUT' }),
    ];

    yield fork(otherSaga, z);
  } catch (e) {
    yield put({ type: 'ERROR', payload: e });
  }
}

function* loopingSaga() {
  while (true) {
    const action = yield take('HELLO');
    yield call(identity, action);
  }
}

const x = 40;
const y = 2;
const z = 20;

const action = { type: 'TEST' };

function createSaga() {
  return testSaga(mainSaga, x, y, z);
}

let saga;

test.beforeEach(_ => {
  saga = createSaga();
});

test('follows the saga', () => {
  saga
    .next()
    .take('HELLO')

    .next(action)
    .put({ type: 'ADD', payload: x + y })

    .next()
    .call(identity, action)

    .next()
    .parallel([
      call(identity, 'parallel call'),
      put({ type: 'PARALLEL_PUT' }),
    ])

    .next()
    .fork(otherSaga, z)

    .next()
    .isDone();
});

test('can back up', () => {
  saga
    .next()
    .take('HELLO')

    .back()
    .next()
    .take('HELLO');
});

test('can back up multiple steps', () => {
  saga
    .next()
    .take('HELLO')

    .next()
    .put({ type: 'ADD', payload: x + y })

    .back(2)
    .next()
    .take('HELLO');
});

test('cannot back up at start', t => {
  t.throws(_ => {
    saga.back();
  });
});

test('cannot back up past beginning', t => {
  t.throws(_ => {
    saga
      .next()
      .take('HELLO')
      .back(2);
  });
});

test('can finish the generator early', () => {
  testSaga(loopingSaga)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .finish()
    .next()
    .isDone();
});

test('throws for an incorrect take', t => {
  t.throws(_ => {
    saga.next().take('WORLD');
  });
});

test('throws for an incorrect put', t => {
  t.throws(_ => {
    saga
      .next()
      .take('HELLO')

      .next()
      .put({ type: 'ADD', payload: x + y + 1 });
  });

  t.throws(_ => {
    saga
      .next()
      .take('HELLO')

      .next()
      .put({ type: 'SUBTRACT', payload: x + y });
  });
});<commit_msg>Add another finish test case<commit_after>/* @flow */
/* eslint-disable no-constant-condition */
import test from 'ava';
import { call, fork, put, take } from 'redux-saga/effects';
import { testSaga } from '../src';

const identity = value => value;

function* otherSaga(z) {
  yield put({ type: 'OTHER', payload: 'hi' });
  return z;
}

function* mainSaga(x, y, z) {
  try {
    const action = yield take('HELLO');
    yield put({ type: 'ADD', payload: x + y });
    yield call(identity, action);

    yield [
      call(identity, 'parallel call'),
      put({ type: 'PARALLEL_PUT' }),
    ];

    yield fork(otherSaga, z);
  } catch (e) {
    yield put({ type: 'ERROR', payload: e });
  }
}

function* loopingSaga() {
  while (true) {
    const action = yield take('HELLO');
    yield call(identity, action);
  }
}

const x = 40;
const y = 2;
const z = 20;

const action = { type: 'TEST' };

function createSaga() {
  return testSaga(mainSaga, x, y, z);
}

let saga;

test.beforeEach(_ => {
  saga = createSaga();
});

test('follows the saga', () => {
  saga
    .next()
    .take('HELLO')

    .next(action)
    .put({ type: 'ADD', payload: x + y })

    .next()
    .call(identity, action)

    .next()
    .parallel([
      call(identity, 'parallel call'),
      put({ type: 'PARALLEL_PUT' }),
    ])

    .next()
    .fork(otherSaga, z)

    .next()
    .isDone();
});

test('can back up', () => {
  saga
    .next()
    .take('HELLO')

    .back()
    .next()
    .take('HELLO');
});

test('can back up multiple steps', () => {
  saga
    .next()
    .take('HELLO')

    .next()
    .put({ type: 'ADD', payload: x + y })

    .back(2)
    .next()
    .take('HELLO');
});

test('cannot back up at start', t => {
  t.throws(_ => {
    saga.back();
  });
});

test('cannot back up past beginning', t => {
  t.throws(_ => {
    saga
      .next()
      .take('HELLO')
      .back(2);
  });
});

test('can finish the generator early', () => {
  testSaga(loopingSaga)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .finish()
    .next()
    .isDone();
});

test('can finish with an arg', () => {
  testSaga(loopingSaga)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .take('HELLO')
    .next(action)
    .call(identity, action)
    .next()

    .finish(42)
    .returns(42);
});

test('throws for an incorrect take', t => {
  t.throws(_ => {
    saga.next().take('WORLD');
  });
});

test('throws for an incorrect put', t => {
  t.throws(_ => {
    saga
      .next()
      .take('HELLO')

      .next()
      .put({ type: 'ADD', payload: x + y + 1 });
  });

  t.throws(_ => {
    saga
      .next()
      .take('HELLO')

      .next()
      .put({ type: 'SUBTRACT', payload: x + y });
  });
});
{'fraction_non_alphanumeric': 0.20825216929343013, 'fraction_numerical': 0.003541703559412077, 'mean_word_length': 2.310668229777257, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, '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': '1641659', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2121, 'n_words': 512}