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

Increase unit test coverage in the http file 🧪 #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
129 changes: 129 additions & 0 deletions test/tests/http.js
Original file line number Diff line number Diff line change
@@ -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 }`);
}
}
});
});
1 change: 1 addition & 0 deletions test/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import './css';
import './experiment';
import './global';
import './device';
import './http';