Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: rewrite phone tests #396

Merged
merged 5 commits into from
Feb 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/);
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
});

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