Skip to content

Latest commit

 

History

History
454 lines (366 loc) · 12.7 KB

docs.md

File metadata and controls

454 lines (366 loc) · 12.7 KB

TOC

Iteration assertion

.be.iteration

does not fail if tested object has done and value keys.

const iteration = { done: false, value: null };
expect(iteration).to.be.an.iteration;
iteration.should.be.an.iteration;

.not.be.iteration

does not fail if tested object does not have done or value key.

const notIterationObject = { value: 42 };
expect(null).not.to.be.an.iteration;
expect(undefined).not.to.be.an.iteration;
expect(42).not.to.be.an.iteration;
expect(notIterationObject).not.to.be.an.iteration;
notIterationObject.should.not.be.an.iteration;

Effect assertion

.be.effect

does not fail if tested object is redux-saga effect.

expect(take('ACTION')).to.be.an.effect;
expect(takem('ACTION')).to.be.an.effect;
expect(put({ type: 'ACTION' })).to.be.an.effect;
expect(race([])).to.be.an.effect;
expect(call(noop)).to.be.an.effect;
expect(apply({}, noop)).to.be.an.effect;
expect(cps(noop)).to.be.an.effect;
expect(fork(noop)).to.be.an.effect;
expect(spawn(noop)).to.be.an.effect;
expect(join(mockTask)).to.be.an.effect;
expect(cancel(mockTask)).to.be.an.effect;
expect(actionChannel('ACTION')).to.be.an.effect;

take('ACTION').should.be.an.effect;
takem('ACTION').should.be.an.effect;
put({ type: 'ACTION' }).should.be.an.effect;
race([]).should.be.an.effect;
call(noop).should.be.an.effect;
apply({}, noop).should.be.an.effect;
cps(noop).should.be.an.effect;
fork(noop).should.be.an.effect;
spawn(noop).should.be.an.effect;
join(mockTask).should.be.an.effect;
cancel(mockTask).should.be.an.effect;
actionChannel('ACTION').should.be.an.effect;

not.be.an.effect

does not fail if tested object is not a redux-saga effect.

const notEffectObject = { value: 42 };

expect(null).not.to.be.an.effect;
expect(undefined).not.to.be.an.effect;
expect(42).not.to.be.an.effect;
expect(notEffectObject).not.to.be.an.effect;

notEffectObject.should.not.be.an.effect;

Effects

take(pattern)/takem(pattern)

.be.takeEffect

does not fail if tested object is take effect.

const effect = take('ACTION');

expect(effect).to.be.takeEffect;
effect.should.be.takeEffect;

.not.be.takeEffect

does not fail if tested object is not take effect.

const notTakeEffectObject = put({ type: 'ACTION' });

expect(null).not.to.be.an.takeEffect;
expect(undefined).not.to.be.an.takeEffect;
expect(42).not.to.be.an.takeEffect;
expect(notTakeEffectObject).not.to.be.an.takeEffect;

notTakeEffectObject.should.not.be.an.takeEffect;

.to.take(pattern)

does not fail if tested object is take effect yielded from generator.

function* testSaga() {
  yield take('*');
  yield take();
  yield take('ACTION');
  yield take(['ACTION1', 'ACTION2', 'ACTION3']);
  yield take(action => action.type.startsWith('IMPORTANT'));
}

const gen = testSaga();
const steps = [];
let next = gen.next();

while (!next.done) {
  steps.push(next);
  next = gen.next();
}

expect(steps[0]).to.take('ACTION');
expect(steps[1]).to.take('ACTION');

expect(steps[0]).to.take(['ACTION1', 'ACTION2', 'ACTION3']);
expect(steps[1]).to.take(['ACTION1', 'ACTION2', 'ACTION3']);

expect(steps[2]).to.take('ACTION');
steps[2].should.take('ACTION');

expect(steps[3]).to.take(['ACTION1', 'ACTION2', 'ACTION3']);
expect(steps[3]).to.take(['ACTION1', 'ACTION2']);
expect(steps[3]).to.take('ACTION1');
steps[3].should.take(['ACTION1', 'ACTION2', 'ACTION3']);
steps[3].should.take(['ACTION1', 'ACTION2']);
steps[3].should.take('ACTION1');

expect(steps[4]).to.take('IMPORTANT_ACTION');
steps[4].should.take('IMPORTANT_ACTION');

.not.to.take(pattern)

does not fail if tested object is not take effect yielded from generator.

function* testSaga() {
  yield take('ACTION');
  yield take(['ACTION1', 'ACTION2', 'ACTION3']);
  yield take(action => action.type.startsWith('IMPORTANT'));
}

const gen = testSaga();

const first = gen.next();
const second = gen.next();
const third = gen.next();

expect(first).not.to.take('NOT_ACTION');
first.should.not.take('NOT_ACTION');

expect(second).not.to.take('WRONG_ACTION');
expect(second).not.to.take(['FOO', 'BAR', 'BAZ']);
second.should.not.take('WRONG_ACTION');
second.should.not.take(['FOO', 'BAR', 'BAZ']);

expect(third).not.to.take('NOT_IMPORTANT_ACTION');
third.should.not.take('NOT_IMPORTANT_ACTION');

Effects

put(action)

.be.putEffect

