From 37e747ac86c2fb3852b87d246040919456f4b3bf Mon Sep 17 00:00:00 2001 From: Nikhil Thombare Date: Tue, 28 Jan 2025 20:48:36 +0530 Subject: [PATCH] Added test coverage --- tests/PhoneNumber.test.ts | 121 ++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 52 deletions(-) diff --git a/tests/PhoneNumber.test.ts b/tests/PhoneNumber.test.ts index e26c8e76e..f7502060b 100644 --- a/tests/PhoneNumber.test.ts +++ b/tests/PhoneNumber.test.ts @@ -3,44 +3,80 @@ import { Kind } from 'graphql/language'; import { GraphQLPhoneNumber } from '../src/scalars/PhoneNumber.js'; +function PhoneNumberError(value: string) { + return `Invalid phone number: ${value}. Please ensure it's in a valid format. The country code is optional, and Spaces and dashes are allowed. Examples: +1 (123) 456-7890, +44 (20) 2121 2222, or 123 456-7890.` +} + describe('PhoneNumber', () => { - describe('valid', () => { - test('serialize', () => { - expect(GraphQLPhoneNumber.serialize('+16075551234')).toBe('+16075551234'); + describe('valid formats', () => { + describe('with country code', () => { + test('serialize', () => { + expect(GraphQLPhoneNumber.serialize('+16075551234')).toBe('+16075551234'); + }); + + test('parseValue', () => { + expect(GraphQLPhoneNumber.parseValue('+16075551234')).toBe('+16075551234'); + }); + + test('parseLiteral', () => { + expect( + GraphQLPhoneNumber.parseLiteral({ value: '+16075551234', kind: Kind.STRING }, {}), + ).toBe('+16075551234'); + }); }); + describe('without country code', () => { + test('serialize', () => { + expect(() => GraphQLPhoneNumber.serialize('7895551234')).not.toThrow() + }); - test('parseValue', () => { - expect(GraphQLPhoneNumber.parseValue('+16075551234')).toBe('+16075551234'); + test('parseValue', () => { + expect(() => GraphQLPhoneNumber.parseValue('123 456-7890')).not.toThrow() + }); + + test('parseLiteral', () => { + expect(() => + GraphQLPhoneNumber.parseLiteral({ value: '789 555 1234', kind: Kind.STRING }, {}), + ).not.toThrow(); + }); }); + describe('different formatting', () => { + test('serialize', () => { + expect(() => GraphQLPhoneNumber.serialize('62-(21)-9175-5194')).not.toThrow() + }); + + test('serialize', () => { + expect(() => GraphQLPhoneNumber.serialize('+622191755194')).not.toThrow() + }); + + test('parseValue', () => { + expect(() => GraphQLPhoneNumber.parseValue('+62 (21) 9175 5194')).not.toThrow() + }); - test('parseLiteral', () => { - expect( - GraphQLPhoneNumber.parseLiteral({ value: '+16075551234', kind: Kind.STRING }, {}), - ).toBe('+16075551234'); + test('parseLiteral', () => { + expect(() => + GraphQLPhoneNumber.parseLiteral({ value: '+1 (123) 456-7890', kind: Kind.STRING }, {}), + ).not.toThrow(); + }); }); }); - describe('invalid', () => { - describe('not a phone number', () => { + describe('invalid case', () => { + describe('contains Non-Numeric Characters', () => { test('serialize', () => { - expect(() => GraphQLPhoneNumber.serialize('this is not a phone number')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); + expect(() => GraphQLPhoneNumber.serialize('98aaa333')).toThrow(PhoneNumberError('98aaa333')); }); test('parseValue', () => { - expect(() => GraphQLPhoneNumber.parseValue('this is not a phone number')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); + expect(() => GraphQLPhoneNumber.parseValue('98aaa333ppp')).toThrow(PhoneNumberError('98aaa333ppp')); }); test('parseLiteral', () => { expect(() => GraphQLPhoneNumber.parseLiteral( - { value: 'this is not a phone number', kind: Kind.STRING }, + { value: '98aa', kind: Kind.STRING }, {}, ), - ).toThrow(/^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/); + ).toThrow(PhoneNumberError('98aa')); }); }); @@ -60,65 +96,46 @@ describe('PhoneNumber', () => { }); }); - describe('too long', () => { + describe('wrong formate', () => { test('serialize', () => { - expect(() => GraphQLPhoneNumber.serialize('+1789555123456789')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); - }); - - test('parseValue', () => { - expect(() => GraphQLPhoneNumber.parseValue('+1789555123456789')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); + expect(() => GraphQLPhoneNumber.serialize('+17 89- 5')).toThrow(PhoneNumberError('+17 89- 5')); }); - test('parseLiteral', () => { - expect(() => - GraphQLPhoneNumber.parseLiteral({ value: '+1789555123456789', kind: Kind.STRING }, {}), - ).toThrow(/^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/); + test('serialize', () => { + expect(() => GraphQLPhoneNumber.serialize('+1 ( 123 ) 456-7890')).toThrow(PhoneNumberError('+1 ( 123 ) 456-7890')); }); - }); - describe('too small', () => { test('serialize', () => { - expect(() => GraphQLPhoneNumber.serialize('+123')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); + expect(() => GraphQLPhoneNumber.serialize('+1[123]456 7890')).toThrow(PhoneNumberError('+1[123]456 7890')); }); test('parseValue', () => { - expect(() => GraphQLPhoneNumber.parseValue('+123')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); + expect(() => GraphQLPhoneNumber.parseValue('+(178)95 55 5678')).toThrow(PhoneNumberError('+(178)95 55 5678')); }); test('parseLiteral', () => { expect(() => - GraphQLPhoneNumber.parseLiteral({ value: '+123', kind: Kind.STRING }, {}), - ).toThrow(/^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/); + GraphQLPhoneNumber.parseLiteral({ value: '1789 [555] 1234', kind: Kind.STRING }, {}), + ).toThrow(PhoneNumberError('1789 [555] 1234')); }); }); - describe('no plus sign', () => { + describe('too small', () => { test('serialize', () => { - expect(() => GraphQLPhoneNumber.serialize('17895551234')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); + expect(() => GraphQLPhoneNumber.serialize('+12')).toThrow(PhoneNumberError('+12')); }); test('parseValue', () => { - expect(() => GraphQLPhoneNumber.parseValue('17895551234')).toThrow( - /^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/, - ); + expect(() => GraphQLPhoneNumber.parseValue('+12')).toThrow(PhoneNumberError('+12')); }); test('parseLiteral', () => { expect(() => - GraphQLPhoneNumber.parseLiteral({ value: '17895551234', kind: Kind.STRING }, {}), - ).toThrow(/^Value is not a valid phone number of the form \+17895551234 \(7-15 digits\)/); + GraphQLPhoneNumber.parseLiteral({ value: '+12', kind: Kind.STRING }, {}), + ).toThrow(PhoneNumberError('+12')); }); }); + describe('support more countries', () => { test('support Singapore numbers - 10 digits', () => { expect(() => {