diff --git a/README.md b/README.md index d78ac23..4224150 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ -![image](https://img.shields.io/badge/JavaScript-323330?style=for-the-badge&logo=javascript&logoColor=F7DF1E) -![image](https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white) - +![test workflow](https://github.com/github/docs/actions/workflows/test.yml/badge.svg) # Appyx-Comm -![test workflow](https://github.com/github/docs/actions/workflows/test.yml/badge.svg) +![image](https://img.shields.io/badge/JavaScript-323330?style=for-the-badge&logo=javascript&logoColor=F7DF1E) ![image](https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white) Communication package for javascript. @@ -16,12 +14,12 @@ $ npm i @eryxcoop/appyx-comm # Contribute -In order to update npm package a new release must be created. Any new release must be named @eryxcoop/appyx-comm-{version} - +In order to update npm package a new release must be created. Any new release must be named +@eryxcoop/appyx-comm-{version} # Work to be done - [x] Deploy library as a package in npm - [ ] Create sample project to show how the package works - [x] Add ApiClient -- [ ] Add tests +- [x] Add tests diff --git a/test/api.test.js b/test/api.test.js deleted file mode 100644 index 9c2e1f5..0000000 --- a/test/api.test.js +++ /dev/null @@ -1,97 +0,0 @@ -import AuthorizationManager from "../src/authorization/AuthorizationManager"; -import Endpoint from "../src/endpoints/Endpoint"; -import {SuccessfulApiResponse} from "../index"; -import ApiClient from "../src/ApiClient"; -import ApiResponseErrorHandler from "../src/errors/ApiResponseErrorHandler"; -import AuthenticationErrorResponse from "../src/responses/generalResponses/AuthenticationErrorResponse"; - -import {assert,expect, test} from 'vitest' -import {Requester} from "../src/requester/Requester.js"; - -class DummyRequester extends Requester { - constructor(expectedResponses) { - super(); - this._expectedResponses = expectedResponses; - } - - setExpectedResponses(expectedResponses) { - this._expectedResponses = expectedResponses; - } - - call({endpoint, data = undefined}) { - return this._expectedResponses; - } -} - -class AppAuthorizationManager extends AuthorizationManager { - configureHeaders(headers) { - headers["Authorization"] = `Bearer 123456}`; - } -} - -class ExampleEndpoint extends Endpoint { - url() { - return "example/example"; - } - - method() { - return "GET"; - } - - needsAuthorization() { - return false; - } - - responses() { - return [SuccessfulApiResponse]; - } - -} - - -class ExampleApiClient extends ApiClient { - exampleEndpoint(customErrorHandler) { - const endpoint = new ExampleEndpoint(); - return this._callEndpoint(endpoint, {}, customErrorHandler); - } -} - -class GeneralErrorHandlerBuilder { - build() { - return new ApiResponseErrorHandler().handlesError( - AuthenticationErrorResponse, - this._handleUnauthenticatedResponse.bind(this), - ); - } - - async _handleUnauthenticatedResponse(request) { - console.log('handling request!') - } -} - -test('Basic use', async () => { - const requester = new DummyRequester(); - requester.setExpectedResponses( - new AuthenticationErrorResponse( - { - "object": null, - "errors": [ - { - "code": "authentication_error", - "text": "" - } - ] - } - )); - const generalErrorHandler = new GeneralErrorHandlerBuilder().build(); - const client = new ExampleApiClient(requester, generalErrorHandler); - const customErrorHandler = new ApiResponseErrorHandler().handlesError( - AuthenticationErrorResponse, - (request) => { - return 'custom error handler' - }, - ); - //assert(() => true, 'should be true') - const response = await client.exampleEndpoint(customErrorHandler); - expect(response).toBe('custom error handler') -}); \ No newline at end of file diff --git a/test/errorHandling.test.js b/test/errorHandling.test.js new file mode 100644 index 0000000..de289fd --- /dev/null +++ b/test/errorHandling.test.js @@ -0,0 +1,103 @@ +import Endpoint from "../src/endpoints/Endpoint"; +import {SuccessfulApiResponse} from "../index"; +import ApiClient from "../src/ApiClient"; +import ApiResponseErrorHandler from "../src/errors/ApiResponseErrorHandler"; +import AuthenticationErrorResponse from "../src/responses/generalResponses/AuthenticationErrorResponse"; + +import {expect, test} from 'vitest' +import {DummyRequester} from "./utils/DummyRequester.js"; + +class ExampleEndpoint extends Endpoint { + url() { + return "example/example"; + } + + method() { + return "GET"; + } + + needsAuthorization() { + return false; + } + + responses() { + return [SuccessfulApiResponse]; + } + +} + + +class ExampleApiClient extends ApiClient { + exampleEndpoint(customErrorHandler) { + const endpoint = new ExampleEndpoint(); + return this._callEndpoint(endpoint, {}, customErrorHandler); + } +} + +test('Test general error handling can be set for api client', async () => { + // Given a client that is not authenticated + const requester = new DummyRequester(); + requester.setExpectedResponses( + new AuthenticationErrorResponse( + { + "object": null, + "errors": [ + { + "code": "authentication_error", + "text": "" + } + ] + } + )); + + // And a call to an endpoint that needs authentication + const generalErrorHandler = new ApiResponseErrorHandler().handlesError( + AuthenticationErrorResponse, + (request) => { + return 'general error handler' + }, + ); + const client = new ExampleApiClient(requester, generalErrorHandler); + const response = await client.exampleEndpoint(); + + // Then the response is handled by the general error handler + expect(response).toBe('general error handler') +}); + +test('Test general error can be overridden for call in api client', async () => { + // Given a client that is not authenticated + const requester = new DummyRequester(); + requester.setExpectedResponses( + new AuthenticationErrorResponse( + { + "object": null, + "errors": [ + { + "code": "authentication_error", + "text": "" + } + ] + } + )); + + // And a call to an endpoint that needs authentication + const generalErrorHandler = new ApiResponseErrorHandler().handlesError( + AuthenticationErrorResponse, + (request) => { + return 'general error handler' + }, + ); + const client = new ExampleApiClient(requester, generalErrorHandler); + + // but has a custom error handler for it + const customErrorHandler = new ApiResponseErrorHandler().handlesError( + AuthenticationErrorResponse, + (request) => { + return 'custom error handler' + }, + ); + const response = await client.exampleEndpoint(customErrorHandler); + + // Then the response is handled by the custom error handler + expect(response).toBe('custom error handler') +}); \ No newline at end of file diff --git a/test/utils/DummyRequester.js b/test/utils/DummyRequester.js new file mode 100644 index 0000000..497ecf2 --- /dev/null +++ b/test/utils/DummyRequester.js @@ -0,0 +1,16 @@ +import {Requester} from "../../src/requester/Requester.js"; + +export class DummyRequester extends Requester { + constructor(expectedResponses) { + super(); + this._expectedResponses = expectedResponses; + } + + setExpectedResponses(expectedResponses) { + this._expectedResponses = expectedResponses; + } + + call({endpoint, data = undefined}) { + return this._expectedResponses; + } +} \ No newline at end of file