Skip to content

Commit

Permalink
Merge pull request #4 from eryxcoop/endpoint-equality
Browse files Browse the repository at this point in the history
Endpoints name
  • Loading branch information
Florencia-97 authored Jul 23, 2024
2 parents 8a0ddd6 + 31f780d commit 8b8d7aa
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eryxcoop/appyx-comm",
"version": "1.0.8",
"version": "1.0.9",
"description": "Paquete de comunicación web",
"main": "index.js",
"source": "index.js",
Expand Down
4 changes: 4 additions & 0 deletions src/endpoints/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export default class Endpoint {
return this.ownResponses().concat(this.generalResponses())
}

get name() {
return `${this.method()} ${this.url()}`
}

url() {
return this._url;
}
Expand Down
4 changes: 2 additions & 2 deletions src/requester/FakeRequester.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default class FakeRequester extends Requester {
}

addResponseFor({endpoint, response}) {
this._expectedResponses[endpoint.constructor.name] = response;
this._expectedResponses[endpoint.name] = response;
}

_getResponseClassFor(endpoint) {
return this._expectedResponses[endpoint.constructor.name];
return this._expectedResponses[endpoint.name];
}

_getExpectedResponseFor(endpoint) {
Expand Down
66 changes: 62 additions & 4 deletions test/errorHandling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,17 @@ function dummyRequesterExpectingAuthenticationErrorResponse() {
return requester;
}

function endpointWithResponses(responses) {
function getEndpointWithResponses(responses, url = 'example') {
return Endpoint.newGet({
url: 'example',
url: url,
ownResponses: responses,
needsAuthorization: false,
});
}

function postEndpointWithResponses(responses, url = 'example') {
return Endpoint.newPost({
url: url,
ownResponses: responses,
needsAuthorization: false,
});
Expand Down Expand Up @@ -331,7 +339,7 @@ test('When fake requester is used default response is returned', async () => {
const apiClient = new ApiClient(requester);

// I can create a get endpoint
const getEndpoint = endpointWithResponses([TestSuccessfulApiResponse]);
const getEndpoint = getEndpointWithResponses([TestSuccessfulApiResponse]);

const customResponseHandler = new ApiResponseHandler(
{
Expand All @@ -353,7 +361,7 @@ test('When using fake requester default response can be overwritten', async () =
const apiClient = new ApiClient(requester);

// I can create a get endpoint
const getEndpoint = endpointWithResponses([TestSuccessfulApiResponse]);
const getEndpoint = getEndpointWithResponses([TestSuccessfulApiResponse]);

requester.addResponseFor({endpoint: getEndpoint, response: AnotherTestSuccessfulApiResponse});

Expand All @@ -369,4 +377,54 @@ test('When using fake requester default response can be overwritten', async () =

// Then the response is handled by the custom response handler
expect(response).toBe(AnotherTestSuccessfulApiResponse.defaultResponse());
});

test('When using fake requester only endpoint added to Fakerequester is overwritten', async () => {
// Given a client
const requester = new FakeRequester({waitingTime: 0});
const apiClient = new ApiClient(requester);

// I can create two get endpoint
const getEndpoint = getEndpointWithResponses([TestSuccessfulApiResponse], 'example-url');
const getAnotherEndpoint = getEndpointWithResponses([TestSuccessfulApiResponse], 'another-example-url');

requester.addResponseFor({endpoint: getAnotherEndpoint, response: AnotherTestSuccessfulApiResponse});

const customResponseHandler = new ApiResponseHandler(
{
handlesSuccess: (response, request) => {
return response.response();
}
}
);

const response = await apiClient.callEndpoint(getEndpoint, {}, customResponseHandler);

// Then the response is handled by the custom response handler
expect(response).toBe(TestSuccessfulApiResponse.defaultResponse());
});

test('When using fake requester for endpoint with same url only the clarified method is overwritten', async () => {
// Given a client
const requester = new FakeRequester({waitingTime: 0});
const apiClient = new ApiClient(requester);

// I can create two get endpoint
const getEndpoint = getEndpointWithResponses([TestSuccessfulApiResponse], 'example-url');
const getAnotherEndpoint = postEndpointWithResponses([TestSuccessfulApiResponse], 'example-url');

requester.addResponseFor({endpoint: getAnotherEndpoint, response: AnotherTestSuccessfulApiResponse});

const customResponseHandler = new ApiResponseHandler(
{
handlesSuccess: (response, request) => {
return response.response();
}
}
);

const response = await apiClient.callEndpoint(getEndpoint, {}, customResponseHandler);

// Then the response is handled by the custom response handler
expect(response).toBe(TestSuccessfulApiResponse.defaultResponse());
});

0 comments on commit 8b8d7aa

Please sign in to comment.