Skip to content

Commit

Permalink
test(vest): add tests for VestTest.cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Dec 26, 2020
1 parent b8746f9 commit ede4587
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/vest/src/core/test/lib/VestTest.js
Expand Up @@ -45,6 +45,10 @@ VestTest.prototype.warn = function () {
this.isWarning = true;
};

/**
* Marks a test as canceled, removes it from the state.
* This function needs to be called within a stateRef context.
*/
VestTest.prototype.cancel = function () {
this.canceled = true;
removePending(this);
Expand Down
53 changes: 53 additions & 0 deletions packages/vest/src/core/test/lib/__tests__/VestTest.test.js
@@ -1,8 +1,19 @@
import runCreateRef from '../../../../../testUtils/runCreateRef';

import VestTest from 'VestTest';
import addTestToState from 'addTestToState';
import context from 'ctx';
import { setPending } from 'pending';
import usePending from 'usePending';
import useTestObjects from 'useTestObjects';

const fieldName = 'unicycle';
const statement = 'I am Root.';

let stateRef;

it.ctx = (str, cb) => it(str, () => context.run({ stateRef }, cb));

describe('VestTest', () => {
let testObject;

Expand Down Expand Up @@ -72,4 +83,46 @@ describe('VestTest', () => {
expect(testObject.valueOf()).toBe(false);
});
});

describe('testObject.cancel', () => {
beforeEach(() => {
stateRef = runCreateRef();

context.run({ stateRef }, () => {
addTestToState(testObject);
});
});
it.ctx('Should remove a testObject from the state', () => {
const [testObjects] = useTestObjects();
expect(testObjects).toEqual(expect.arrayContaining([testObject]));
testObject.cancel();
expect(testObjects).toEqual(expect.not.arrayContaining([testObject]));
});

it.ctx('Should remove a testObject from the pending state', () => {
setPending(testObject);
{
const [{ pending }] = usePending();
expect(pending).toEqual(expect.arrayContaining([testObject]));
}
testObject.cancel();
{
const [{ pending }] = usePending();
expect(pending).toEqual(expect.not.arrayContaining([testObject]));
}
});

it.ctx('Should remove a testObject from the lagging state', () => {
usePending(state => ({ ...state, lagging: [testObject] }));
{
const [{ lagging }] = usePending();
expect(lagging).toEqual(expect.arrayContaining([testObject]));
}
testObject.cancel();
{
const [{ lagging }] = usePending();
expect(lagging).toEqual(expect.not.arrayContaining([testObject]));
}
});
});
});

0 comments on commit ede4587

Please sign in to comment.