Skip to content

Commit

Permalink
test(client-request): add tests for ClientRequest class
Browse files Browse the repository at this point in the history
feat(client-request): add error handling for failed requests in get method
feat(client-request): add support for query parameters in get method
  • Loading branch information
mdwitr0 committed Jun 24, 2023
1 parent a8bd939 commit 7879f4b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
67 changes: 67 additions & 0 deletions src/client-request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ClientRequest } from './client-request';
import { VERSIONS } from './enums/version.enum';
const fetchMock = require('jest-fetch-mock');
fetchMock.enableMocks();

describe('ClientRequest', () => {
let clientRequest: ClientRequest;

beforeEach(() => {
clientRequest = new ClientRequest('test_key', 'https://test_url.com');
fetchMock.resetMocks();
});

describe('get', () => {
it('should return data when request is successful', async () => {
const testResponse = { data: 'test_data' };
fetchMock.mockResponseOnce(JSON.stringify(testResponse), { status: 200 });

const response = await clientRequest.get(VERSIONS.V1, '/test_path');

expect(fetchMock).toHaveBeenCalledWith(
'https://test_url.com/v1/test_path?',
{ headers: { 'X-API-KEY': 'test_key' } },
);
expect(response.data).toEqual(testResponse);
});

it('should return error message when request fails', async () => {
const testError = { error: 'test_error', message: 'test_message' };
fetchMock.mockResponseOnce(JSON.stringify(testError), { status: 500 });

const response = await clientRequest.get(VERSIONS.V1, '/test_path');

expect(fetchMock).toHaveBeenCalledWith(
'https://test_url.com/v1/test_path?',
{ headers: { 'X-API-KEY': 'test_key' } },
);
expect(response.statusCode).toEqual(500);
expect(response.error).toEqual(testError.error);
expect(response.message).toEqual(testError.message);
});

it('should handle query parameters correctly', async () => {
const testResponse = { data: 'test_data' };
fetchMock.mockResponseOnce(JSON.stringify(testResponse), { status: 200 });

const queryParams = {
id: 666,
year: [2020, 2023],
rating: '7.5-10',
'poster.url': '!null',
};

const response = await clientRequest.get(
VERSIONS.V1,
'/test_path',
queryParams,
);

expect(fetchMock).toHaveBeenCalledWith(
'https://test_url.com/v1/test_path?id=666&year=2020&year=2023&rating=7.5-10&poster.url=%21null',
{ headers: { 'X-API-KEY': 'test_key' } },
);
expect(response.data).toEqual(testResponse);
});
});
});
21 changes: 13 additions & 8 deletions src/client-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class ClientRequest {
) {}

private queryParams(params: { [key: string]: any }): string {
let urlSearchParams = new URLSearchParams();
const urlSearchParams = new URLSearchParams();

for (let key in params) {
for (const key in params) {
if (Array.isArray(params[key])) {
for (let value of params[key]) {
for (const value of params[key]) {
urlSearchParams.append(key, value);
}
} else {
Expand All @@ -39,7 +39,11 @@ export class ClientRequest {
if (!response.ok) {
const error: IResponseError = await response.json();
throw new Error(
`HTTP error! status: ${response.status}, error: ${error.error}, message: ${error.message}`,
JSON.stringify({
status: response.status,
error: error.error,
message: error.message,
}),
);
}

Expand All @@ -50,12 +54,13 @@ export class ClientRequest {
message: null,
};
} catch (error) {
console.error('There was an error with your request:', error);
const { status, message, error: err } = JSON.parse(error.message);
console.log('There was an error with your request:', error);
return {
data: null,
statusCode: error.status,
error: error.error,
message: error.message,
statusCode: status,
error: err,
message: message,
};
}
}
Expand Down

0 comments on commit 7879f4b

Please sign in to comment.