Skip to content

Commit

Permalink
patch: Remove state history
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 17, 2020
1 parent 699b293 commit 65a7468
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 88 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
globals: {
VEST_VERSION: true,
LIBRARY_NAME: true,
ENV_DEVELOPMENT : true
ENV_DEVELOPMENT: true,
},
ignorePatterns: ['playground'],
parser: 'babel-eslint',
Expand All @@ -34,7 +34,6 @@ module.exports = {
plugins: ['jest', 'spellcheck'],

rules: {
'arrow-body-style': [2, 'as-needed'],
'import/newline-after-import': 2,
'import/no-self-import': 2,
'import/no-useless-path-segments': 2,
Expand Down
7 changes: 3 additions & 4 deletions packages/vest/src/core/produce/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ const done = (...args) => {

useTestCallbacks(current => {
if (fieldName) {
current.fieldCallbacks[fieldName] = [].concat(
...(current.fieldCallbacks[fieldName] || []),
cb
);
current.fieldCallbacks[fieldName] = (
current.fieldCallbacks[fieldName] || []
).concat(cb);
} else {
current.doneCallbacks.push(cb);
}
Expand Down
65 changes: 23 additions & 42 deletions packages/vest/src/core/state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@ export default (function createState() {
function update(patcher) {
const { stateRef } = context.use();
const currentState = stateRef.current();
const prevState = stateRef.prev();

stateRef.set(
key,
typeof patcher === 'function'
? patcher(currentState?.[key], prevState?.[key])
: patcher
typeof patcher === 'function' ? patcher(currentState?.[key]) : patcher
);
}

return [stateRef.current()[key], update];
}

use[SYMBOL_ADD_TO_STATE] = function (stateKey, ...args) {
use[SYMBOL_ADD_TO_STATE] = function (stateKey, args) {
const { stateRef } = context.use();
key = stateKey;

if (!Object.prototype.hasOwnProperty.call(stateRef.current(), key)) {
stateRef.set(
key,
typeof initialValue === 'function'
? initialValue(...args)
? initialValue.apply(null, args)
: initialValue
);
}
Expand All @@ -46,62 +43,46 @@ export default (function createState() {
}

function createRef(handlers = {}) {
const state = [];
let state = {};
const registeredHandlers = [];

function current() {
return state[0];
}

function prev() {
return state[1];
return state;
}

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

function reset() {
state.length = 0;

unshift();
}

function unshift() {
context.run({ stateRef }, () => {
state.unshift({});
for (const key in handlers) {
if (!Object.prototype.hasOwnProperty.call(current(), key)) {
if (Array.isArray(handlers[key])) {
/*
state.createRef({
useSuiteId: [useSuiteId, [id, name]],
})
*/
handlers[key][0][SYMBOL_ADD_TO_STATE](key, ...handlers[key][1]);
} else {
/*
state.createRef({
useSuiteId,
});
*/
handlers[key][SYMBOL_ADD_TO_STATE](key);
}
}
}
});
state = {};
registeredHandlers.forEach(fn => fn());
}

const stateRef = {
current,
prev,
reset,
set,
unshift,
};

for (const key in handlers) {
registeredHandlers.push(
Array.isArray(handlers[key])
? context.bind(
{ stateRef },
handlers[key][0][SYMBOL_ADD_TO_STATE],
key,
handlers[key][1]
)
: context.bind({ stateRef }, handlers[key][SYMBOL_ADD_TO_STATE], key)
);
}

context.run({ stateRef }, reset);

return stateRef;
Expand Down
32 changes: 1 addition & 31 deletions packages/vest/src/core/state/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,21 @@ describe('state', () => {
it('Should initialize state with provided keys', () => {
expect(stateRef.current()).toEqual(initialState);
});

it('Shoult initialize without a previous state', () => {
expect(stateRef.prev()).toBeUndefined();
});
});

describe('State reset', () => {
it.ctx('Should return state to its initial value', () => {
stateRef.unshift();
stateRef.reset();

useExampleState(() => 'example_2');

expect(stateRef.current()).toEqual({
...initialState,
exampleState: 'example_2',
});
expect(stateRef.prev()).toEqual(initialState);

stateRef.reset();
expect(stateRef.current()).toEqual(initialState);
expect(stateRef.prev()).toBeUndefined();
});
});

describe('unshift', () => {
it.ctx('Should move current state to `prev`', () => {
useExampleState(() => ({ example: true }));
const current = stateRef.current();

stateRef.unshift();

expect(stateRef.prev()).toBe(current);
expect(stateRef.prev()).toEqual({
...initialState,
exampleState: { example: true },
});
});

it.ctx('Should set `current` to initial value', () => {
useExampleState(() => ({ example: true }));
const current = stateRef.current();

stateRef.unshift();
expect(stateRef.current()).not.toBe(current);
expect(stateRef.current()).toEqual(initialState);
});
});

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

return Object.defineProperties(
context.bind({ stateRef }, (...args) => {
stateRef.unshift();
context.bind({ stateRef }, function () {
const [previousTestObjects = []] = useTestObjects();
stateRef.reset();

tests.apply(null, args);
mergeExcludedTests();
tests.apply(null, arguments);
mergeExcludedTests(previousTestObjects);

return produce();
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/core/suite/hasRemainingTests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import usePending from '../../test/lib/pending/usePending';
*/
const hasRemainingTests = fieldName => {
const [{ pending, lagging }] = usePending();
const allIncomplete = [...pending, ...lagging];
const allIncomplete = pending.concat(lagging);
if (!allIncomplete.length) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/test/lib/mergeExcludedTests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import useTestObjects from '../../useTestObjects';
/**
* Merges excluded tests with their prevState values.
*/
const mergeExcludedTests = () => {
useTestObjects((state, prevState) => {
if (!prevState) {
const mergeExcludedTests = prevState => {
useTestObjects(state => {
if (!Array.isArray(prevState) || !prevState.length) {
return state;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const createCache = (maxSize = 10) => {

const result = cacheAction();

cacheStorage.unshift([[...deps], result]);
cacheStorage.unshift([deps.concat(), result]);

if (cacheStorage.length > maxSize) {
cacheStorage.length = maxSize;
Expand Down

0 comments on commit 65a7468

Please sign in to comment.