From 28dcf3f39f64e68c07827fe3746d84231b3862bf Mon Sep 17 00:00:00 2001 From: Florencia-97 Date: Thu, 18 Jul 2024 16:22:49 -0300 Subject: [PATCH 1/3] Now endpoints created without declaring class can be used in fake requester as ewll --- src/endpoints/Endpoint.js | 4 ++++ src/requester/FakeRequester.js | 4 ++-- test/errorHandling.test.js | 29 +++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/endpoints/Endpoint.js b/src/endpoints/Endpoint.js index 8643245..b96cf94 100644 --- a/src/endpoints/Endpoint.js +++ b/src/endpoints/Endpoint.js @@ -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; } diff --git a/src/requester/FakeRequester.js b/src/requester/FakeRequester.js index 3f073e6..7e1eef3 100644 --- a/src/requester/FakeRequester.js +++ b/src/requester/FakeRequester.js @@ -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) { diff --git a/test/errorHandling.test.js b/test/errorHandling.test.js index 39c345b..83f13ba 100644 --- a/test/errorHandling.test.js +++ b/test/errorHandling.test.js @@ -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, }); @@ -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()); }); \ No newline at end of file From 7d3f1c190cee3b7e17213d64cb7c3fecfd26e62a Mon Sep 17 00:00:00 2001 From: Florencia-97 Date: Fri, 19 Jul 2024 11:08:22 -0300 Subject: [PATCH 2/3] Completes tests --- test/errorHandling.test.js | 43 +++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/test/errorHandling.test.js b/test/errorHandling.test.js index 83f13ba..3193176 100644 --- a/test/errorHandling.test.js +++ b/test/errorHandling.test.js @@ -72,7 +72,7 @@ function dummyRequesterExpectingAuthenticationErrorResponse() { return requester; } -function endpointWithResponses(responses, url= 'example') { +function getEndpointWithResponses(responses, url = 'example') { return Endpoint.newGet({ url: url, ownResponses: responses, @@ -80,6 +80,14 @@ function endpointWithResponses(responses, url= 'example') { }); } +function postEndpointWithResponses(responses, url = 'example') { + return Endpoint.newPost({ + url: url, + ownResponses: responses, + needsAuthorization: false, + }); +} + test('Test general error handling can be set for api client', async () => { // Given a client that is not authenticated const requester = dummyRequesterExpectingAuthenticationErrorResponse(); @@ -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( { @@ -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}); @@ -377,8 +385,33 @@ test('When using fake requester only endpoint added to Fakerequester is overwrit const apiClient = new ApiClient(requester); // I can create two get endpoint - const getEndpoint = endpointWithResponses([TestSuccessfulApiResponse]); - const getAnotherEndpoint = endpointWithResponses([TestSuccessfulApiResponse], 'another-example'); + 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}); From 31f780d9b5c8441968cb4583204f93e7679b14eb Mon Sep 17 00:00:00 2001 From: Florencia-97 Date: Tue, 23 Jul 2024 13:41:42 -0300 Subject: [PATCH 3/3] Updates version for package json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bd02910..4d9acd7 100644 --- a/package.json +++ b/package.json @@ -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",