Skip to content

Commit

Permalink
patch: reduce built size (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 23, 2020
1 parent aeb5589 commit 797fe4e
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 111 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
'no-implicit-globals': 2,
'no-lonely-if': 2,
'no-multi-spaces': 1,
'no-prototype-builtins': 0,
'no-trailing-spaces': [2, { ignoreComments: false }],
'no-unneeded-ternary': 2,
'no-unused-expressions': 2,
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- yarn test
- yarn lint
- stage: Release
if: type = push AND fork = false
if: branch = release AND type = push AND fork = false
install: yarn install --frozen-lockfile
script:
- echo //registry.npmjs.org/:_authToken=$NPM_TOKEN > ~/.npmrc
Expand Down
119 changes: 58 additions & 61 deletions packages/vest/src/core/produce/genTestsSummary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,90 +9,87 @@ import {
} from '../../test/lib/VestTest/constants';

/**
*
* @param {Object} summaryKey The container for the test result data
* @param {VestTest} testObject
* @returns {Object} Test result summary
* Reads the testObjects list and gets full validation result from it.
*/
const genTestObject = (summaryKey, testObject) => {
const { fieldName, isWarning, failed, statement } = testObject;
const genTestsSummary = () => {
const [testObjects] = useTestObjects();
const [suiteIdState] = useSuiteId();

summaryKey[fieldName] = summaryKey[fieldName] || {
[SEVERITY_COUNT_WARN]: 0,
[SEVERITY_COUNT_ERROR]: 0,
[TEST_COUNT]: 0,
const summary = {
tests: {},
groups: {},
name: suiteIdState.name,
};

const testKey = summaryKey[fieldName];
testObjects.forEach(testObject => {
const { fieldName, groupName } = testObject;

summaryKey[fieldName][TEST_COUNT]++;
summary.tests[fieldName] = genTestObject(summary.tests, testObject);

if (failed) {
if (isWarning) {
testKey[SEVERITY_COUNT_WARN]++;
if (statement) {
testKey[SEVERITY_GROUP_WARN] = (
testKey[SEVERITY_GROUP_WARN] || []
).concat(statement);
}
} else {
testKey[SEVERITY_COUNT_ERROR]++;
if (statement) {
testKey[SEVERITY_GROUP_ERROR] = (
testKey[SEVERITY_GROUP_ERROR] || []
).concat(statement);
}
if (groupName) {
summary.groups[groupName] = summary.groups[groupName] || {};
summary.groups[groupName][fieldName] = genTestObject(
summary.groups[groupName],
testObject
);
}
}
});

return testKey;
return countFailures(summary);
};

/**
* Counts the failed tests and adds global counters
* @param {Object} state
* @param {Object} summary (generated by genTestsSummary)
*/
export const countFailures = state => {
state[SEVERITY_COUNT_ERROR] = 0;
state[SEVERITY_COUNT_WARN] = 0;
state[TEST_COUNT] = 0;
for (const test in state.tests) {
state[SEVERITY_COUNT_ERROR] += state.tests[test][SEVERITY_COUNT_ERROR];
state[SEVERITY_COUNT_WARN] += state.tests[test][SEVERITY_COUNT_WARN];
state[TEST_COUNT] += state.tests[test][TEST_COUNT];
export const countFailures = summary => {
summary[SEVERITY_COUNT_ERROR] = 0;
summary[SEVERITY_COUNT_WARN] = 0;
summary[TEST_COUNT] = 0;

for (const test in summary.tests) {
summary[SEVERITY_COUNT_ERROR] += summary.tests[test][SEVERITY_COUNT_ERROR];
summary[SEVERITY_COUNT_WARN] += summary.tests[test][SEVERITY_COUNT_WARN];
summary[TEST_COUNT] += summary.tests[test][TEST_COUNT];
}
return state;
return summary;
};

export default genTestsSummary;

/**
* Reads the testObjects list and gets full validation result from it.
* @param {Object} state
*
* @param {Object} summaryKey The container for the test result data
* @param {VestTest} testObject
* @returns {Object} Test result summary
*/
const genTestsSummary = () => {
const [testObjects] = useTestObjects();
const [suiteIdState] = useSuiteId();
const genTestObject = (summaryKey, testObject) => {
const { fieldName, isWarning, failed, statement } = testObject;

const state = {
tests: {},
groups: {},
name: suiteIdState.name,
summaryKey[fieldName] = summaryKey[fieldName] || {
[SEVERITY_COUNT_WARN]: 0,
[SEVERITY_COUNT_ERROR]: 0,
[TEST_COUNT]: 0,
};

testObjects.forEach(testObject => {
const { fieldName, groupName } = testObject;
const testKey = summaryKey[fieldName];

state.tests[fieldName] = genTestObject(state.tests, testObject);
summaryKey[fieldName][TEST_COUNT]++;

if (groupName) {
state.groups[groupName] = state.groups[groupName] || {};
state.groups[groupName][fieldName] = genTestObject(
state.groups[groupName],
testObject
);
const addTo = (count, group) => {
testKey[count]++;
if (statement) {
testKey[group] = (testKey[group] || []).concat(statement);
}
});
};

return state;
};
if (failed) {
if (isWarning) {
addTo(SEVERITY_COUNT_WARN, SEVERITY_GROUP_WARN);
} else {
addTo(SEVERITY_COUNT_ERROR, SEVERITY_GROUP_ERROR);
}
}

export default genTestsSummary;
return testKey;
};
36 changes: 19 additions & 17 deletions packages/vest/src/core/produce/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SEVERITY_GROUP_ERROR,
SEVERITY_GROUP_WARN,
} from '../test/lib/VestTest/constants';
import genTestsSummary, { countFailures } from './genTestsSummary';
import genTestsSummary from './genTestsSummary';
import get from './get';
import getByGroup from './getByGroup';
import has from './has';
Expand Down Expand Up @@ -36,7 +36,7 @@ const done = (...args) => {
}

const cb = context.bind({ stateRef }, () =>
callback(produce({ draft: true }))
callback(produce(/*isDraft:*/ true))
);

// is suite finished || field name exists, and test is finished
Expand All @@ -63,19 +63,18 @@ const done = (...args) => {
};

/**
* @param {Object} Options
* @param {boolean} [Options.draft]
* @param {boolean} [isDraft]
* @returns Vest output object.
*/

const produce = ({ draft } = {}) => {
const produce = isDraft => {
const { stateRef } = context.use();
const [testObjects] = useTestObjects();
return cache(
[testObjects, draft],
[testObjects, isDraft],
context.bind({ stateRef }, () =>
Object.defineProperties(
countFailures(genTestsSummary()),
genTestsSummary(),
[
['hasErrors', context.bind({ stateRef }, has, SEVERITY_GROUP_ERROR)],
['hasWarnings', context.bind({ stateRef }, has, SEVERITY_GROUP_WARN)],
Expand All @@ -98,16 +97,19 @@ const produce = ({ draft } = {}) => {
context.bind({ stateRef }, getByGroup, SEVERITY_GROUP_WARN),
],
]
.concat(draft ? [] : [['done', context.bind({ stateRef }, done)]])
.reduce((properties, [name, value]) => {
properties[name] = {
configurable: true,
enumerable: true,
value,
writeable: true,
};
return properties;
}, {})
.concat(isDraft ? [] : [['done', context.bind({ stateRef }, done)]])
.reduce(
(properties, [name, value]) => (
(properties[name] = {
configurable: true,
enumerable: true,
value,
writeable: true,
}),
properties
),
{}
)
)
)
);
Expand Down
10 changes: 5 additions & 5 deletions packages/vest/src/core/produce/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('module: produce', () => {
describe('When draft: true', () => {
beforeEach(() => {
runCreateSuite('suiteName');
produced = runProduce({ draft: true });
produced = runProduce(/*isDraft:*/ true);
});

it.each(DRAFT_EXCLUDED_METHODS)(
Expand Down Expand Up @@ -726,7 +726,7 @@ describe('module: produce', () => {
draft = vest.draft();
getStateFromContext();
expect(draft).toBe(vest.draft());
expect(produce({ draft: true })).toBe(validate.get());
expect(produce(/*isDraft:*/ true)).toBe(validate.get());
testDummy(vest).failing();
expect(produce()).not.toBe(validate.get());
expect(draft).not.toBe(vest.draft());
Expand All @@ -742,15 +742,15 @@ describe('module: produce', () => {
});
expect(control).toHaveBeenCalledTimes(1);
context.run({ stateRef }, () => {
expect(produce({ draft: true })).toBe(validate.get());
expect(produce(/*isDraft:*/ true)).toBe(validate.get());
expect(res).not.toBe(draft);
expect(res).not.toBe(produce({ draft: true }));
expect(res).not.toBe(produce(/*isDraft:*/ true));
expect(draft).not.toBe(validate.get());
res = validate().done(result => {
expect(result).toBe(validate.get());
});
expect(res).not.toBe(draft);
expect(res).not.toBe(produce({ draft: true }));
expect(res).not.toBe(produce(/*isDraft:*/ true));
expect(control).toHaveBeenCalledTimes(2);
});
}
Expand Down
10 changes: 4 additions & 6 deletions packages/vest/src/core/state/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import callEach from '../../lib/callEach';
import isFunction from '../../lib/isFunction';
import context from '../context';

Expand All @@ -10,7 +11,7 @@ export default (function createState() {
// Register state handler
if (reg) {
key = reg.key;
if (!Object.prototype.hasOwnProperty.call(stateRef.current(), key)) {
if (!stateRef.current().hasOwnProperty(key)) {
stateRef.set(
key,
isFunction(initialValue)
Expand Down Expand Up @@ -50,17 +51,14 @@ export default (function createState() {
}

function set(key, value) {
state = {
...state,
[key]: value,
};
Object.assign(state, { [key]: value });
}

function reset() {
state.length = 0;

state = {};
registeredHandlers.forEach(fn => fn());
callEach(registeredHandlers);
}

const stateRef = {
Expand Down
9 changes: 7 additions & 2 deletions packages/vest/src/core/suite/create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const createSuite = (...args) => {
useTestObjects,
});

/*
context.bind returns our `validate` function
We then wrap it with defineProperties to add
the `get`, and `reset` functions.
*/
return Object.defineProperties(
context.bind({ stateRef }, function () {
const [previousTestObjects] = useTestObjects();
Expand All @@ -43,9 +49,8 @@ const createSuite = (...args) => {
}),
{
get: {
value: context.bind({ stateRef }, produce, { draft: true }),
value: context.bind({ stateRef }, produce, /*isDraft:*/ true),
},
name: { value: 'validate' },
reset: { value: stateRef.reset },
}
);
Expand Down
5 changes: 0 additions & 5 deletions packages/vest/src/core/suite/create/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe('Test createSuite module', () => {
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();
Expand All @@ -30,10 +29,6 @@ describe('Test createSuite module', () => {
it('should be a function', () => {
expect(typeof create('suiteName', noop)).toBe('function');
});

test('returned function name is `validate`', () => {
expect(create('boop', noop).name).toBe('validate');
});
});

describe('When returned function is invoked', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/vest/src/core/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ const test = (fieldName, ...args) => {
return testObject;
};

/**
* Caches, or returns an already cached test call
* @param {String} fieldName Name of the field to test.
* @param {String} [statement] The message returned in case of a failure.
* @param {function} testFn The actual test callback.
* @param {any[]} deps Dependency array.
* @return {VestTest} A VestTest instance.
*/
test.memo = (fieldName, ...args) => {
cache = cache || createCache(100);

Expand All @@ -119,7 +128,7 @@ test.memo = (fieldName, ...args) => {
return cache(dependencies, () => test(fieldName, msg, testFn));
}

const { 1: testObject } = cached;
const [, testObject] = cached;

if (isExcluded(testObject)) {
return testObject;
Expand Down

0 comments on commit 797fe4e

Please sign in to comment.