Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: vest.reset() #150

Merged
merged 2 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,29 @@ In case you have an async test that didn't finish from the previous suite run -
## What about server side validations?

You probably needs to consider running vest using its [stateless interface](./stateless_validations).

## Resetting suite state with `vest.reset();`

In some cases, such as form reset, you want to discard of previous validation results. This can be done with `vest.reset()`.

`vest.reset` disables all pending async tests in your suite, and deletes the suite state. To use your suite again, you will need to initialize it again with `vest.create()`.

### `vest.reset()` params:

| Name | Type | Optional | Description |
| --------- | -------- | -------- | ---------------------------------------- |
| suiteName | `string` | No | The name of the suite you with to reset. |

### Usage:

```js
import vest from 'vest';

const validate = vest.create('suite_name', () => {
// your tests here
});

validate(); // validation result is added to the state.

vest.reset('suite_name'); // validation result is removed
```
57 changes: 29 additions & 28 deletions packages/vest/dist/vest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/vest/dist/vest.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/vest/src/__snapshots__/spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Object {
"draft": [Function],
"enforce": [Function],
"only": [Function],
"reset": [Function],
"runWithContext": [Function],
"skip": [Function],
"test": [Function],
Expand Down
18 changes: 18 additions & 0 deletions packages/vest/src/core/state/cleanupCompletedSuite/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getSuiteState from '../getSuiteState';
import hasRemainingTests from '../hasRemainingTests';
import reset from '../reset';
/**
* Removes completed "stateless" suite from state storage.
* @param {string} suiteId
*/
const cleanupCompletedSuite = suiteId => {
const state = getSuiteState(suiteId);

if (hasRemainingTests(state)) {
return;
}

reset(suiteId);
};

export default cleanupCompletedSuite;
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
import runWithContext from '../../../lib/runWithContext';
import singleton from '../../../lib/singleton';
import { SYMBOL_SUITES } from '../symbols';
import cleanupStatelessSuite from '.';
import cleanupCompletedSuite from '.';

const suiteName = 'suite_1';
const suiteId = 'suiteId_1';
const suiteId_2 = 'suiteId_2';
let context, defaultContext;
describe('cleanupStatelessSuite', () => {
describe('cleanupCompletedSuite', () => {
beforeEach(() => {
defaultContext = {
name: suiteName,
Expand All @@ -24,7 +24,7 @@ describe('cleanupStatelessSuite', () => {

describe('When suite does not exist', () => {
it('Should throw an error', () => {
expect(() => cleanupStatelessSuite(suiteId)).toThrow();
expect(() => cleanupCompletedSuite(suiteId)).toThrow();
});
});

Expand All @@ -33,7 +33,7 @@ describe('cleanupStatelessSuite', () => {
context = singleton.useContext();
runWithContext(context, () => {
expect(singleton.useContext()).toBe(context);
cleanupStatelessSuite(suiteId);
cleanupCompletedSuite(suiteId);
expect(singleton.useContext()).toBeNull();
});
});
Expand All @@ -53,7 +53,7 @@ describe('cleanupStatelessSuite', () => {
it('Should remove suite from state', () => {
expect(getState()[SYMBOL_SUITES]).toHaveProperty(suiteId);
expect(getState()[SYMBOL_SUITES]).toHaveProperty(suiteId_2);
cleanupStatelessSuite(suiteId_2);
cleanupCompletedSuite(suiteId_2);
expect(getState()[SYMBOL_SUITES]).not.toHaveProperty(suiteId_2);
expect(getState()[SYMBOL_SUITES]).toHaveProperty(suiteId);
});
Expand Down
20 changes: 0 additions & 20 deletions packages/vest/src/core/state/cleanupStatelessSuite/index.js

This file was deleted.

6 changes: 3 additions & 3 deletions packages/vest/src/core/state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const register = () => {

/**
* Retrieves the state object or a portion of it.
* @param {string} [suiteId] state portion to retrieve.
* @param {string} [key] state portion to retrieve.
*/
export const getState = suiteId =>
suiteId ? singleton.use(SYMBOL_STATE)[suiteId] : singleton.use(SYMBOL_STATE);
export const getState = key =>
key ? singleton.use(SYMBOL_STATE)[key] : singleton.use(SYMBOL_STATE);

/**
* Updates the state with the value return from the setter callback.
Expand Down