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

feat: allow any defined response value #214

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions packages/mock-addon-docs/stories/docs/installation-setup.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { Meta } from '@storybook/addon-docs';
import LinkTo from '@storybook/addon-links/react';
import { Footer } from './footer';
Expand All @@ -22,6 +21,7 @@ import { Footer } from './footer';
```sh
npm i storybook-addon-mock -D
```

<div> Using yarn </div>

```sh
Expand Down Expand Up @@ -75,19 +75,17 @@ export default {
const Template = () => <FetchExample />;

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 or Object) or function. <br/> 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. <br/> 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` |

<br />

Expand Down
7 changes: 1 addition & 6 deletions packages/mock-addon/src/utils/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions packages/mock-addon/src/utils/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -213,10 +213,10 @@ describe('Validator', () => {
response: 'string',
};
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 empty error if response is null', () => {
const mock = {
url: 'https://jsonplaceholder.typicode.com/todos/:id',
method: 'GET',
Expand All @@ -225,7 +225,19 @@ describe('Validator', () => {
response: null,
};
const actual = validate(mock, schema);
expect(actual).toEqual(['response: null is not valid.']);
expect(actual).toEqual([]);
});

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: undefined,
};
const actual = validate(mock, schema);
expect(actual).toEqual(['response: undefined is not valid.']);
});
});
describe('validate delay', () => {
Expand Down