From f05527b92268c49272204c1b9159d933ca01ac16 Mon Sep 17 00:00:00 2001 From: Sebastian Rettig Date: Mon, 20 Nov 2023 19:48:09 +0100 Subject: [PATCH 1/4] feat: allow string responses --- packages/mock-addon/src/utils/headers.js | 4 ++++ packages/mock-addon/src/utils/response.js | 6 ++++-- packages/mock-addon/src/utils/response.test.js | 6 ++++-- packages/mock-addon/src/utils/validator.js | 1 + packages/mock-addon/src/utils/validator.test.js | 6 +++--- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/mock-addon/src/utils/headers.js b/packages/mock-addon/src/utils/headers.js index cb03fe1..1776426 100644 --- a/packages/mock-addon/src/utils/headers.js +++ b/packages/mock-addon/src/utils/headers.js @@ -21,3 +21,7 @@ export function getResponseHeaderMap(xhr) { export const defaultResponseHeaders = { 'content-type': 'application/json', }; + +export const textResponseHeaders = { + 'content-type': 'text/plain', +}; diff --git a/packages/mock-addon/src/utils/response.js b/packages/mock-addon/src/utils/response.js index 2287f0b..437e4d2 100644 --- a/packages/mock-addon/src/utils/response.js +++ b/packages/mock-addon/src/utils/response.js @@ -1,6 +1,6 @@ import 'whatwg-fetch'; import statusTextMap from './statusMap'; -import { defaultResponseHeaders } from './headers'; +import { defaultResponseHeaders, textResponseHeaders } from './headers'; export function CustomResponse(url, status, responseText) { const text = @@ -13,7 +13,9 @@ export function CustomResponse(url, status, responseText) { status: status, statusText: statusTextMap[status.toString()], headers: new Headers({ - ...defaultResponseHeaders, + ...(typeof responseText === 'string' + ? textResponseHeaders + : defaultResponseHeaders), }), url, }); diff --git a/packages/mock-addon/src/utils/response.test.js b/packages/mock-addon/src/utils/response.test.js index 3417f5b..4efcecc 100644 --- a/packages/mock-addon/src/utils/response.test.js +++ b/packages/mock-addon/src/utils/response.test.js @@ -21,8 +21,10 @@ describe('CustomResponse', () => { }); it('should return text as a string if responseText is string', async () => { const response = new CustomResponse(mockURL, 200, 'This is a string'); - const actual = await response.text(); - expect(actual).toEqual('This is a string'); + const actualText = await response.text(); + const actualContentType = await response.headers.get("content-type"); + expect(actualText).toEqual('This is a string'); + expect(actualContentType).toEqual('text/plain') }); it('should return text as a string if responseText is an object', async () => { const response = new CustomResponse(mockURL, 200, { key: 'test' }); diff --git a/packages/mock-addon/src/utils/validator.js b/packages/mock-addon/src/utils/validator.js index 3e0be46..aa1aba5 100644 --- a/packages/mock-addon/src/utils/validator.js +++ b/packages/mock-addon/src/utils/validator.js @@ -25,6 +25,7 @@ export const schema = { return ( (isObject(value) || Array.isArray(value) || + value === 'string' || typeof value === 'function') && value !== null ); diff --git a/packages/mock-addon/src/utils/validator.test.js b/packages/mock-addon/src/utils/validator.test.js index e2eb4d8..c1c538d 100644 --- a/packages/mock-addon/src/utils/validator.test.js +++ b/packages/mock-addon/src/utils/validator.test.js @@ -52,7 +52,7 @@ describe('Validator', () => { expect(actual).toEqual([]); }); - it('should return rul error array if url is not a string', () => { + it('should return url error array if url is not a string', () => { const mock = { url: {}, method: 'GET', @@ -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', @@ -213,7 +213,7 @@ 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', () => { From 89f3c1bf30517b5286ddd4eaafa4902a378aef3b Mon Sep 17 00:00:00 2001 From: Sebastian Rettig Date: Mon, 20 Nov 2023 19:51:24 +0100 Subject: [PATCH 2/4] refactor: fix formatting --- packages/mock-addon/src/utils/response.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mock-addon/src/utils/response.test.js b/packages/mock-addon/src/utils/response.test.js index 4efcecc..09b74c2 100644 --- a/packages/mock-addon/src/utils/response.test.js +++ b/packages/mock-addon/src/utils/response.test.js @@ -22,9 +22,9 @@ describe('CustomResponse', () => { it('should return text as a string if responseText is string', async () => { const response = new CustomResponse(mockURL, 200, 'This is a string'); const actualText = await response.text(); - const actualContentType = await response.headers.get("content-type"); + const actualContentType = await response.headers.get('content-type'); expect(actualText).toEqual('This is a string'); - expect(actualContentType).toEqual('text/plain') + expect(actualContentType).toEqual('text/plain'); }); it('should return text as a string if responseText is an object', async () => { const response = new CustomResponse(mockURL, 200, { key: 'test' }); From 95070632aaa70b6fd63f486cd4cf2cb53d93807b Mon Sep 17 00:00:00 2001 From: Sebastian Rettig Date: Thu, 23 Nov 2023 17:45:03 +0100 Subject: [PATCH 3/4] fix: correct validator --- packages/mock-addon/src/utils/validator.js | 2 +- packages/mock-addon/src/utils/validator.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mock-addon/src/utils/validator.js b/packages/mock-addon/src/utils/validator.js index aa1aba5..e0dff11 100644 --- a/packages/mock-addon/src/utils/validator.js +++ b/packages/mock-addon/src/utils/validator.js @@ -25,7 +25,7 @@ export const schema = { return ( (isObject(value) || Array.isArray(value) || - value === 'string' || + typeof value === 'string' || typeof value === 'function') && value !== null ); diff --git a/packages/mock-addon/src/utils/validator.test.js b/packages/mock-addon/src/utils/validator.test.js index c1c538d..9c0279e 100644 --- a/packages/mock-addon/src/utils/validator.test.js +++ b/packages/mock-addon/src/utils/validator.test.js @@ -210,7 +210,7 @@ describe('Validator', () => { method: 'GET', status: 200, delay: 0, - response: 'string', + response: 'a string value', }; const actual = validate(mock, schema); expect(actual).toEqual([]); From e8307abbaed536f3093f287d19ff5b576da3ffaa Mon Sep 17 00:00:00 2001 From: Sebastian Rettig Date: Thu, 23 Nov 2023 17:47:01 +0100 Subject: [PATCH 4/4] docs: add response string to docs --- .../mock-addon-docs/stories/docs/installation-setup.stories.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mock-addon-docs/stories/docs/installation-setup.stories.mdx b/packages/mock-addon-docs/stories/docs/installation-setup.stories.mdx index f4a1941..b811823 100644 --- a/packages/mock-addon-docs/stories/docs/installation-setup.stories.mdx +++ b/packages/mock-addon-docs/stories/docs/installation-setup.stories.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 or Object), function or string.
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` |