Skip to content

Commit

Permalink
Removes unnecessary code in tests and case for general and custom err…
Browse files Browse the repository at this point in the history
…or handling
  • Loading branch information
Florencia-97 committed Jan 16, 2024
1 parent 3f7d507 commit 52b04b9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 104 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
97 changes: 0 additions & 97 deletions test/api.test.js

This file was deleted.

103 changes: 103 additions & 0 deletions test/errorHandling.test.js
Original file line number Diff line number Diff line change
@@ -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')
});
16 changes: 16 additions & 0 deletions test/utils/DummyRequester.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 52b04b9

Please sign in to comment.