From b0e8ef0a31c1baf3f7ccd3d59030a7746b12cf86 Mon Sep 17 00:00:00 2001 From: Arturo Riveron Borodovisina Date: Mon, 14 Mar 2022 22:10:14 +0100 Subject: [PATCH] feat(http): increase unit test coverage --- package.json | 3 +- test/tests/http.js | 129 ++++++++++++++++++++++++++++++++++++++++++++ test/tests/index.js | 1 + 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 test/tests/http.js diff --git a/package.json b/package.json index fcb0786e..4b7e6fa7 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,8 @@ "grumbler-scripts": "^5.0.0", "husky": "^7.0.4", "mocha": "^4", - "standard-version": "^9.3.2" + "standard-version": "^9.3.2", + "sync-browser-mocks": "^2.0.8" }, "dependencies": { "@krakenjs/cross-domain-safe-weakmap": "^2.0.2", diff --git a/test/tests/http.js b/test/tests/http.js new file mode 100644 index 00000000..fef4289e --- /dev/null +++ b/test/tests/http.js @@ -0,0 +1,129 @@ +/* @flow */ +import { $mockEndpoint, patchXmlHttpRequest } from 'sync-browser-mocks/dist/sync-browser-mocks'; + +import { request, addHeaderBuilder } from '../../src/http'; + +describe.only('http cases', () => { + const url = '/test'; + const initialRequestConfig = { + method: 'GET', + uri: url, + data: true + }; + const mockRequest = (sourceData = initialRequestConfig) => { + $mockEndpoint.register(sourceData).listen(); + }; + + before(() => { + patchXmlHttpRequest(); + }); + + it('request should fail when multiple data is set', async () => { + const expecteErrorMessage = 'Only options.json or options.data or options.body should be passed'; + try { + await request({ url, json: [ true ], data: {}, body: 'true' }); + } catch (err) { + if (err.message !== expecteErrorMessage) { + throw new Error(`should throw the error message "${ expecteErrorMessage }", but got: ${ err.message }`); + } + } + }); + + // TODO: Depends on the changes in the "sync-browser-mocks" repo + // it('request should fail when response status is not valid', async () => { + // const expectedErrorMessage = `Request to ${ initialRequestConfig.method.toLowerCase() } ${ url } failed: no response status code.`; + + // mockRequest({ ...initialRequestConfig, status: 500 }); + // try { + // await request({ url }); + // } catch (err) { + // if (err.message !== expectedErrorMessage) { + // throw new Error(`should throw the error message "${ expectedErrorMessage }", but got: ${ err.message }`); + // } + // } + // }); + + // TODO: Depends on the changes in the "sync-browser-mocks" repo + // it('request should handle exception when something was wrong', async () => { + // const expectedErrorMessage = `Request to ${ initialRequestConfig.method.toLowerCase() } ${ url } failed: no response status code.`; + + // mockRequest({ + // ...initialRequestConfig, + // status: 500, + // data: undefined, + // handler: () => { + // throw new Error('server error'); + // } + // }); + + // try { + // await request({ url }); + // } catch (err) { + // if (err.message !== expectedErrorMessage) { + // throw new Error(`should throw the error message "${ expectedErrorMessage }", but got: ${ err.message }`); + // } + // } + // }); + + // TODO: Depends on the changes in the "sync-browser-mocks" repo + // it('request should fail when with timeout', async () => { + // const expectedErrorMessage = `Request to ${ initialRequestConfig.method.toLowerCase() } ${ url } has timed out`; + // mockRequest({ + // method: 'GET', + // uri: url, + // handler: setTimeout(() => true, 1000) + // }); + // try { + // await request({ url }); + // } catch (err) { + // if (err.message !== expectedErrorMessage) { + // throw new Error(`should throw the error message "${ expectedErrorMessage }", but got: ${ err.message }`); + // } + // } + // }); + + it('request should received a valid response when use data parameter', async () => { + mockRequest(); + const result = await request({ + url, + data: { 'value': '1' } + }); + + // $FlowIgnore[prop-missing] + if (result.status !== 200 || !result.body) { + // $FlowIgnore[prop-missing] + throw new Error(`should return a "true" value as response, but got status: ${ result.status } and body: ${ result.body }`); + } + }); + + it('request should received a valid response when use json parameter', async () => { + mockRequest(); + addHeaderBuilder(() => ({ 'Content-Type': 'application/json' })); + const result = await request({ + url, + headers: { 'Content-Type': 'application/json' }, + json: { value: 1 } + }); + + // $FlowIgnore[prop-missing] + if (result.status !== 200 || !result.body) { + // $FlowIgnore[prop-missing] + throw new Error(`should return a "true" value as response, but got status: ${ result.status } and body: ${ result.body }`); + } + }); + + it('request should throw and error when parsing "undefined" response', async () => { + const expectedErrorMessage = 'Invalid json: undefined.'; + mockRequest({ ...initialRequestConfig, data: undefined }); + try { + await request({ + url, + headers: { 'Content-Type': 'application/json' } + }); + } catch (err) { + if (err.message !== expectedErrorMessage) { + throw new Error(`should throw error message "${ expectedErrorMessage }", but got: ${ err.message }`); + } + } + }); +}); diff --git a/test/tests/index.js b/test/tests/index.js index ec8e7b1f..5cdb329c 100644 --- a/test/tests/index.js +++ b/test/tests/index.js @@ -6,3 +6,4 @@ import './css'; import './experiment'; import './global'; import './device'; +import './http';