From 5791fef3d68016aa5d7cb24a2ad13a17e3d970d5 Mon Sep 17 00:00:00 2001 From: ealush Date: Sat, 3 Apr 2021 23:11:32 +0400 Subject: [PATCH] patch(vest): use vast state library --- jsconfig.json | 1 - packages/vest/package.json | 5 +- .../src/core/state/__tests__/state.test.js | 147 ------------------ packages/vest/src/core/state/state.js | 50 ------ packages/vest/src/core/suite/createSuite.js | 2 +- packages/vest/testUtils/runCreateRef.js | 3 +- yarn.lock | 5 + 7 files changed, 11 insertions(+), 202 deletions(-) delete mode 100644 packages/vest/src/core/state/__tests__/state.test.js delete mode 100644 packages/vest/src/core/state/state.js diff --git a/jsconfig.json b/jsconfig.json index 474701d51..b5d5608c4 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -112,7 +112,6 @@ "produce": ["./packages/vest/src/core/produce/produce.js"], "resultKeys": ["./packages/vest/src/core/produce/resultKeys.js"], "createStateRef": ["./packages/vest/src/core/state/createStateRef.js"], - "state": ["./packages/vest/src/core/state/state.js"], "stateHooks": ["./packages/vest/src/core/state/stateHooks.js"], "createSuite": ["./packages/vest/src/core/suite/createSuite.js"], "hasRemainingTests": [ diff --git a/packages/vest/package.json b/packages/vest/package.json index 026776c26..de7493178 100644 --- a/packages/vest/package.json +++ b/packages/vest/package.json @@ -61,7 +61,8 @@ "devDependencies": { "anyone": "^0.0.7", "context": "^1.1.0", - "n4s": "^2.1.1" + "n4s": "^2.1.1", + "vast": "^1.0.1" }, "module": "./esm/vest.es.production.js" -} +} \ No newline at end of file diff --git a/packages/vest/src/core/state/__tests__/state.test.js b/packages/vest/src/core/state/__tests__/state.test.js deleted file mode 100644 index 873065cf0..000000000 --- a/packages/vest/src/core/state/__tests__/state.test.js +++ /dev/null @@ -1,147 +0,0 @@ -import createState from 'state'; - -let state; - -describe('state', () => { - beforeEach(() => { - state = createState(); - }); - - describe('createState', () => { - it('Should return all stateRef methods', () => { - expect(state).toMatchInlineSnapshot(` - Object { - "registerStateKey": [Function], - "reset": [Function], - } - `); - }); - }); - - describe('state.registerStateKey', () => { - it('Should return a function', () => { - expect(typeof state.registerStateKey()).toBe('function'); - }); - - it('Should append another state key on each call', () => { - const stateValues = Array.from({ length: 100 }, () => Math.random()); - const stateGetters = stateValues.map(value => - state.registerStateKey(value) - ); - expect( - stateGetters.every( - (stateGetter, i) => stateGetter()[0] === stateValues[i] - ) - ).toBe(true); - expect(stateGetters).toHaveLength(100); - }); - - describe('When initial value is a function', () => { - it('Should generate initial state from key', () => { - const initialStateKey = { key: 'value' }; - const stateGetter = state.registerStateKey(() => initialStateKey); - expect(stateGetter()[0]).toBe(initialStateKey); - }); - }); - - describe('When initial value is not a function', () => { - it('Should use provided value as initial state', () => { - const stateValue = { key: 'value' }; - const stateGetter = state.registerStateKey(stateValue); - expect(stateGetter()[0]).toBe(stateValue); - }); - }); - - describe('When initial value is not provided', () => { - it('Should set initial state to undefined', () => { - const stateGetter = state.registerStateKey(); - expect(stateGetter()[0]).toBeUndefined(); - }); - }); - }); - - describe('State key function', () => { - it('Should return an Array with two elements', () => { - expect(state.registerStateKey()()).toHaveLength(2); - expect(state.registerStateKey('some value')()).toMatchInlineSnapshot(` - Array [ - "some value", - [Function], - ] - `); - }); - - describe('getting current value', () => { - it('Should have current value in the first array element', () => { - const stateGetter = state.registerStateKey('Some Value'); - expect(stateGetter()[0]).toBe('Some Value'); - }); - }); - - describe('updating the state', () => { - it('Should contain state updater in the second array element', () => { - const stateGetter = state.registerStateKey('Some Value'); - expect(typeof stateGetter()[1]).toBe('function'); - }); - - it('Should update the state with provided value', () => { - const stateGetter = state.registerStateKey('Some Value'); - const [, valueSetter] = stateGetter(); - const nextValue = { key: 'value' }; - valueSetter(nextValue); - expect(stateGetter()[0]).toBe(nextValue); - }); - describe('When passing a function', () => { - it('Should update the state with the result of the function', () => { - const stateGetter = state.registerStateKey('Some Value'); - const [, valueSetter] = stateGetter(); - const nextValue = { key: 'value' }; - valueSetter(() => nextValue); - expect(stateGetter()[0]).toBe(nextValue); - }); - it('Should pass the function the current state value', () => { - const setter = jest.fn(() => 100); - const stateGetter = state.registerStateKey('555'); - const [, valueSetter] = stateGetter(); - valueSetter(setter); - expect(setter).toHaveBeenCalledWith('555'); - }); - }); - }); - }); - - describe('state.reset', () => { - it('Should fill up the state with registered keys', () => { - const s1 = state.registerStateKey(111); - const s2 = state.registerStateKey(222); - const s3 = state.registerStateKey(333); - const s4 = state.registerStateKey(444); - s1()[1](555); - s2()[1](666); - s3()[1](777); - s4()[1](888); - - // sanity - expect(s1()[0]).toBe(555); - expect(s2()[0]).toBe(666); - expect(s3()[0]).toBe(777); - expect(s4()[0]).toBe(888); - - state.reset(); - - // testing now that everything is back to initial value - expect(s1()[0]).toBe(111); - expect(s2()[0]).toBe(222); - expect(s3()[0]).toBe(333); - expect(s4()[0]).toBe(444); - }); - - it('Should allow setting a value after a state reset', () => { - const stateGetter = state.registerStateKey(() => 'hello!'); - const [, stateSetter] = stateGetter(); - state.reset(); - stateSetter('Good Bye!'); - expect(stateGetter()[0]).toBe('Good Bye!'); - }); - }); -}); diff --git a/packages/vest/src/core/state/state.js b/packages/vest/src/core/state/state.js deleted file mode 100644 index 782b42749..000000000 --- a/packages/vest/src/core/state/state.js +++ /dev/null @@ -1,50 +0,0 @@ -import isFunction from 'isFunction'; -import optionalFunctionValue from 'optionalFunctionValue'; - -export default function createState(onStateChange) { - const state = { - references: [], - }; - - const initializers = []; - - return { - registerStateKey, - reset, - }; - - function registerStateKey(initialValue) { - const key = initializers.length; - initializers.push(initialValue); - return initKey(key, initialValue); - } - - function initKey(key, value) { - current().push(optionalFunctionValue(value)); - - return function useStateKey() { - return [ - current()[key], - nextValue => - set(key, optionalFunctionValue(nextValue, [current()[key]])), - ]; - }; - } - - function current() { - return state.references; - } - - function reset() { - state.references = []; - initializers.forEach((value, index) => initKey(index, value)); - } - - function set(key, value) { - state.references[key] = value; - - if (isFunction(onStateChange)) { - onStateChange(); - } - } -} diff --git a/packages/vest/src/core/suite/createSuite.js b/packages/vest/src/core/suite/createSuite.js index 798533f29..aa7b031f4 100644 --- a/packages/vest/src/core/suite/createSuite.js +++ b/packages/vest/src/core/suite/createSuite.js @@ -1,3 +1,4 @@ +import createState from 'vast'; import asArray from 'asArray'; import createStateRef from 'createStateRef'; @@ -6,7 +7,6 @@ import genId from 'genId'; import isFunction from 'isFunction'; import mergeExcludedTests from 'mergeExcludedTests'; import produce from 'produce'; -import createState from 'state'; import { usePending, useTestObjects } from 'stateHooks'; import throwError from 'throwError'; import withArgs from 'withArgs'; diff --git a/packages/vest/testUtils/runCreateRef.js b/packages/vest/testUtils/runCreateRef.js index cc8c16778..164898bcc 100644 --- a/packages/vest/testUtils/runCreateRef.js +++ b/packages/vest/testUtils/runCreateRef.js @@ -1,5 +1,6 @@ +import createState from 'vast'; + import createStateRef from 'createStateRef'; -import createState from 'state'; export default state => createStateRef(state ? state : createState(), { diff --git a/yarn.lock b/yarn.lock index 4f6209b61..f912fa42e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5614,6 +5614,11 @@ validator@^13.5.2: resolved "https://registry.yarnpkg.com/validator/-/validator-13.5.2.tgz#c97ae63ed4224999fb6f42c91eaca9567fe69a46" integrity sha512-mD45p0rvHVBlY2Zuy3F3ESIe1h5X58GPfAtslBjY7EtTqGquZTj+VX/J4RnHWN8FKq0C9WRVt1oWAcytWRuYLQ== +vast@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/vast/-/vast-1.0.1.tgz#1021546aab2675fd8ff0f9d9dce6286d9515b0ef" + integrity sha512-MXneHapPy1DLusERpmCIOkMO4qZeK7HpUxNtBaSDGqZETub8fUnM0Y43LwzISavy0G+NfYoIY6hBOgTVm6ur4w== + verror@1.10.0: version "1.10.0" resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"