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 company test #493

Merged
merged 1 commit into from
Feb 16, 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
334 changes: 224 additions & 110 deletions test/company.spec.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,234 @@
import { describe, expect, it, vi } from 'vitest';
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';

describe('company', () => {
describe('companyName()', () => {
it('sometimes returns three last names', () => {
const spy_name_lastName = vi.spyOn(faker.name, 'lastName');
const spy_datatype_number = vi
.spyOn(faker.datatype, 'number')
.mockReturnValue(2);

const name = faker.company.companyName();
const parts = name.split(' ');

expect(parts.length).toBe(4); // account for word 'and'
expect(spy_name_lastName).toHaveBeenCalledTimes(3);

spy_datatype_number.mockRestore();
spy_name_lastName.mockRestore();
});
const seededRuns = [
{
seed: 42,
expectations: {
suffixes: ['Inc', 'and Sons', 'LLC', 'Group'],
companyName: 'Schinner - Wiegand',
companySuffix: 'and Sons',
catchPhrase: 'Implemented responsive throughput',
bs: 'seize impactful web services',
catchPhraseAdjective: 'Implemented',
catchPhraseDescriptor: 'explicit',
catchPhraseNoun: 'framework',
bsAdjective: 'dynamic',
bsBuzz: 'seize',
bsNoun: 'portals',
},
},
{
seed: 1337,
expectations: {
suffixes: ['Inc', 'and Sons', 'LLC', 'Group'],
companyName: 'Macejkovic Inc',
companySuffix: 'and Sons',
catchPhrase: 'Expanded leading edge capacity',
bs: 'incentivize efficient initiatives',
catchPhraseAdjective: 'Expanded',
catchPhraseDescriptor: 'demand-driven',
catchPhraseNoun: 'data-warehouse',
bsAdjective: 'global',
bsBuzz: 'incentivize',
bsNoun: 'ROI',
},
},
{
seed: 1211,
expectations: {
suffixes: ['Inc', 'and Sons', 'LLC', 'Group'],
companyName: 'Koch, Trantow and Sanford',
companySuffix: 'Group',
catchPhrase: 'Up-sized high-level success',
bs: 'cultivate bleeding-edge functionalities',
catchPhraseAdjective: 'Up-sized',
catchPhraseDescriptor: 'upward-trending',
catchPhraseNoun: 'system engine',
bsAdjective: 'plug-and-play',
bsBuzz: 'cultivate',
bsNoun: 'experiences',
},
},
];

const NON_SEEDED_BASED_RUN = 5;

const functionNames = [
'suffixes',
'companyName',
'companySuffix',
'catchPhrase',
'bs',
'catchPhraseAdjective',
'catchPhraseDescriptor',
'catchPhraseNoun',
'bsAdjective',
'bsBuzz',
'bsNoun',
];

it('sometimes returns two last names separated by a hyphen', () => {
const spy_name_lastName = vi.spyOn(faker.name, 'lastName');
const spy_datatype_number = vi
.spyOn(faker.datatype, 'number')
.mockReturnValue(1);

const name = faker.company.companyName();
const parts = name.split('-');

expect(parts.length).greaterThanOrEqual(2);
expect(spy_name_lastName).toHaveBeenCalledTimes(2);

spy_datatype_number.mockRestore();
spy_name_lastName.mockRestore();
});

it('sometimes returns a last name with a company suffix', () => {
const spy_company_companySuffix = vi.spyOn(
faker.company,
'companySuffix'
);
const spy_name_lastName = vi.spyOn(faker.name, 'lastName');
const spy_datatype_number = vi
.spyOn(faker.datatype, 'number')
.mockReturnValue(0);

const name = faker.company.companyName();
const parts = name.split(' ');

expect(parts.length).greaterThanOrEqual(2);
expect(spy_name_lastName).toHaveBeenCalledOnce();
expect(spy_company_companySuffix).toHaveBeenCalledOnce();

spy_datatype_number.mockRestore();
spy_name_lastName.mockRestore();
spy_company_companySuffix.mockRestore();
});
describe('company', () => {
afterEach(() => {
faker.locale = 'en';
});

describe('companySuffix()', () => {
it('returns random value from company.suffixes array', () => {
const suffix = faker.company.companySuffix();
expect(faker.company.suffixes()).toContain(suffix);
});
});
for (const { seed, expectations } of seededRuns) {
describe(`seed: ${seed}`, () => {
for (const functionName of functionNames) {
it(`${functionName}()`, () => {
faker.seed(seed);

describe('catchPhrase()', () => {
it('returns phrase comprising of a catch phrase adjective, descriptor, and noun', () => {
const spy_random_arrayElement = vi.spyOn(faker.random, 'arrayElement');
const spy_company_catchPhraseAdjective = vi.spyOn(
faker.company,
'catchPhraseAdjective'
);
const spy_company_catchPhraseDescriptor = vi.spyOn(
faker.company,
'catchPhraseDescriptor'
);
const spy_company_catchPhraseNoun = vi.spyOn(
faker.company,
'catchPhraseNoun'
);

const phrase = faker.company.catchPhrase();

expect(phrase.split(' ').length).greaterThanOrEqual(3);
expect(spy_random_arrayElement).toHaveBeenCalledTimes(3);
expect(spy_company_catchPhraseAdjective).toHaveBeenCalledOnce();
expect(spy_company_catchPhraseDescriptor).toHaveBeenCalledOnce();
expect(spy_company_catchPhraseNoun).toHaveBeenCalledOnce();

spy_random_arrayElement.mockRestore();
spy_company_catchPhraseAdjective.mockRestore();
spy_company_catchPhraseDescriptor.mockRestore();
spy_company_catchPhraseNoun.mockRestore();
const actual = faker.company[functionName]();
expect(actual).toEqual(expectations[functionName]);
});
}
});
});
}

describe('bs()', () => {
it('returns phrase comprising of a BS buzz, adjective, and noun', () => {
const spy_random_arrayElement = vi.spyOn(faker.random, 'arrayElement');
const spy_company_bsBuzz = vi.spyOn(faker.company, 'bsBuzz');
const spy_company_bsAdjective = vi.spyOn(faker.company, 'bsAdjective');
const spy_company_bsNoun = vi.spyOn(faker.company, 'bsNoun');

const bs = faker.company.bs();

expect(typeof bs).toBe('string');
expect(spy_random_arrayElement).toHaveBeenCalledTimes(3);
expect(spy_company_bsBuzz).toHaveBeenCalledOnce();
expect(spy_company_bsAdjective).toHaveBeenCalledOnce();
expect(spy_company_bsNoun).toHaveBeenCalledOnce();

spy_random_arrayElement.mockRestore();
spy_company_bsBuzz.mockRestore();
spy_company_bsAdjective.mockRestore();
spy_company_bsNoun.mockRestore();
});
// 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('suffixes()', () => {
it('should return all suffixes', () => {
const actual = faker.company.suffixes();

expect(actual).toBeTruthy();
expect(faker.definitions.company.suffix).toEqual(actual);
});
});

describe('companyName()', () => {
it('should return a random company name', () => {
const actual = faker.company.companyName();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
});

it('should return a random company name with format 0', () => {
const actual = faker.company.companyName(0);

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(actual).includes(' ');
});

it('should return a random company name with format 1', () => {
const actual = faker.company.companyName(1);

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(actual).includes(' - ');
});

it('should return a random company name with format 2', () => {
const actual = faker.company.companyName(2);

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(actual).includes(', ');
expect(actual).includes(' and ');
});
});

describe('companySuffix()', () => {
it('should return random value from company.suffixes array', () => {
const actual = faker.company.companySuffix();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(faker.definitions.company.suffix).toContain(actual);
});
});

describe('catchPhrase()', () => {
it('should return phrase comprising of a catch phrase adjective, descriptor, and noun', () => {
const actual = faker.company.catchPhrase();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');

const parts = actual.split(' ');

expect(parts.length).greaterThanOrEqual(3);
});
});

describe('bs()', () => {
it('should return phrase comprising of a BS buzz, adjective, and noun', () => {
const actual = faker.company.bs();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');

const parts = actual.split(' ');

expect(parts.length).greaterThanOrEqual(3);
});
});

describe('catchPhraseAdjective()', () => {
it('should return random value from adjective array', () => {
const actual = faker.company.catchPhraseAdjective();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(faker.definitions.company.adjective).toContain(actual);
});
});

describe('catchPhraseDescriptor()', () => {
it('should return random value from descriptor array', () => {
const actual = faker.company.catchPhraseDescriptor();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(faker.definitions.company.descriptor).toContain(actual);
});
});

describe('catchPhraseNoun()', () => {
it('should return random value from noun array', () => {
const actual = faker.company.catchPhraseNoun();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(faker.definitions.company.noun).toContain(actual);
});
});

describe('bsAdjective()', () => {
it('should return random value from bs_adjective array', () => {
const actual = faker.company.bsAdjective();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(faker.definitions.company.bs_adjective).toContain(actual);
});
});

describe('bsBuzz()', () => {
it('should return random value from bs_verb array', () => {
const actual = faker.company.bsBuzz();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(faker.definitions.company.bs_verb).toContain(actual);
});
});

describe('bsNoun()', () => {
it('should return random value from bs_noun array', () => {
const actual = faker.company.bsNoun();

expect(actual).toBeTruthy();
expect(typeof actual).toBe('string');
expect(faker.definitions.company.bs_noun).toContain(actual);
});
});
}
});
});