Skip to content

Commit

Permalink
Now endpoints created without declaring class can be used in fake req…
Browse files Browse the repository at this point in the history
…uester as ewll
  • Loading branch information
Florencia-97 committed Jul 18, 2024
1 parent 8a0ddd6 commit 28dcf3f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
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
29 changes: 27 additions & 2 deletions test/errorHandling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ function dummyRequesterExpectingAuthenticationErrorResponse() {
return requester;
}

function endpointWithResponses(responses) {
function endpointWithResponses(responses, url= 'example') {
return Endpoint.newGet({
url: 'example',
url: url,
ownResponses: responses,
needsAuthorization: false,
});
Expand Down Expand Up @@ -369,4 +369,29 @@ 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 = endpointWithResponses([TestSuccessfulApiResponse]);
const getAnotherEndpoint = endpointWithResponses([TestSuccessfulApiResponse], 'another-example');

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 28dcf3f

Please sign in to comment.