does not fail if tested object is put effect.

const effect = put({ type: 'ACTION' });

expect(effect).to.be.putEffect;
effect.should.be.putEffect;

.not.be.putEffect

does not fail if tested object is not put effect.

const notPutEffectObject = take('ACTION');

expect(null).not.to.be.an.putEffect;
expect(undefined).not.to.be.an.putEffect;
expect(42).not.to.be.an.putEffect;
expect(notPutEffectObject).not.to.be.an.putEffect;

notPutEffectObject.should.not.be.an.putEffect;

.to.put(action)

does not fail if tested object is put effect with correct action yielded from generator.

function* testSaga() {
  yield put({ type: 'ACTION' });
  yield put({ type: 'ACTION', payload: 42 });
}

const gen = testSaga();
const first = gen.next();
const second = gen.next();

expect(first).to.put({ type: 'ACTION' });
expect(second).to.put({ type: 'ACTION', payload: 42 });

.not.to.put(action)

does not fail if tested object is put effect with incorrect action.

function* testSaga() {
  yield put({ type: 'ACTION' });
  yield put({ type: 'OTHER_ACTION', payload: 42 });
}

const gen = testSaga();
const first = gen.next();
const second = gen.next();

expect(first).not.to.put({ type: 'OTHER_ACTION' });
expect(second).not.to.put({ type: 'OTHER_ACTION' }); // no payload

Effects

join/cancel(task)

.be.joinEffect/cancelEffect

does not fail if tested object is join/cancel effect.

const joinEffect = join(mockTask);
const cancelEffect = cancel(mockTask);

expect(joinEffect).to.be.joinEffect;
expect(cancelEffect).to.be.cancelEffect;

.not.be.joinEffect/cancelEffect

does not fail if tested object is not join/cancel effect.

const invalidEffect = take('ACTION');

expect(null).not.to.be.an.joinEffect;
expect(undefined).not.to.be.an.joinEffect;
expect(42).not.to.be.an.joinEffect;
expect(invalidEffect).not.to.be.an.joinEffect;

invalidEffect.should.not.be.an.joinEffect;

expect(null).not.to.be.an.cancelEffect;
expect(undefined).not.to.be.an.cancelEffect;
expect(42).not.to.be.an.cancelEffect;
expect(invalidEffect).not.to.be.an.cancelEffect;

invalidEffect.should.not.be.an.cancelEffect;

.to.join/cancel(task)

does not fail if tested object is join/cancel effect with correct task yielded from generator.

function* testSaga() {
  yield join(mockTask);
  yield cancel(otherMockTask);
  // docs-ignore-start
  yield join(otherMockTask);
  yield cancel(mockTask);
  // docs-ignore-end
}

const gen = testSaga();
const first = gen.next();
const second = gen.next();

expect(first).to.join(mockTask);
expect(second).to.cancel(otherMockTask);

.not.to.join/cancel(task)

does not fail if tested object is join/cancel effect with incorrect task.

function* testSaga() {
  yield join(otherMockTask);
  yield join(mockTask);
  yield cancel(otherMockTask);
  yield cancel(mockTask);
}

const gen = testSaga();
const first = gen.next();
const second = gen.next();
const third = gen.next();
const fourth = gen.next();

expect(first).not.to.join(mockTask);
expect(second).not.to.join(otherMockTask);
expect(third).not.to.cancel(mockTask);
expect(fourth).not.to.cancel(otherMockTask);

Effects

select(pattern)

.be.selectEffect

does not fail if tested object is select effect.

const effect = select(noop);
expect(effect).to.be.selectEffect;

.not.be.selectEffect

does not fail if tested object is not select effect.

const notTakeEffectObject = put({ type: 'ACTION' });

expect(null).not.to.be.an.selectEffect;
expect(undefined).not.to.be.an.selectEffect;
expect(42).not.to.be.an.selectEffect;
expect(notTakeEffectObject).not.to.be.an.selectEffect;

.to.select(selector)

does not fail if tested object is select effect yielded from generator.

function* testSaga() {
  yield select(noop);
}
const next = testSaga().next();
expect(next).to.select(noop);
// dpcs-ingore-start
expect(() => {
  expect(next).to.select(x => x);
}).to.throw(AssertionError);

.not.to.select(selector)

does not fail if tested object is select effect with wrong selector.

const selector = state => state.prop;

function* testSaga() {
  yield select(selector);
}
const next = testSaga().next();
expect(next).not.to.select(noop);

.to.select(selector).withArgs(...args)

does not fail if tested object is select effect and it was with called with correct arguments.

function* testSaga() {
  yield select(noop, 1);
  yield select(noop, 1, 2);
  yield select(noop, 1, 2, 3);
}

const gen = testSaga();
const steps = [];
let next = gen.next();

while (!next.done) {
  steps.push(next);
  next = gen.next();
}

expect(steps[0]).to.select(noop).withArgs(1);
expect(steps[1]).to.select(noop).withArgs(1, 2);
expect(steps[2]).to.select(noop).withArgs(1, 2, 3);

.to.select(selector).withoutArgs(..args)

does not fail if tested object is select effect and it was called without args.

function* testSaga() {
  yield select(noop);
}

const next = testSaga().next();

expect(next).to.select(noop).withoutArgs();