From 426eeb510cce5678fdc4d34ffb744adff6ec4dc7 Mon Sep 17 00:00:00 2001 From: Humed Essie Date: Sat, 13 Aug 2022 19:16:15 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20Port=20and=20Phone?= =?UTF-8?q?=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Tests/Types/Phone.test.ts | 37 +++++++++++++++++++++++++++++ Common/Tests/Types/Port.test.ts | 40 ++++++++++++++++++++++++++++++++ Common/Types/Phone.ts | 6 +++++ Common/package.json | 2 ++ 4 files changed, 85 insertions(+) create mode 100644 Common/Tests/Types/Phone.test.ts create mode 100644 Common/Tests/Types/Port.test.ts diff --git a/Common/Tests/Types/Phone.test.ts b/Common/Tests/Types/Phone.test.ts new file mode 100644 index 0000000000..b756da79b0 --- /dev/null +++ b/Common/Tests/Types/Phone.test.ts @@ -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('+1202 588 6500').phone).toEqual('+1202 588 6500'); + }); + + 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('2519259$74121'); + }).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); + }); + +}); diff --git a/Common/Tests/Types/Port.test.ts b/Common/Tests/Types/Port.test.ts new file mode 100644 index 0000000000..82078c6617 --- /dev/null +++ b/Common/Tests/Types/Port.test.ts @@ -0,0 +1,40 @@ +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'); + }); +}); diff --git a/Common/Types/Phone.ts b/Common/Types/Phone.ts index be544f1e69..17f26d464c 100644 --- a/Common/Types/Phone.ts +++ b/Common/Types/Phone.ts @@ -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,11 @@ export default class Phone extends DatabaseProperty { * throw new BadDataException('Phone is not in valid format.'); * } */ + const re: RegExp = /^\+(?:[0-9] ?){6,14}[0-9]$/; // 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; } diff --git a/Common/package.json b/Common/package.json index c4db239814..e6b6155611 100644 --- a/Common/package.json +++ b/Common/package.json @@ -5,6 +5,8 @@ "main": "index.js", "scripts": { "test": "jest", + "test-port": "jest Tests/Types/Port.test.ts", + "test-phone": "jest Tests/Types/Phone.test.ts", "compile": "tsc" }, "author": "", From eefb4d2cb93832fe87a1ab32313f98ab194c1a29 Mon Sep 17 00:00:00 2001 From: Humed Essie Date: Sat, 13 Aug 2022 19:39:08 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=A7=B9=20CLEAN:=20fixing=20lint=20err?= =?UTF-8?q?ors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Tests/Types/Phone.test.ts | 2 -- Common/Tests/Types/Port.test.ts | 17 ++++++++--------- Common/Types/Phone.ts | 6 +++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Common/Tests/Types/Phone.test.ts b/Common/Tests/Types/Phone.test.ts index b756da79b0..23d6c1c62f 100644 --- a/Common/Tests/Types/Phone.test.ts +++ b/Common/Tests/Types/Phone.test.ts @@ -18,7 +18,6 @@ describe('Testing Class Phone', () => { expect(() => { new Phone('2519259$74121'); }).toThrowError(BadDataException); - }); test('try to mutating Phone.phone with invalid value should throw an BadDataExcepection', () => { @@ -33,5 +32,4 @@ describe('Testing Class Phone', () => { value.phone = 'hgjuit879'; }).toThrowError(BadDataException); }); - }); diff --git a/Common/Tests/Types/Port.test.ts b/Common/Tests/Types/Port.test.ts index 82078c6617..907d126cb1 100644 --- a/Common/Tests/Types/Port.test.ts +++ b/Common/Tests/Types/Port.test.ts @@ -4,17 +4,17 @@ import PositiveNumber from '../../Types/PositiveNumber'; describe('Testing class port', () => { test('should return a posetive number', () => { - const value:Port = new Port(3000); + const value: Port = new Port(3000); expect(value.port.positiveNumber).toBeGreaterThanOrEqual(0); - expect(new Port("6000").port.positiveNumber).toEqual(6000); + 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"); + 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); @@ -25,14 +25,13 @@ describe('Testing class port', () => { 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'); + 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'); diff --git a/Common/Types/Phone.ts b/Common/Types/Phone.ts index 17f26d464c..b0ae2e0a44 100644 --- a/Common/Types/Phone.ts +++ b/Common/Types/Phone.ts @@ -19,9 +19,9 @@ export default class Phone extends DatabaseProperty { */ const re: RegExp = /^\+(?:[0-9] ?){6,14}[0-9]$/; // 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.'); - } + if (!isValid) { + throw new BadDataException('Phone is not in valid format.'); + } this._phone = v; } From f49e8c7a99d0e936514977890df167bb9e33f2ce Mon Sep 17 00:00:00 2001 From: Humed Essie Date: Sat, 13 Aug 2022 19:46:26 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=9A=91=20HOTFIX:=20precommit=20comman?= =?UTF-8?q?d=20(husky)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .husky/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index e3db172b4a..dba9310457 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/bin/sh . "$(dirname "$0")/_/husky.sh" -# npm run lint +npm run lint \ No newline at end of file From f8c143528dba373b72b7b00006b31a0869cd509c Mon Sep 17 00:00:00 2001 From: Humed Essie Date: Sat, 13 Aug 2022 19:49:48 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=94=A5=20REMOVE:=20unwanted=20script?= =?UTF-8?q?=20(package.json)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/Common/package.json b/Common/package.json index e6b6155611..c4db239814 100644 --- a/Common/package.json +++ b/Common/package.json @@ -5,8 +5,6 @@ "main": "index.js", "scripts": { "test": "jest", - "test-port": "jest Tests/Types/Port.test.ts", - "test-phone": "jest Tests/Types/Phone.test.ts", "compile": "tsc" }, "author": "", From ef8251239f2f1e7aa79513a78f16cc9a805d398e Mon Sep 17 00:00:00 2001 From: Humed Essie Date: Sat, 13 Aug 2022 21:58:16 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=8E=A8=20IMPROVE:=20adding=20trycatch?= =?UTF-8?q?=20to=20Faker.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Tests/TestingUtils/Faker.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Common/Tests/TestingUtils/Faker.ts b/Common/Tests/TestingUtils/Faker.ts index 49ca94f070..cb6024519d 100644 --- a/Common/Tests/TestingUtils/Faker.ts +++ b/Common/Tests/TestingUtils/Faker.ts @@ -1,5 +1,6 @@ import { faker } from '@faker-js/faker'; import Email from '../../Types/Email'; +import BadDataException from '../../Types/Exception/BadDataException'; import Name from '../../Types/Name'; import Phone from '../../Types/Phone'; @@ -25,6 +26,11 @@ export default class Faker { } public static generatePhone(): Phone { - return new Phone(faker.phone.phoneNumber()); + try { + const newPhone: Phone = new Phone(faker.phone.phoneNumber()); + return newPhone; + } catch (error) { + throw new BadDataException('Phone number generted is not valid'); + } } } From 2d7290b99e89af23af60a83519f62c37d3b7be9d Mon Sep 17 00:00:00 2001 From: Humed Essie Date: Sat, 13 Aug 2022 22:03:19 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=8E=A8=20IMPROVE:=20Regex=20matching?= =?UTF-8?q?=20for=20phone=20numbers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Tests/Types/Phone.test.ts | 4 ++-- Common/Types/Phone.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Common/Tests/Types/Phone.test.ts b/Common/Tests/Types/Phone.test.ts index 23d6c1c62f..b06f34e96f 100644 --- a/Common/Tests/Types/Phone.test.ts +++ b/Common/Tests/Types/Phone.test.ts @@ -4,7 +4,7 @@ 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('+1202 588 6500').phone).toEqual('+1202 588 6500'); + expect(new Phone('961-770-7727').phone).toEqual('961-770-7727'); }); test('Phone.phone should be mutatable', () => { @@ -16,7 +16,7 @@ describe('Testing Class Phone', () => { test('Creating phone number with invalid format should throw BadDataException', () => { expect(() => { - new Phone('2519259$74121'); + new Phone('25192599879079074121'); }).toThrowError(BadDataException); }); diff --git a/Common/Types/Phone.ts b/Common/Types/Phone.ts index b0ae2e0a44..64607597fc 100644 --- a/Common/Types/Phone.ts +++ b/Common/Types/Phone.ts @@ -17,7 +17,8 @@ export default class Phone extends DatabaseProperty { * throw new BadDataException('Phone is not in valid format.'); * } */ - const re: RegExp = /^\+(?:[0-9] ?){6,14}[0-9]$/; // regex for international phone numbers format based on (ITU-T E.123) + 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.'); From 0d8bbc6afa20efa22ce4ad55ec44e3ac607d89b9 Mon Sep 17 00:00:00 2001 From: Humed Essie Date: Sat, 13 Aug 2022 22:16:01 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=9A=91=20HOTFIX:=20Regex=20and=20Fake?= =?UTF-8?q?r.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Tests/TestingUtils/Faker.ts | 8 +------- Common/Tests/Types/Phone.test.ts | 2 ++ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Common/Tests/TestingUtils/Faker.ts b/Common/Tests/TestingUtils/Faker.ts index cb6024519d..91700c4d2f 100644 --- a/Common/Tests/TestingUtils/Faker.ts +++ b/Common/Tests/TestingUtils/Faker.ts @@ -1,6 +1,5 @@ import { faker } from '@faker-js/faker'; import Email from '../../Types/Email'; -import BadDataException from '../../Types/Exception/BadDataException'; import Name from '../../Types/Name'; import Phone from '../../Types/Phone'; @@ -26,11 +25,6 @@ export default class Faker { } public static generatePhone(): Phone { - try { - const newPhone: Phone = new Phone(faker.phone.phoneNumber()); - return newPhone; - } catch (error) { - throw new BadDataException('Phone number generted is not valid'); - } + return new Phone(faker.phone.phoneNumberFormat()); } } diff --git a/Common/Tests/Types/Phone.test.ts b/Common/Tests/Types/Phone.test.ts index b06f34e96f..dc53474eb4 100644 --- a/Common/Tests/Types/Phone.test.ts +++ b/Common/Tests/Types/Phone.test.ts @@ -5,6 +5,8 @@ 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', () => {