mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
Merge pull request #95 from OneUptime/59-tests-for-commontypesexceptionts
Add tests for exceptions
This commit is contained in:
16
Common/Tests/Types/Exception/ApiException.test.ts
Normal file
16
Common/Tests/Types/Exception/ApiException.test.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import APIException from '../../../Types/Exception/ApiException';
|
||||
|
||||
describe('ApiException', () => {
|
||||
test('should return error message from ApiException', () => {
|
||||
expect(
|
||||
new APIException('Server responded with a status code of 5000')
|
||||
.message
|
||||
).toBe('Server responded with a status code of 5000');
|
||||
});
|
||||
|
||||
test('should return 2 as the code for ApiException', () => {
|
||||
expect(
|
||||
new APIException('Server responded with a status code of 5000').code
|
||||
).toBe(2);
|
||||
});
|
||||
});
|
||||
15
Common/Tests/Types/Exception/BadDataException.test.ts
Normal file
15
Common/Tests/Types/Exception/BadDataException.test.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import BadDataException from '../../../Types/Exception/BadDataException';
|
||||
|
||||
describe('BadDataException', () => {
|
||||
test('should throw a not bad data exception', () => {
|
||||
expect(() => {
|
||||
throw new BadDataException('This is not a valid IPv4 address');
|
||||
}).toThrow('This is not a valid IPv4 address');
|
||||
});
|
||||
|
||||
test('should return 400 as the code for BadDataException', () => {
|
||||
expect(
|
||||
new BadDataException('This is not a valid IPv4 address').code
|
||||
).toBe(400);
|
||||
});
|
||||
});
|
||||
16
Common/Tests/Types/Exception/BadOperationException.test.ts
Normal file
16
Common/Tests/Types/Exception/BadOperationException.test.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import BadOperationException from '../../../Types/Exception/BadOperationException';
|
||||
|
||||
describe('BadOperationException', () => {
|
||||
test('should return error message from BadOperationException', () => {
|
||||
expect(
|
||||
new BadOperationException('Cannot await a non-thenable code')
|
||||
.message
|
||||
).toBe('Cannot await a non-thenable code');
|
||||
});
|
||||
|
||||
test('should return 5 as the code for BadOperationException', () => {
|
||||
expect(
|
||||
new BadOperationException('Cannot await a non-thenable code').code
|
||||
).toBe(5);
|
||||
});
|
||||
});
|
||||
15
Common/Tests/Types/Exception/BadRequestException.test.ts
Normal file
15
Common/Tests/Types/Exception/BadRequestException.test.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import BadRequestException from '../../../Types/Exception/BadRequestException';
|
||||
|
||||
describe('BadRequestException', () => {
|
||||
test('should throw a bad request exception', () => {
|
||||
expect(() => {
|
||||
throw new BadRequestException('Forbidden. A token is needed');
|
||||
}).toThrow('Forbidden. A token is needed');
|
||||
});
|
||||
|
||||
test('should return 400 as the code for BadRequestException', () => {
|
||||
expect(
|
||||
new BadRequestException('Forbidden. A token is needed').code
|
||||
).toBe(400);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
import DatabaseNotConnectedException from '../../../Types/Exception/DatabaseNotConnectedException';
|
||||
|
||||
describe('DatabaseNotConnectedException', () => {
|
||||
test('should return the error message set in database exception', () => {
|
||||
expect(new DatabaseNotConnectedException().message).toBe(
|
||||
'Database not connected'
|
||||
);
|
||||
});
|
||||
|
||||
test('should return 3 as the code for DatabaseNotConnectedException', () => {
|
||||
expect(new DatabaseNotConnectedException().code).toBe(3);
|
||||
});
|
||||
});
|
||||
21
Common/Tests/Types/Exception/Exception.test.ts
Normal file
21
Common/Tests/Types/Exception/Exception.test.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import Exception from '../../../Types/Exception/Exception';
|
||||
|
||||
describe('Exception', () => {
|
||||
test('should throw an exception from exception class', () => {
|
||||
expect(() => {
|
||||
throw new Exception(1, 'General exception error message');
|
||||
}).toThrow('General exception error message');
|
||||
});
|
||||
|
||||
test('should return error message', () => {
|
||||
expect(
|
||||
new Exception(0, 'This code has not been implemented').message
|
||||
).toBe('This code has not been implemented');
|
||||
});
|
||||
|
||||
test('should return 1 as the code for Exception', () => {
|
||||
expect(new Exception(1, 'This is not a valid IPv4 address').code).toBe(
|
||||
1
|
||||
);
|
||||
});
|
||||
});
|
||||
13
Common/Tests/Types/Exception/NotImplementedException.test.ts
Normal file
13
Common/Tests/Types/Exception/NotImplementedException.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import NotImplementedException from '../../../Types/Exception/NotImplementedException';
|
||||
|
||||
describe('NotImplementedException', () => {
|
||||
test('should throw a not implemented exception', () => {
|
||||
expect(() => {
|
||||
throw new NotImplementedException();
|
||||
}).toThrow('This code is not implemented');
|
||||
});
|
||||
|
||||
test('should return 0 as the code for NotImplementedException', () => {
|
||||
expect(new NotImplementedException().code).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -7,8 +7,8 @@ export default class Exception extends Error {
|
||||
return this._code;
|
||||
}
|
||||
|
||||
public set code(v: ExceptionCode) {
|
||||
this._code = v;
|
||||
public set code(value: ExceptionCode) {
|
||||
this._code = value;
|
||||
}
|
||||
|
||||
public constructor(code: ExceptionCode, message: string) {
|
||||
|
||||
Reference in New Issue
Block a user