Skip to content

Commit 40b2ace

Browse files
committed
added unit test for validatePhoneNumber method
1 parent 876d5d9 commit 40b2ace

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

__test__/pet.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const app = require("../app");
22
const request = require("supertest");
33

44
describe("Pet Service", () => {
5-
describe("Normal scenario", () => {
5+
describe("when a valid object is posted", () => {
66
it("should response with 201 when new pet created with all the fields", async () => {
77
const response = await request(app).post("/api/pet").send({
88
id: 5,
@@ -24,7 +24,7 @@ describe("Pet Service", () => {
2424
});
2525
});
2626

27-
describe("Negative scenario", () => {
27+
describe("when name and type is missing", () => {
2828
it("should response with 400 when name or type is missing for the pet", async () => {
2929
const bodyData = [
3030
{

__test__/utils.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { validatePhoneNumber } = require("../helper/utils");
2+
3+
it("should return false if non number string is passed", () => {
4+
expect(validatePhoneNumber("onetwothree")).toBe(false);
5+
});
6+
7+
test("it returns false when a string argument is passed", () => {
8+
expect(validatePhoneNumber("onetwothree")).toBe(false);
9+
});
10+
11+
describe("Validate Phone Number", () => {
12+
describe("when valid phone number is passed", () => {
13+
it("should return true for a valid Bangladeshi phone number", () => {
14+
const phoneNumber = "01622971272";
15+
expect(validatePhoneNumber(phoneNumber)).toBe(true);
16+
});
17+
18+
it('should return true for a valid Bangladeshi phone number wth the "+" sign', () => {
19+
const phoneNumber = "+8801622971272";
20+
expect(validatePhoneNumber(phoneNumber)).toBe(true);
21+
});
22+
it('should return true for a valid Bangladeshi phone number without the "+" sign', () => {
23+
const phoneNumber = "8801622971272";
24+
expect(validatePhoneNumber(phoneNumber)).toBe(true);
25+
});
26+
27+
it('should return true for a valid Bangladeshi phone number the "0088" prefix', () => {
28+
const phoneNumber = "008801622971272";
29+
expect(validatePhoneNumber(phoneNumber)).toBe(true);
30+
});
31+
});
32+
33+
describe("when invalid phone number is passed", () => {
34+
it("should return false for an invalid phone number", () => {
35+
const phoneNumber = "+8801234";
36+
expect(validatePhoneNumber(phoneNumber)).toBe(false);
37+
});
38+
39+
it("should return false for a phone number with an incorrect digit", () => {
40+
const phoneNumber = "+88012345678A";
41+
expect(validatePhoneNumber(phoneNumber)).toBe(false);
42+
});
43+
44+
it("should return false for a phone number with more than 11 digits", () => {
45+
const phoneNumber = "+88012345678901";
46+
expect(validatePhoneNumber(phoneNumber)).toBe(false);
47+
});
48+
49+
it("should return false for a phone number with less than 11 digits", () => {
50+
const phoneNumber = "+88012345678";
51+
expect(validatePhoneNumber(phoneNumber)).toBe(false);
52+
});
53+
});
54+
});

helper/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.validatePhoneNumber = (phoneNumber) => {
2+
const regex = /^(?:\+?88|0088)?01[15-9]\d{8}$/;
3+
const isValid = regex.test(phoneNumber);
4+
return isValid;
5+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "app.js",
66
"scripts": {
77
"start": "nodemon server.js",
8-
"test": "jest --watch"
8+
"test": "jest --watch --verbose"
99
},
1010
"keywords": [
1111
"nodejs",

0 commit comments

Comments
 (0)