chore: add common/api/types/api tests

This commit is contained in:
Sostene Munezero Bagira
2022-04-28 00:45:47 +02:00
parent 39a736775e
commit f2e25e2629
10 changed files with 154 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
import Dictionary from '../Types/Dictionary';
describe('Dictionary', () => {
test('should allow basic types compile', () => {
const user: Dictionary<string> = {
user: 'test',
};
expect(user).toBeTruthy();
});
test('should compile', () => {
const user: Dictionary<{ [x: string]: string }> = {
user: { use: 'welcome' },
};
expect(user).toBeTruthy();
});
});

View File

@@ -0,0 +1,12 @@
import ErrorResponse from '../Types/API/ErrorResponse';
describe('ErrorResponse', () => {
test('should return a valid error response object', () => {
const errorResponseObject: ErrorResponse = new ErrorResponse(500, {
error: 'Internal Server Error',
});
expect(errorResponseObject.statusCode).toBe(500);
expect(errorResponseObject.data).toEqual({
error: 'Internal Server Error',
});
});
});

View File

@@ -0,0 +1,16 @@
import HTTPMethod from '../Types/API/HTTPMethod';
describe('HTTPMethod', () => {
test('HTTPMethod.GET should be GET', () => {
expect(HTTPMethod.GET).toBe('GET');
});
test('HTTPMethod.POST should be POST', () => {
expect(HTTPMethod.POST).toBe('POST');
});
test('HTTPMethod.PUT should be PUT', () => {
expect(HTTPMethod.PUT).toBe('PUT');
});
test('HTTPMethod.DELETE should be DELETE', () => {
expect(HTTPMethod.DELETE).toBe('DELETE');
});
});

View File

@@ -0,0 +1,10 @@
import Headers from '../Types/API/Headers';
describe('Headers', () => {
test('should compile', () => {
const headers: Headers = {
accept: 'application/json',
'x-api-number': '2',
};
expect(headers).toBeTruthy();
});
});

View File

@@ -0,0 +1,8 @@
import Hostname from '../Types/API/Hostname';
describe('Hostname', () => {
test('new Hostname(hostname) should return a valid object', () => {
const hostnameObject: Hostname = new Hostname('localhost:5000');
expect(hostnameObject.hostname).toBeTruthy();
expect(hostnameObject.hostname).toBe('localhost:5000');
});
});

View File

@@ -0,0 +1,16 @@
import Protocol from '../Types/API/Protocol';
describe('Protocol', () => {
test('Protocol.HTTPS should be https://', () => {
expect(Protocol.HTTPS).toBe('https://');
});
test('Protocol.HTTP should be https://', () => {
expect(Protocol.HTTP).toBe('http://');
});
test('Protocol.WS should be ws://', () => {
expect(Protocol.WS).toBe('ws://');
});
test('Protocol.MONGO_DB should be mongodb://', () => {
expect(Protocol.MONGO_DB).toBe('mongodb://');
});
});

View File

@@ -0,0 +1,12 @@
import Response from '../Types/API/Response';
describe('Response()', () => {
test('should return a valid response object', () => {
let responseObject: Response;
responseObject = new Response(200, { welcome: 'here' });
expect(responseObject.statusCode).toBe(200);
expect(responseObject.data).toEqual({ welcome: 'here' });
responseObject = new Response(200, [{ welcome: 'here' }]);
expect(responseObject.statusCode).toBe(200);
expect(responseObject.data).toEqual([{ welcome: 'here' }]);
});
});

View File

@@ -0,0 +1,12 @@
import ResponseType from '../Types/API/ResponseType';
describe('ResponseType', () => {
test('ResponsetType.CSV to be csv', () => {
expect(ResponseType.CSV).toBe('csv');
});
test('ResponseType.HTML to be json', () => {
expect(ResponseType.HTML).toBe('html');
});
test('ResponseType.HTML to be', () => {
expect(ResponseType.JSON).toBe('json');
});
});

View File

@@ -0,0 +1,11 @@
import Route from '../Types/API/Route';
describe('Route', () => {
test('new Route should return a create object', () => {
const route: Route = new Route('/api/test');
expect(route.route).toBe('/api/test');
});
test('Route.toString() should return valid string', () => {
const route: Route = new Route('/api/test');
expect(route.toString()).toBe('/api/test');
});
});

42
Common/Tests/URL.test.ts Normal file
View File

@@ -0,0 +1,42 @@
import Hostname from '../Types/API/Hostname';
import Protocol from '../Types/API/Protocol';
import Route from '../Types/API/Route';
import URL from '../Types/API/URL';
describe('URL', () => {
test('new URL() should return a valid object', () => {
const url: URL = new URL(
Protocol.HTTPS,
new Hostname('localhost:5000'),
new Route('/api/test')
);
expect(url.hostname).toBeInstanceOf(Hostname);
expect(url.protocol).toBe('https://');
expect(url.route).toBeInstanceOf(Route);
expect(url.isHttps()).toBe(true);
expect(url.toString).toBeTruthy();
});
test('URL.fromString should create URL object', () => {
let url: URL = URL.fromString('https://localhost:5000/api/test');
expect(url).toBeInstanceOf(URL);
expect(url.protocol).toBe('https://');
url = URL.fromString('mongodb://localhost:27017/test');
expect(url).toBeInstanceOf(URL);
expect(url.protocol).toBe('mongodb://');
url = URL.fromString('ws://localhost:5000/api/test');
expect(url).toBeInstanceOf(URL);
expect(url.protocol).toBe('ws://');
url = URL.fromString('wss://localhost:5000/api/test');
expect(url).toBeInstanceOf(URL);
expect(url.protocol).toBe('wss://');
});
test('URL.toString should return a valid URL', () => {
const url: URL = new URL(
Protocol.HTTPS,
new Hostname('localhost:5000'),
new Route('/api/test')
);
expect(url.toString()).toBe('https://localhost:5000/api/test');
});
});