From 4ce2322f59dd303b86b503d532990f1c03fbf15d Mon Sep 17 00:00:00 2001 From: Preston Richey Date: Tue, 9 Jul 2024 09:54:00 -0400 Subject: [PATCH 1/3] Allow null response --- .../stories/docs/installation-setup.mdx | 2 +- packages/mock-addon/src/utils/validator.js | 8 ++++---- packages/mock-addon/src/utils/validator.test.js | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/mock-addon-docs/stories/docs/installation-setup.mdx b/packages/mock-addon-docs/stories/docs/installation-setup.mdx index f4a1941..3cb7f88 100644 --- a/packages/mock-addon-docs/stories/docs/installation-setup.mdx +++ b/packages/mock-addon-docs/stories/docs/installation-setup.mdx @@ -85,7 +85,7 @@ Each mock object contains the following properties. | `url` | Supports both **named parameters** (`/:foo/:bar`) and **query parameters**.(`/foo?bar=true`) | true | - | | `method` | Supports `GET`, `POST`, `PUT`, `PATCH` and `DELETE` methods. | true | - | | `status` | All possible [HTTP status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status). | true | - | -| `response` | A valid JSON format(Array or Object) or function.
Response function is a function that contains request object as a parameter. See the **Custom Response** section for example. | true | - | +| `response` | A valid JSON format(Array, Object, or null) or function.
Response function is a function that contains request object as a parameter. See the **Custom Response** section for example. | true | - | | `delay` | Emulate delayed response time in milliseconds. | - | `0` | diff --git a/packages/mock-addon/src/utils/validator.js b/packages/mock-addon/src/utils/validator.js index 3e0be46..5f6a624 100644 --- a/packages/mock-addon/src/utils/validator.js +++ b/packages/mock-addon/src/utils/validator.js @@ -23,10 +23,10 @@ export const schema = { }, response: (value) => { return ( - (isObject(value) || - Array.isArray(value) || - typeof value === 'function') && - value !== null + isObject(value) || + Array.isArray(value) || + typeof value === 'function' || + value === null ); }, delay: (value) => { diff --git a/packages/mock-addon/src/utils/validator.test.js b/packages/mock-addon/src/utils/validator.test.js index e2eb4d8..b852bc4 100644 --- a/packages/mock-addon/src/utils/validator.test.js +++ b/packages/mock-addon/src/utils/validator.test.js @@ -204,28 +204,28 @@ describe('Validator', () => { expect(actual).toEqual([]); }); - it('should return not valid response error if response is a string', () => { + it('should return empty error if response is null', () => { const mock = { url: 'https://jsonplaceholder.typicode.com/todos/:id', method: 'GET', status: 200, delay: 0, - response: 'string', + response: null, }; const actual = validate(mock, schema); - expect(actual).toEqual(['response: "string" is not valid.']); + expect(actual).toEqual([]); }); - it('should return not valid response error if response is null', () => { + it('should return not valid response error if response is a string', () => { const mock = { url: 'https://jsonplaceholder.typicode.com/todos/:id', method: 'GET', status: 200, delay: 0, - response: null, + response: 'string', }; const actual = validate(mock, schema); - expect(actual).toEqual(['response: null is not valid.']); + expect(actual).toEqual(['response: "string" is not valid.']); }); }); describe('validate delay', () => { From 2672531528ef961a8d724fd169d52ce64312d4ca Mon Sep 17 00:00:00 2001 From: Preston Richey Date: Tue, 9 Jul 2024 10:22:56 -0400 Subject: [PATCH 2/3] Allow any defined response value --- .../stories/docs/installation-setup.mdx | 18 ++++++++---------- packages/mock-addon/src/utils/validator.js | 7 +------ .../mock-addon/src/utils/validator.test.js | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/mock-addon-docs/stories/docs/installation-setup.mdx b/packages/mock-addon-docs/stories/docs/installation-setup.mdx index 3cb7f88..2ebe6db 100644 --- a/packages/mock-addon-docs/stories/docs/installation-setup.mdx +++ b/packages/mock-addon-docs/stories/docs/installation-setup.mdx @@ -1,4 +1,3 @@ - import { Meta } from '@storybook/addon-docs'; import LinkTo from '@storybook/addon-links/react'; import { Footer } from './footer'; @@ -22,6 +21,7 @@ import { Footer } from './footer'; ```sh npm i storybook-addon-mock -D ``` +
Using yarn
```sh @@ -75,19 +75,17 @@ export default { const Template = () => ; export const FetchCall = Template.bind({}); - ``` Each mock object contains the following properties. -| Property | Description | Required | Default | -| ---------- | :------------------------------------------------------------------------------------------ | :------- | :------ | -| `url` | Supports both **named parameters** (`/:foo/:bar`) and **query parameters**.(`/foo?bar=true`) | true | - | -| `method` | Supports `GET`, `POST`, `PUT`, `PATCH` and `DELETE` methods. | true | - | -| `status` | All possible [HTTP status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status). | true | - | -| `response` | A valid JSON format(Array, Object, or null) or function.
Response function is a function that contains request object as a parameter. See the **Custom Response** section for example. | true | - | -| `delay` | Emulate delayed response time in milliseconds. | - | `0` | - +| Property | Description | Required | Default | +| ---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------- | :------ | +| `url` | Supports both **named parameters** (`/:foo/:bar`) and **query parameters**.(`/foo?bar=true`) | true | - | +| `method` | Supports `GET`, `POST`, `PUT`, `PATCH` and `DELETE` methods. | true | - | +| `status` | All possible [HTTP status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status). | true | - | +| `response` | A value or a function that returns a value.
Response function is a function that contains request object as a parameter. See the **Custom Response** section for example. | true | - | +| `delay` | Emulate delayed response time in milliseconds. | - | `0` |
diff --git a/packages/mock-addon/src/utils/validator.js b/packages/mock-addon/src/utils/validator.js index 5f6a624..7662ade 100644 --- a/packages/mock-addon/src/utils/validator.js +++ b/packages/mock-addon/src/utils/validator.js @@ -22,12 +22,7 @@ export const schema = { return value && statusCodes.indexOf(value.toString()) >= 0; }, response: (value) => { - return ( - isObject(value) || - Array.isArray(value) || - typeof value === 'function' || - value === null - ); + return typeof value !== 'undefined'; }, delay: (value) => { return value ? typeof value === 'number' : true; diff --git a/packages/mock-addon/src/utils/validator.test.js b/packages/mock-addon/src/utils/validator.test.js index b852bc4..548c584 100644 --- a/packages/mock-addon/src/utils/validator.test.js +++ b/packages/mock-addon/src/utils/validator.test.js @@ -204,6 +204,18 @@ describe('Validator', () => { expect(actual).toEqual([]); }); + it('should return not valid response error if response is a string', () => { + const mock = { + url: 'https://jsonplaceholder.typicode.com/todos/:id', + method: 'GET', + status: 200, + delay: 0, + response: 'string', + }; + const actual = validate(mock, schema); + expect(actual).toEqual([]); + }); + it('should return empty error if response is null', () => { const mock = { url: 'https://jsonplaceholder.typicode.com/todos/:id', @@ -216,16 +228,16 @@ describe('Validator', () => { expect(actual).toEqual([]); }); - it('should return not valid response error if response is a string', () => { + it('should return not valid response error if response is undefined', () => { const mock = { url: 'https://jsonplaceholder.typicode.com/todos/:id', method: 'GET', status: 200, delay: 0, - response: 'string', + response: undefined, }; const actual = validate(mock, schema); - expect(actual).toEqual(['response: "string" is not valid.']); + expect(actual).toEqual(['response: undefined is not valid.']); }); }); describe('validate delay', () => { From b80aafcb5208e297b50973fad1c2b2de3214e780 Mon Sep 17 00:00:00 2001 From: Preston Richey Date: Tue, 9 Jul 2024 10:24:41 -0400 Subject: [PATCH 3/3] Fix string assertion test name --- packages/mock-addon/src/utils/validator.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mock-addon/src/utils/validator.test.js b/packages/mock-addon/src/utils/validator.test.js index 548c584..d0c320d 100644 --- a/packages/mock-addon/src/utils/validator.test.js +++ b/packages/mock-addon/src/utils/validator.test.js @@ -204,7 +204,7 @@ describe('Validator', () => { expect(actual).toEqual([]); }); - it('should return not valid response error if response is a string', () => { + it('should return empty error if response is a string', () => { const mock = { url: 'https://jsonplaceholder.typicode.com/todos/:id', method: 'GET',