Skip to content

Commit

Permalink
test: rewrite phone tests (faker-js#396)
Browse files Browse the repository at this point in the history
Co-authored-by: Shinigami <[email protected]>
  • Loading branch information
2 people authored and demipel8 committed Mar 11, 2022
1 parent 5672d74 commit 1591668
Showing 1 changed file with 115 additions and 34 deletions.
149 changes: 115 additions & 34 deletions test/phone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,128 @@
import { describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it } 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' },
},
phoneFormats: {
noArgs: '!##.!##.####',
},
},
},
{
seed: 1337,
expectations: {
phoneNumber: {
noArgs: '(612) 454-0325',
},
phoneNumberFormat: {
noArgs: '451-325-4032',
phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(451) 325-4032' },
},
phoneFormats: {
noArgs: '(!##) !##-####',
},
},
},
{
seed: 1211,
expectations: {
phoneNumber: {
noArgs: '1-587-319-0616 x27431',
},
phoneNumberFormat: {
noArgs: '948-821-9061',
phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(948) 821-9061' },
},
phoneFormats: {
noArgs: '1-!##-!##-#### x#####',
},
},
},
];

const phone_number = faker.phone.phoneNumber();
const functionNames = ['phoneNumber', 'phoneNumberFormat', 'phoneFormats'];

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 phoneNumber = faker.phone.phoneNumber();

expect(phoneNumber).match(/\d/);
});
});

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);
});
});
}
});
});

0 comments on commit 1591668

Please sign in to comment.