Skip to content

Commit

Permalink
breaking: remove validate
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 17, 2020
1 parent 00a5aba commit 0ef8ec8
Show file tree
Hide file tree
Showing 24 changed files with 123 additions and 201 deletions.
4 changes: 1 addition & 3 deletions config/babel/babel.config.js
Expand Up @@ -7,10 +7,8 @@ const env = api => {
};

switch (api.env()) {
case 'development':
conf.targets.node = 'current';
break;
case 'es6':
case 'development':
conf.targets.chrome = 52;
break;
default:
Expand Down
8 changes: 8 additions & 0 deletions config/builds/main.js.tmpl
@@ -0,0 +1,8 @@

'use strict'

if (process.env.NODE_ENV === 'production') {
module.exports = require('./${name}.${format}.production.js')
} else {
module.exports = require('./${name}.${format}.development.js')
}
2 changes: 1 addition & 1 deletion config/builds/vest.js
Expand Up @@ -56,7 +56,7 @@ const plugins = ({ name, format }) => {
replace({
VEST_VERSION: JSON.stringify(version),
LIBRARY_NAME: JSON.stringify(PACKAGE_VEST),
__DEV__: name === ENV_DEVELOPMENT
__DEV__: name === ENV_DEVELOPMENT,
}),
];

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -32,6 +32,7 @@
"mocha": "^8.2.0",
"node-fetch": "^2.6.1",
"onchange": "^7.0.2",
"paraphrase": "^1.8.0",
"prettier": "2.1.2",
"pretty-quick": "^3.1.0",
"rollup": "^2.32.0",
Expand Down
26 changes: 14 additions & 12 deletions packages/vest/package.json
Expand Up @@ -2,24 +2,26 @@
"name": "vest",
"version": "2.2.3",
"description": "Validation Test",
"main": "./vest.es5.js",
"browser": "./vest.es5.js",
"module": "./esm/vest.mjs.js",
"main": "./vest.umd.index.js",
"browser": "./vest.umd.index.js",
"module": "./esm/vest.mjs.production.js",
"exports": {
".": {
"browser": "./vest.es5.js",
"import": "./esm/vest.mjs.js",
"require": "./vest.cjs.js",
"node": "./vest.cjs.js",
"default": "./vest.es5.js"
"browser": "./vest.umd.index.js",
"import": "./esm/vest.mjs.production.js",
"require": "./vest.cjs.production.js",
"node": "./vest.cjs.production.js",
"default": "./vest.cjs.production.js"
},
"./vest.es5.js": "./vest.es5.js",
"./any.js": "./any.js",
"./classNames.js": "./classNames.js",
"./enforceExtended.js": "./enforceExtended.js",
"./vest.development.js": "./vest.development.js",
"./vest.js": "./vest.js",
"./vest.min.js": "./vest.min.js",
"./vest.umd.index.js": "./vest.umd.index.js",
"./vest.umd.production.js": "./vest.umd.production.js",
"./vest.umd.development.js": "./vest.umd.development.js",
"./esm/vest.mjs.production.js": "./esm/vest.mjs.production.js",
"./esm/vest.mjs.development.js": "./esm/vest.mjs.development.js",
"./vest.cjs.production.js": "./vest.cjs.production.js",
"./package.json": "./package.json"
},
"types": "./vest.d.ts",
Expand Down
1 change: 0 additions & 1 deletion packages/vest/src/core/produce/index.js
Expand Up @@ -23,7 +23,6 @@ const cache = createCache(20);
const done = (stateRef, ...args) => {
const { length, [length - 1]: callback, [length - 2]: fieldName } = args;

// TODO: FInd if this line can be removed - at least for sync tests
const output = produce(stateRef);

// If we do not have any tests for current field
Expand Down
1 change: 0 additions & 1 deletion packages/vest/src/core/state/index.js
Expand Up @@ -72,7 +72,6 @@ const createState = name => {
};

const removeCanceled = ({ id }) => {
// TODO: check if this is really needed
delete canceled[id];
};

Expand Down
19 changes: 19 additions & 0 deletions packages/vest/src/core/suite/create/__snapshots__/spec.js.snap
Expand Up @@ -37,3 +37,22 @@ Object {
"warnCount": 0,
}
`;

exports[`Test createSuite module Test suite Arguments allows omitting suite name 1`] = `
Object {
"errorCount": 0,
"getErrors": [Function],
"getErrorsByGroup": [Function],
"getWarnings": [Function],
"getWarningsByGroup": [Function],
"groups": Object {},
"hasErrors": [Function],
"hasErrorsByGroup": [Function],
"hasWarnings": [Function],
"hasWarningsByGroup": [Function],
"name": undefined,
"testCount": 0,
"tests": Object {},
"warnCount": 0,
}
`;
22 changes: 21 additions & 1 deletion packages/vest/src/core/suite/create/spec.js
Expand Up @@ -4,7 +4,27 @@ import { dummyTest } from '../../../../testUtils/testDummy';
import create from '.';

describe('Test createSuite module', () => {
describe.skip('Test Arguments');
describe('Test suite Arguments', () => {
it('allows omitting suite name', () => {
expect(typeof create(Function.prototype)).toBe('function');
expect(create(Function.prototype).name).toBe('validate');
expect(typeof create(Function.prototype).get).toBe('function');
expect(typeof create(Function.prototype).reset).toBe('function');
expect(create(Function.prototype).get()).toMatchSnapshot();
});

it.each([faker.random.word(), null, undefined, 0, 1, true, false, NaN, ''])(
'Throws an error when `tests` callback is not a function',
value => {
expect(() => create(value)).toThrow(
'[Vest]: Suite initialization error. Expected `tests` to be a function.'
);
expect(() => create('suite_name', value)).toThrow(
'[Vest]: Suite initialization error. Expected `tests` to be a function.'
);
}
);
});

describe('Return value', () => {
it('should be a function', () => {
Expand Down
11 changes: 0 additions & 11 deletions packages/vest/src/core/suite/validate/index.js

This file was deleted.

22 changes: 11 additions & 11 deletions packages/vest/src/core/test/spec.js
Expand Up @@ -4,43 +4,43 @@ import runSpec from '../../../testUtils/runSpec';
runSpec(vest => {
let testObject;

const { validate, test, enforce } = vest;
const { create, test, enforce } = vest;
describe("Test Vest's `test` function", () => {
describe('test callbacks', () => {
describe('Warn hook', () => {
it('Should be marked as warning when the warn hook gets called', () => {
validate(faker.random.word(), () => {
create(faker.random.word(), () => {
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() => {
vest.warn();
}
);
});
})();
expect(testObject.isWarning).toBe(true);
});
});

describe('Sync', () => {
it('Should be marked as failed after a thrown error', () => {
validate(faker.random.word(), () => {
create(faker.random.word(), () => {
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() => {
throw new Error();
}
);
});
})();
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});

it('Should be marked as failed for an explicit false return', () => {
validate(faker.random.word(), () => {
create(faker.random.word(), () => {
test(faker.random.word(), faker.lorem.sentence(), () => false);
});
})();
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});
Expand All @@ -49,7 +49,7 @@ runSpec(vest => {
describe('async', () => {
it('Should be marked as failed when a returned promise rejects', () =>
new Promise(done => {
validate(faker.random.word(), () => {
create(faker.random.word(), () => {
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
Expand All @@ -64,7 +64,7 @@ runSpec(vest => {
expect(testObject.failed).toBe(true);
done();
}, 310);
});
})();
}));
});
});
Expand All @@ -73,15 +73,15 @@ runSpec(vest => {
describe('When enforce is returned (not thrown)', () => {
it('Should continue without failing', () =>
new Promise(done => {
validate('form_with_enforce', () => {
create('form_with_enforce', () => {
try {
test('field_with_enforce', () =>
enforce('example').isNotEmpty());
done();
} catch {
/* */
}
});
})();
}));
});
});
Expand Down
23 changes: 9 additions & 14 deletions packages/vest/src/hooks/draft/spec.js
@@ -1,43 +1,38 @@
import faker from 'faker';
import runSpec from '../../../testUtils/runSpec';

runSpec(vest => {
const { test, validate } = vest;

const createSuite = tests => {
validate(faker.random.word(), tests);
};
const { test, create } = vest;

describe('Draft', () => {
it('Should be exposed as a function from vest', () => {
createSuite(() => {
create(() => {
expect(typeof vest.draft).toBe('function');
});
})();
});

it('Should the same object when result is unchanged', () => {
createSuite(() => {
create(() => {
const a = vest.draft();
const b = vest.draft();
expect(a).toBe(b);
});
})();
});

it('Should only contain has/get callbacks', () => {
createSuite(() => {
create(() => {
expect(typeof vest.draft().hasErrors).toBe('function');
expect(typeof vest.draft().getErrors).toBe('function');
expect(typeof vest.draft().hasWarnings).toBe('function');
expect(typeof vest.draft().getWarnings).toBe('function');
expect(typeof vest.draft().done).not.toBe('function');
expect(typeof vest.draft().cancel).not.toBe('function');
});
})();
});

it('Should contain intermediate test result', () => {
// This test is so long because it tests `draft` throughout
// A suite's life cycle, both as an argument, and as an import
createSuite(() => {
create(() => {
expect(vest.draft().errorCount).toBe(0);
expect(vest.draft().warnCount).toBe(0);
expect(vest.draft().hasErrors()).toBe(false);
Expand Down Expand Up @@ -73,7 +68,7 @@ runSpec(vest => {
expect(vest.draft().hasErrors()).toBe(true);
expect(vest.draft().hasWarnings()).toBe(true);
expect(vest.draft().hasWarnings('field4')).toBe(false);
});
})();
});
});
});
15 changes: 12 additions & 3 deletions packages/vest/src/hooks/group/index.js
@@ -1,5 +1,5 @@
import { throwError } from '../../../../n4s/src/lib';
import context from '../../core/context';
import validateSuiteParams from '../../lib/validateSuiteParams';
import { isGroupExcluded } from '../exclusive';

/**
Expand All @@ -21,8 +21,17 @@ const registerGroup = groupName => {
* @param {Function} tests
*/
const group = (groupName, tests) => {
// TODO: Replace with something local
validateSuiteParams('group', groupName, tests);
if (typeof groupName !== 'string') {
throwError(
`group initialization error. Expected "${groupName}" to be a string.`
);
}

if (typeof tests !== 'function') {
throwError(
`group initialization error. Expected "${tests}" to be a function.`
);
}

const { stateRef } = context.use();
const state = stateRef.current();
Expand Down
14 changes: 7 additions & 7 deletions packages/vest/src/hooks/warn/spec.js
Expand Up @@ -6,38 +6,38 @@ import context from '../../core/context';
import { ERROR_HOOK_CALLED_OUTSIDE } from '../constants';
import { ERROR_OUTSIDE_OF_TEST } from './constants';

const { validate, test, warn } = vest;
const { create, test, warn } = vest;

describe('warn hook', () => {
describe('When currentTest exists', () => {
it('Should set isWarning to true', () => {
let beforeWarn, afterWarn;
validate(faker.random.word(), () => {
create(faker.random.word(), () => {
test(faker.lorem.word(), faker.lorem.sentence(), () => {
beforeWarn = context.use().currentTest.isWarning;
warn();
afterWarn = context.use().currentTest.isWarning;
});
});
})();

expect(beforeWarn).toBe(false);
expect(afterWarn).toBe(true);
});
});

describe('Error handling', () => {
let warn, validate;
let warn, create;

beforeEach(() => {
({ validate, warn } = require('../..'));
({ create, warn } = require('../..'));
});

it('Should throw error when currentTest is not present', () => {
const done = jest.fn();
validate(faker.random.word(), () => {
create(faker.random.word(), () => {
expect(warn).toThrow(ERROR_OUTSIDE_OF_TEST);
done();
});
})();
expect(done).toHaveBeenCalled();
});

Expand Down
2 changes: 0 additions & 2 deletions packages/vest/src/index.js
@@ -1,7 +1,6 @@
import enforce from '../../n4s/src/enforce';
import context from './core/context';
import create from './core/suite/create';
import validate from './core/suite/validate';
import test from './core/test';
import { draft, only, skip, warn, group } from './hooks';

Expand All @@ -17,6 +16,5 @@ export default {
runWithContext: context.run,
skip,
test,
validate,
warn,
};

0 comments on commit 0ef8ec8

Please sign in to comment.