From e9898fc613dfb488473a31af4e45528beb721c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Naro=C5=BCnik?= Date: Tue, 1 Feb 2022 22:59:36 +0100 Subject: [PATCH 1/4] test: rewrite phone tests --- test/phone.spec.ts | 146 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 112 insertions(+), 34 deletions(-) diff --git a/test/phone.spec.ts b/test/phone.spec.ts index 849cd0bb2f3..64e1f1dae00 100644 --- a/test/phone.spec.ts +++ b/test/phone.spec.ts @@ -1,47 +1,125 @@ -import { describe, expect, it, vi } from 'vitest'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; import { faker } from '../dist/cjs'; -describe('phone', () => { - describe('phoneNumber()', () => { - it('returns a random phoneNumber with a random format', () => { - const spy_helpers_replaceSymbolWithNumber = vi.spyOn( - faker.helpers, - 'replaceSymbolWithNumber' - ); +const seededRuns = [ + { + seed: 42, + expectations: { + phoneNumber: { + noArgs: '891.775.5141', + }, + phoneNumberFormat: { + noArgs: '479-377-5514', + phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(479) 377-5514' }, + }, + }, + }, + { + seed: 1337, + expectations: { + phoneNumber: { + noArgs: '(612) 454-0325', + }, + phoneNumberFormat: { + noArgs: '451-325-4032', + phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(451) 325-4032' }, + }, + }, + }, + { + seed: 1211, + expectations: { + phoneNumber: { + noArgs: '1-587-319-0616 x27431', + }, + phoneNumberFormat: { + noArgs: '948-821-9061', + phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(948) 821-9061' }, + }, + }, + }, +]; - const phone_number = faker.phone.phoneNumber(); +const functionNames = ['phoneNumber', 'phoneNumberFormat']; - expect(phone_number).match(/\d/); - expect(spy_helpers_replaceSymbolWithNumber).toHaveBeenCalled(); +const NON_SEEDED_BASED_RUN = 25; - spy_helpers_replaceSymbolWithNumber.mockRestore(); - }); +describe('phone', () => { + beforeEach(() => { + faker.locale = 'en'; }); - describe('phoneNumberFormat()', () => { - it('returns phone number with requested format (Array index)', () => { - faker.locale = 'en'; - for (let i = 0; i < 10; i++) { - const phone_number = faker.phone.phoneNumberFormat(1); - expect(phone_number).match(/\(\d\d\d\) \d\d\d-\d\d\d\d/); - } - }); + for (const { seed, expectations } of seededRuns) { + describe(`seed: ${seed}`, () => { + for (const functionName of functionNames) { + it(`${functionName}()`, () => { + faker.seed(seed); - it('returns phone number with proper format US (Array index)', () => { - faker.locale = 'en'; - for (let i = 0; i < 25; i++) { - const phone_number = faker.phone.phoneNumberFormat(1); - console.log(phone_number); - expect(phone_number).match(/\([2-9]\d\d\) [2-9]\d\d-\d\d\d\d/); - } - }); + const actual = faker.phone[functionName](); - it('returns phone number with proper format CA (Array index)', () => { - faker.locale = 'en_CA'; - for (let i = 0; i < 25; i++) { - const phone_number = faker.phone.phoneNumberFormat(1); - expect(phone_number).match(/\([2-9]\d\d\)[2-9]\d\d-\d\d\d\d/); + expect(actual).toEqual(expectations[functionName].noArgs); + }); } + + describe('phoneNumberFormat', () => { + const { arrayIndex, expected } = + expectations.phoneNumberFormat.phoneFormatsArrayIndex; + it(`should return ${expected} for ${arrayIndex}`, () => { + faker.seed(seed); + + const actual = faker.phone.phoneNumberFormat(arrayIndex); + + expect(actual).toEqual(expected); + }); + }); }); + } + + // Create and log-back the seed for debug purposes + faker.seed(Math.ceil(Math.random() * 1_000_000_000)); + + describe(`random seeded tests for seed ${faker.seedValue}`, () => { + for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe('phoneNumber()', () => { + it('should return a random phoneNumber with a random format', () => { + const fakeHelpers_replaceSymbolWithNumberSpy = vi.spyOn( + faker.helpers, + 'replaceSymbolWithNumber' + ); + + const phoneNumber = faker.phone.phoneNumber(); + + expect(phoneNumber).match(/\d/); + expect(fakeHelpers_replaceSymbolWithNumberSpy).toHaveBeenCalled(); + }); + }); + + describe('phoneNumberFormat()', () => { + it('should return phone number with proper US format (Array index)', () => { + faker.locale = 'en'; + const phoneNumber = faker.phone.phoneNumberFormat(1); + expect(phoneNumber).match(/\([2-9]\d\d\) [2-9]\d\d-\d\d\d\d/); + }); + + it('should return phone number with proper CA format (Array index)', () => { + faker.locale = 'en_CA'; + const phoneNumber = faker.phone.phoneNumberFormat(1); + expect(phoneNumber).match(/\([2-9]\d\d\)[2-9]\d\d-\d\d\d\d/); + }); + + it('should return phone number with proper PL format (Array index)', () => { + faker.locale = 'pl'; + const phoneNumber = faker.phone.phoneNumberFormat(1); + expect(phoneNumber).match(/13-\d{3}-\d{2}-\d{2}/); + }); + }); + + describe('phoneFormats()', () => { + it('should return random phone number format', () => { + const phoneFormat = faker.phone.phoneFormats(); + expect(faker.definitions.phone_number.formats).contain(phoneFormat); + }); + }); + } }); }); From af70cfdca9de8465f0bf807e1ebf81b762d65584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Naro=C5=BCnik?= <4689234+piotrekn@users.noreply.github.com> Date: Thu, 3 Feb 2022 21:54:44 +0100 Subject: [PATCH 2/4] test: add seeded tests for phoneFormats --- test/phone.spec.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/phone.spec.ts b/test/phone.spec.ts index 64e1f1dae00..1958611218f 100644 --- a/test/phone.spec.ts +++ b/test/phone.spec.ts @@ -12,6 +12,9 @@ const seededRuns = [ noArgs: '479-377-5514', phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(479) 377-5514' }, }, + phoneFormats: { + noArgs: '!##.!##.####', + }, }, }, { @@ -24,6 +27,9 @@ const seededRuns = [ noArgs: '451-325-4032', phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(451) 325-4032' }, }, + phoneFormats: { + noArgs: '(!##) !##-####', + }, }, }, { @@ -36,11 +42,14 @@ const seededRuns = [ noArgs: '948-821-9061', phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(948) 821-9061' }, }, + phoneFormats: { + noArgs: '1-!##-!##-#### x#####', + }, }, }, ]; -const functionNames = ['phoneNumber', 'phoneNumberFormat']; +const functionNames = ['phoneNumber', 'phoneNumberFormat', 'phoneFormats']; const NON_SEEDED_BASED_RUN = 25; From cf740f6b917d031e8d0bf344f82f8cf03e1cb7db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Naro=C5=BCnik?= <4689234+piotrekn@users.noreply.github.com> Date: Fri, 4 Feb 2022 09:45:32 +0100 Subject: [PATCH 3/4] test: remove spy from test to test api only --- test/phone.spec.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/phone.spec.ts b/test/phone.spec.ts index 1958611218f..a763724e323 100644 --- a/test/phone.spec.ts +++ b/test/phone.spec.ts @@ -91,15 +91,9 @@ describe('phone', () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { describe('phoneNumber()', () => { it('should return a random phoneNumber with a random format', () => { - const fakeHelpers_replaceSymbolWithNumberSpy = vi.spyOn( - faker.helpers, - 'replaceSymbolWithNumber' - ); - const phoneNumber = faker.phone.phoneNumber(); expect(phoneNumber).match(/\d/); - expect(fakeHelpers_replaceSymbolWithNumberSpy).toHaveBeenCalled(); }); }); From bd048b62ee117622d39474966ead44606f5047bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Naro=C5=BCnik?= Date: Fri, 4 Feb 2022 10:28:51 +0100 Subject: [PATCH 4/4] refactor: remove redundant import Co-authored-by: Shinigami --- test/phone.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/phone.spec.ts b/test/phone.spec.ts index a763724e323..6a8cfdc92a1 100644 --- a/test/phone.spec.ts +++ b/test/phone.spec.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { beforeEach, describe, expect, it } from 'vitest'; import { faker } from '../dist/cjs'; const seededRuns = [