mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
Merge pull request #224 from Humed-Muhammad/test-branch
🧪 TEST: Port and Phone class
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#!/bin/sh
|
||||
. "$(dirname "$0")/_/husky.sh"
|
||||
|
||||
# npm run lint
|
||||
npm run lint
|
||||
@@ -25,6 +25,6 @@ export default class Faker {
|
||||
}
|
||||
|
||||
public static generatePhone(): Phone {
|
||||
return new Phone(faker.phone.phoneNumber());
|
||||
return new Phone(faker.phone.phoneNumberFormat());
|
||||
}
|
||||
}
|
||||
|
||||
37
Common/Tests/Types/Phone.test.ts
Normal file
37
Common/Tests/Types/Phone.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import BadDataException from '../../Types/Exception/BadDataException';
|
||||
import Phone from '../../Types/Phone';
|
||||
|
||||
describe('Testing Class Phone', () => {
|
||||
test('Should create a phone if the phone is valid phone number', () => {
|
||||
expect(new Phone('+251912974103').toString()).toEqual('+251912974103');
|
||||
expect(new Phone('961-770-7727').phone).toEqual('961-770-7727');
|
||||
expect(new Phone('943-627-0355').phone).toEqual('943-627-0355');
|
||||
expect(new Phone('282.652.3201').phone).toEqual('282.652.3201');
|
||||
});
|
||||
|
||||
test('Phone.phone should be mutatable', () => {
|
||||
const value: Phone = new Phone('+251912974103');
|
||||
value.phone = '+251925974121';
|
||||
expect(value.phone).toEqual('+251925974121');
|
||||
expect(value.toString()).toEqual('+251925974121');
|
||||
});
|
||||
|
||||
test('Creating phone number with invalid format should throw BadDataException', () => {
|
||||
expect(() => {
|
||||
new Phone('25192599879079074121');
|
||||
}).toThrowError(BadDataException);
|
||||
});
|
||||
|
||||
test('try to mutating Phone.phone with invalid value should throw an BadDataExcepection', () => {
|
||||
const value: Phone = new Phone('+251912974103');
|
||||
expect(() => {
|
||||
value.phone = '567';
|
||||
}).toThrowError(BadDataException);
|
||||
expect(() => {
|
||||
value.phone = '278@$90> ';
|
||||
}).toThrow('Phone is not in valid format.');
|
||||
expect(() => {
|
||||
value.phone = 'hgjuit879';
|
||||
}).toThrowError(BadDataException);
|
||||
});
|
||||
});
|
||||
39
Common/Tests/Types/Port.test.ts
Normal file
39
Common/Tests/Types/Port.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import BadDataException from '../../Types/Exception/BadDataException';
|
||||
import Port from '../../Types/Port';
|
||||
import PositiveNumber from '../../Types/PositiveNumber';
|
||||
|
||||
describe('Testing class port', () => {
|
||||
test('should return a posetive number', () => {
|
||||
const value: Port = new Port(3000);
|
||||
expect(value.port.positiveNumber).toBeGreaterThanOrEqual(0);
|
||||
expect(new Port('6000').port.positiveNumber).toEqual(6000);
|
||||
});
|
||||
|
||||
test('should throw exception "Port should be in the range from 0 to 65535"', () => {
|
||||
expect(() => {
|
||||
new Port(67000);
|
||||
}).toThrow('Port should be in the range from 0 to 65535');
|
||||
});
|
||||
|
||||
test('Port.port should be mutatable', () => {
|
||||
const value: Port = new Port(5000);
|
||||
value.port = new PositiveNumber(7000);
|
||||
expect(value.port.positiveNumber).toEqual(7000);
|
||||
expect(value.port.toNumber()).toEqual(7000);
|
||||
});
|
||||
|
||||
test('try to mutating Port.port with invalid value should throw an BadDataExcepection', () => {
|
||||
const value: Port = new Port(3000);
|
||||
expect(() => {
|
||||
value.port = new PositiveNumber('hj567');
|
||||
}).toThrowError(BadDataException);
|
||||
expect(() => {
|
||||
value.port = new PositiveNumber(-6000);
|
||||
}).toThrow(BadDataException);
|
||||
});
|
||||
|
||||
test('If the supplied port is string type, is should convert it to number before creating port', () => {
|
||||
const value: Port = new Port('6000');
|
||||
expect(typeof value.port.positiveNumber).toBe('number');
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { FindOperator } from 'typeorm';
|
||||
import DatabaseProperty from './Database/DatabaseProperty';
|
||||
import BadDataException from './Exception/BadDataException';
|
||||
|
||||
export default class Phone extends DatabaseProperty {
|
||||
private _phone: string = '';
|
||||
@@ -16,6 +17,12 @@ export default class Phone extends DatabaseProperty {
|
||||
* throw new BadDataException('Phone is not in valid format.');
|
||||
* }
|
||||
*/
|
||||
const re: RegExp =
|
||||
/^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/; // regex for international phone numbers format based on (ITU-T E.123)
|
||||
const isValid: boolean = re.test(v);
|
||||
if (!isValid) {
|
||||
throw new BadDataException('Phone is not in valid format.');
|
||||
}
|
||||
this._phone = v;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user