From 2a059a0ec6f266295340a73217595822d281d081 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 30 Jan 2022 15:04:41 +0100 Subject: [PATCH 1/7] test: rewrite name tests --- test/name.spec.ts | 651 ++++++++++++++++++++++++++-------------------- 1 file changed, 364 insertions(+), 287 deletions(-) diff --git a/test/name.spec.ts b/test/name.spec.ts index 12baa90eac2..0c541955505 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -1,333 +1,410 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { faker } from '../dist/cjs'; -function assertInArray(value: T, array: readonly T[]): void { - const idx = array.indexOf(value); - expect(idx).not.toBe(-1); -} +const seededRuns = [ + { + seed: 42, + expectations: { + firstName: { + noArgs: 'Garnett', + }, + lastName: { + noArgs: 'Hintz', + }, + middleName: { + noArgs: 'b', + }, + findName: { + noArgs: 'Lorene Deckow', + }, + jobTitle: { + noArgs: 'Regional Data Representative', + }, + gender: { + noArgs: 'Cis', + }, + prefix: { + noArgs: 'Mrs.', + }, + suffix: { + noArgs: 'III', + }, + title: { + noArgs: 'Regional Data Representative', + }, + jobDescriptor: { + noArgs: 'Regional', + }, + jobArea: { + noArgs: 'Identity', + }, + jobType: { + noArgs: 'Coordinator', + }, + }, + }, + { + seed: 1337, + expectations: { + firstName: { + noArgs: 'Devyn', + }, + lastName: { + noArgs: 'Gibson', + }, + middleName: { + noArgs: 'a', + }, + findName: { + noArgs: 'Marilyn Effertz', + }, + jobTitle: { + noArgs: 'Future Infrastructure Liaison', + }, + gender: { + noArgs: 'Two* person', + }, + prefix: { + noArgs: 'Mrs.', + }, + suffix: { + noArgs: 'I', + }, + title: { + noArgs: 'Future Infrastructure Liaison', + }, + jobDescriptor: { + noArgs: 'Future', + }, + jobArea: { + noArgs: 'Functionality', + }, + jobType: { + noArgs: 'Engineer', + }, + }, + }, + { + seed: 1211, + expectations: { + firstName: { + noArgs: 'Tito', + }, + lastName: { + noArgs: 'Ward', + }, + middleName: { + noArgs: 'c', + }, + findName: { + noArgs: 'Darrel Sanford', + }, + jobTitle: { + noArgs: 'Chief Division Agent', + }, + gender: { + noArgs: 'Transexual Person', + }, + prefix: { + noArgs: 'Dr.', + }, + suffix: { + noArgs: 'DVM', + }, + title: { + noArgs: 'Chief Division Agent', + }, + jobDescriptor: { + noArgs: 'Chief', + }, + jobArea: { + noArgs: 'Factors', + }, + jobType: { + noArgs: 'Representative', + }, + }, + }, +]; + +const NON_SEEDED_BASED_RUN = 5; + +const functionNames = [ + 'firstName', + 'lastName', + 'middleName', + 'findName', + 'jobTitle', + 'gender', + 'prefix', + 'suffix', + 'title', + 'jobDescriptor', + 'jobArea', + 'jobType', +]; describe('name', () => { - describe('firstName()', () => { - it('returns a random name', () => { - const spy_name_firstName = vi - .spyOn(faker.name, 'firstName') - .mockReturnValue('foo'); - - const first_name = faker.name.firstName(); + afterEach(() => { + faker.locale = 'en'; + }); - expect(first_name).toBe('foo'); + for (const { seed, expectations } of seededRuns) { + describe(`seed: ${seed}`, () => { + for (const functionName of functionNames) { + it(`${functionName}()`, () => { + faker.seed(seed); - spy_name_firstName.mockRestore(); - }); - - it('returns a gender-specific name when passed a number', () => { - for (let q = 0; q < 30; q++) { - const gender = Math.floor(Math.random() * 2); - const name = faker.name.firstName(gender); - if (gender === 0) { - assertInArray(name, faker.definitions.name.male_first_name); - } else { - assertInArray(name, faker.definitions.name.female_first_name); - } + const actual = faker.name[functionName](); + expect(actual).toEqual(expectations[functionName].noArgs); + }); } }); - - it('returns a gender-specific name when passed a string', () => { - for (let q = 0; q < 30; q++) { - const gender = Math.floor(Math.random() * 2); - const genderString = gender === 0 ? 'male' : 'female'; - const name = faker.name.firstName(genderString); - assertInArray( - name, - faker.definitions.name[genderString + '_first_name'] + } + + // 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('firstName()', () => { + it('should return a random first name', () => { + const first_name = faker.name.firstName(); + + expect(typeof first_name).toBe('string'); + expect(first_name.length).greaterThan(0); + }); + + it('should return a gender-specific first name when passed a number', () => { + let name = faker.name.firstName(0); + expect(faker.definitions.name.male_first_name).toContain(name); + name = faker.name.firstName(1); + expect(faker.definitions.name.female_first_name).toContain(name); + }); + + // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 + it.todo( + 'should return a gender-specific first name when passed a string', + () => { + let name = faker.name.firstName('male'); + expect(faker.definitions.name.male_first_name).toContain(name); + name = faker.name.firstName('female'); + expect(faker.definitions.name.female_first_name).toContain(name); + } ); - } - }); - }); - - describe('lastName()', () => { - it('returns a random name', () => { - const spy_name_lastName = vi - .spyOn(faker.name, 'lastName') - .mockReturnValue('foo'); - - const last_name = faker.name.lastName(); - - expect(last_name).toBe('foo'); - - spy_name_lastName.mockRestore(); - }); - }); - - describe('middleName()', () => { - it('returns a random middle name', () => { - const spy_name_middleName = vi - .spyOn(faker.name, 'middleName') - .mockReturnValue('foo'); - - const middle_name = faker.name.middleName(); - - expect(middle_name).toBe('foo'); - - spy_name_middleName.mockRestore(); - }); - - describe('when using a locale with gender specific middle names', () => { - let oldLocale: string; - - beforeEach(() => { - oldLocale = faker.locale; - faker.locale = 'TEST'; - - // @ts-expect-error - faker.locales.TEST = { - name: { - male_middle_name: ['Genaddiesvich'], - female_middle_name: ['Genaddievna'], - }, - }; }); - afterEach(() => { - faker.locale = oldLocale; - // @ts-expect-error - delete faker.locale.TEST; - }); - - it('returns male prefix', () => { - const middle_name = faker.name.middleName(0); + describe('lastName()', () => { + it('should return a random last name', () => { + const last_name = faker.name.lastName(); + + expect(typeof last_name).toBe('string'); + expect(last_name.length).greaterThan(0); + }); + + // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 + it.todo( + 'should return a gender-specific last name when passed a number', + () => { + let name = faker.name.lastName(0); + expect(faker.definitions.name.male_last_name).toContain(name); + name = faker.name.lastName(1); + expect(faker.definitions.name.female_last_name).toContain(name); + } + ); - expect(middle_name).toBe('Genaddiesvich'); + // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 + it.todo( + 'should return a gender-specific last name when passed a string', + () => { + let name = faker.name.lastName('male'); + expect(faker.definitions.name.male_last_name).toContain(name); + name = faker.name.lastName('female'); + expect(faker.definitions.name.female_last_name).toContain(name); + } + ); }); - it('returns female prefix', () => { - const middle_name = faker.name.middleName(1); - - expect(middle_name).toBe('Genaddievna'); + describe('middleName()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a random middle name', () => { + const middle_name = faker.name.middleName(); + + expect(typeof middle_name).toBe('string'); + expect(middle_name.length).greaterThan(0); + }); + + it('should return a gender-specific middle name when passed a number', () => { + faker.locale = 'uk'; + + let name = faker.name.middleName(0); + expect(faker.definitions.name.male_middle_name).toContain(name); + name = faker.name.middleName(1); + expect(faker.definitions.name.female_middle_name).toContain(name); + }); + + // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 + it.todo( + 'should return a gender-specific middle name when passed a string', + () => { + faker.locale = 'uk'; + + let name = faker.name.middleName('male'); + expect(faker.definitions.name.male_middle_name).toContain(name); + name = faker.name.middleName('female'); + expect(faker.definitions.name.female_middle_name).toContain(name); + } + ); }); - }); - }); - describe('findName()', () => { - it('usually returns a first name and last name', () => { - const spy_datatype_number = vi - .spyOn(faker.datatype, 'number') - .mockReturnValue(5); + describe.skip('findName()', () => { + it('usually returns a first name and last name', () => { + const name = faker.name.findName(); - const name = faker.name.findName(); + expect(name).toBeTruthy(); - expect(name).toBeTruthy(); + const parts = name.split(' '); - const parts = name.split(' '); + expect(parts).toHaveLength(2); + }); - expect(parts).toHaveLength(2); + it('occasionally returns a first name and last name with a prefix', () => { + const name = faker.name.findName(); + const parts = name.split(' '); - spy_datatype_number.mockRestore(); - }); + expect(parts.length).greaterThanOrEqual(3); + }); - it('occasionally returns a first name and last name with a prefix', () => { - const spy_datatype_number = vi - .spyOn(faker.datatype, 'number') - .mockReturnValue(0); + it('occasionally returns a male full name with a prefix', () => { + const name = faker.name.findName(); - const name = faker.name.findName(); - const parts = name.split(' '); + expect(name).toBe('Clifford Champlin'); + }); - expect(parts.length).greaterThanOrEqual(3); + it('occasionally returns a first name and last name with a suffix', () => { + const name = faker.name.findName(); + const parts = name.split(' '); - spy_datatype_number.mockRestore(); - }); + expect(parts.length).greaterThanOrEqual(3); + expect(parts[parts.length - 1]).toBe('PhD'); + }); - it('occasionally returns a male full name with a prefix', () => { - const spy_datatype_number = vi - .spyOn(faker.datatype, 'number') - .mockImplementation((opt) => - opt === 8 - ? 0 // with prefix - : opt === 1 - ? 0 // gender male - : undefined + it.todo( + 'needs to work with specific locales and respect the fallbacks', + () => { + faker.locale = 'en_US'; + // this will throw if this is broken + const name = faker.name.findName(); + // TODO @Shinigami92 2022-01-20: This test doesn't check anything + } ); - - const spy_name_prefix = vi - .spyOn(faker.name, 'prefix') - .mockImplementation((arg) => (arg === 0 ? 'X' : undefined)); - const spy_name_firstName = vi - .spyOn(faker.name, 'firstName') - .mockImplementation((arg) => (arg === 0 ? 'Y' : undefined)); - const spy_name_lastName = vi - .spyOn(faker.name, 'lastName') - .mockImplementation((arg) => (arg === 0 ? 'Z' : undefined)); - - const name = faker.name.findName(); - - expect(name).toBe('X Y Z'); - - spy_datatype_number.mockRestore(); - spy_name_prefix.mockRestore(); - spy_name_firstName.mockRestore(); - spy_name_lastName.mockRestore(); - }); - - it('occasionally returns a female full name with a prefix', () => { - const spy_datatype_number = vi - .spyOn(faker.datatype, 'number') - .mockImplementation((opt) => - opt === 8 - ? 0 // with prefix - : opt === 1 - ? 1 // gender female - : undefined - ); - - const spy_name_prefix = vi - .spyOn(faker.name, 'prefix') - .mockImplementation((arg) => (arg === 1 ? 'J' : undefined)); - const spy_name_firstName = vi - .spyOn(faker.name, 'firstName') - .mockImplementation((arg) => (arg === 1 ? 'K' : undefined)); - const spy_name_lastName = vi - .spyOn(faker.name, 'lastName') - .mockImplementation((arg) => (arg === 1 ? 'L' : undefined)); - - const name = faker.name.findName(); - - expect(name).toBe('J K L'); - - spy_datatype_number.mockRestore(); - spy_name_prefix.mockRestore(); - spy_name_firstName.mockRestore(); - spy_name_lastName.mockRestore(); - }); - - it('occasionally returns a first name and last name with a suffix', () => { - const spy_datatype_number = vi - .spyOn(faker.datatype, 'number') - .mockReturnValue(1); - const spy_name_suffix = vi - .spyOn(faker.name, 'suffix') - .mockReturnValue('Jr.'); - - const name = faker.name.findName(); - const parts = name.split(' '); - - expect(parts.length).greaterThanOrEqual(3); - expect(parts[parts.length - 1]).toBe('Jr.'); - - spy_name_suffix.mockRestore(); - spy_datatype_number.mockRestore(); - }); - - it.todo( - 'needs to work with specific locales and respect the fallbacks', - () => { - faker.locale = 'en_US'; - // this will throw if this is broken - const name = faker.name.findName(); - // TODO @Shinigami92 2022-01-20: This test doesn't check anything - } - ); - }); - - describe('title()', () => { - it('returns a random title', () => { - const spy_name_title = vi - .spyOn(faker.name, 'title') - .mockReturnValue('Lead Solutions Supervisor'); - - const title = faker.name.title(); - - expect(title).toBe('Lead Solutions Supervisor'); - - spy_name_title.mockRestore(); - }); - }); - - describe('jobTitle()', () => { - it('returns a job title consisting of a descriptor, area, and type', () => { - const spy_random_arrayElement = vi.spyOn(faker.random, 'arrayElement'); - const spy_name_jobDescriptor = vi.spyOn(faker.name, 'jobDescriptor'); - const spy_name_jobArea = vi.spyOn(faker.name, 'jobArea'); - const spy_name_jobType = vi.spyOn(faker.name, 'jobType'); - - const jobTitle = faker.name.jobTitle(); - - expect(typeof jobTitle).toBe('string'); - expect(spy_random_arrayElement).toHaveBeenCalledTimes(3); - expect(spy_name_jobDescriptor).toHaveBeenCalledOnce(); - expect(spy_name_jobArea).toHaveBeenCalledOnce(); - expect(spy_name_jobType).toHaveBeenCalledOnce(); - - spy_random_arrayElement.mockRestore(); - spy_name_jobDescriptor.mockRestore(); - spy_name_jobArea.mockRestore(); - spy_name_jobType.mockRestore(); - }); - }); - - describe('prefix()', () => { - describe('when using a locale with gender specific name prefixes', () => { - let oldLocale: string; - - beforeEach(() => { - oldLocale = faker.locale; - faker.locale = 'TEST'; - - faker.locales.TEST = { - title: 'Test', - name: { - male_prefix: ['Mp'], - female_prefix: ['Fp'], - }, - }; }); - afterEach(() => { - faker.locale = oldLocale; - delete faker.locales.TEST; - }); + describe.skip('jobTitle()', () => { + it('should return a job title consisting of a descriptor, area, and type', () => { + const jobTitle = faker.name.jobTitle(); - it('returns male prefix', () => { - const prefix = faker.name.prefix(0); - expect(prefix).toBe('Mp'); + expect(typeof jobTitle).toBe('string'); + }); }); - it('returns female prefix', () => { - const prefix = faker.name.prefix(1); - expect(prefix).toBe('Fp'); + describe.skip('gender()', () => { + // ... }); - it('returns either prefix', () => { - const prefix = faker.name.prefix(); - expect(['Mp', 'Fp']).toContain(prefix); + describe.skip('prefix()', () => { + describe('when using a locale with gender specific name prefixes', () => { + let oldLocale: string; + + beforeEach(() => { + oldLocale = faker.locale; + faker.locale = 'TEST'; + + faker.locales.TEST = { + title: 'Test', + name: { + male_prefix: ['Mp'], + female_prefix: ['Fp'], + }, + }; + }); + + afterEach(() => { + faker.locale = oldLocale; + delete faker.locales.TEST; + }); + + it('should return male prefix', () => { + const prefix = faker.name.prefix(0); + expect(prefix).toBe('Mp'); + }); + + it('should return female prefix', () => { + const prefix = faker.name.prefix(1); + expect(prefix).toBe('Fp'); + }); + + it('should return either prefix', () => { + const prefix = faker.name.prefix(); + expect(['Mp', 'Fp']).toContain(prefix); + }); + }); + + describe('when using a locale without gender specific name prefixes', () => { + let oldLocale: string; + + beforeEach(() => { + oldLocale = faker.locale; + faker.locale = 'TEST'; + + faker.locales.TEST = { + title: 'Test', + name: { + prefix: ['P'], + }, + }; + }); + + afterEach(() => { + faker.locale = oldLocale; + delete faker.locales.TEST; + }); + + it('should return a prefix', () => { + const prefix = faker.name.prefix(); + + expect(prefix).toBe('P'); + }); + }); }); - }); - describe('when using a locale without gender specific name prefixes', () => { - let oldLocale: string; + describe.skip('suffix()', () => { + // ... + }); - beforeEach(() => { - oldLocale = faker.locale; - faker.locale = 'TEST'; + describe.skip('title()', () => { + it('should return a random title', () => { + const title = faker.name.title(); - faker.locales.TEST = { - title: 'Test', - name: { - prefix: ['P'], - }, - }; + expect(typeof title).toBe('string'); + expect(title.length).greaterThan(0); + }); }); - afterEach(() => { - faker.locale = oldLocale; - delete faker.locales.TEST; + describe.skip('jobDescriptor()', () => { + // ... }); - it('returns a prefix', () => { - const prefix = faker.name.prefix(); + describe.skip('jobArea()', () => { + // ... + }); - expect(prefix).toBe('P'); + describe.skip('jobType()', () => { + // ... }); - }); + } }); }); From 0f96706691424561e27af40516c587c01c1a3ec0 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 31 Jan 2022 18:50:38 +0100 Subject: [PATCH 2/7] chore: add tests for findName --- test/name.spec.ts | 124 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 37 deletions(-) diff --git a/test/name.spec.ts b/test/name.spec.ts index 0c541955505..c581e251470 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -168,6 +168,10 @@ describe('name', () => { describe(`random seeded tests for seed ${faker.seedValue}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { describe('firstName()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + it('should return a random first name', () => { const first_name = faker.name.firstName(); @@ -195,6 +199,10 @@ describe('name', () => { }); describe('lastName()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + it('should return a random last name', () => { const last_name = faker.name.lastName(); @@ -202,21 +210,21 @@ describe('name', () => { expect(last_name.length).greaterThan(0); }); - // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 - it.todo( - 'should return a gender-specific last name when passed a number', - () => { - let name = faker.name.lastName(0); - expect(faker.definitions.name.male_last_name).toContain(name); - name = faker.name.lastName(1); - expect(faker.definitions.name.female_last_name).toContain(name); - } - ); + it('should return a gender-specific last name when passed a number', () => { + faker.locale = 'az'; + + let name = faker.name.lastName(0); + expect(faker.definitions.name.male_last_name).toContain(name); + name = faker.name.lastName(1); + expect(faker.definitions.name.female_last_name).toContain(name); + }); // TODO @Shinigami92 2022-01-30: There is a bug: https://github.com/faker-js/faker/issues/373 it.todo( 'should return a gender-specific last name when passed a string', () => { + faker.locale = 'az'; + let name = faker.name.lastName('male'); expect(faker.definitions.name.male_last_name).toContain(name); name = faker.name.lastName('female'); @@ -260,47 +268,89 @@ describe('name', () => { ); }); - describe.skip('findName()', () => { - it('usually returns a first name and last name', () => { - const name = faker.name.findName(); - - expect(name).toBeTruthy(); + describe('findName()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); - const parts = name.split(' '); + it('should return a name with firstName and lastName', () => { + const fullName = faker.name.findName(); - expect(parts).toHaveLength(2); + expect(typeof fullName).toBe('string'); + expect(fullName).toContain(' '); }); - it('occasionally returns a first name and last name with a prefix', () => { - const name = faker.name.findName(); - const parts = name.split(' '); + it('should return a male gender-specific name with firstName and lastName', () => { + faker.locale = 'mk'; + + const male_specific = [ + ...faker.definitions.name.male_prefix, + ...faker.definitions.name.male_first_name, + ...faker.definitions.name.male_last_name, + ...faker.definitions.name.suffix, + ]; + + const fullName = faker.name.findName(undefined, undefined, 0); - expect(parts.length).greaterThanOrEqual(3); + const parts = fullName.split(' '); + for (const part of parts) { + expect(male_specific).toContain(part); + } }); - it('occasionally returns a male full name with a prefix', () => { - const name = faker.name.findName(); + it('should return a female gender-specific name with firstName and lastName', () => { + faker.locale = 'mk'; + + const female_specific = [ + ...faker.definitions.name.female_prefix, + ...faker.definitions.name.female_first_name, + ...faker.definitions.name.female_last_name, + ...faker.definitions.name.suffix, + ]; + + const fullName = faker.name.findName(undefined, undefined, 1); - expect(name).toBe('Clifford Champlin'); + const parts = fullName.split(' '); + for (const part of parts) { + expect(female_specific).toContain(part); + } }); - it('occasionally returns a first name and last name with a suffix', () => { - const name = faker.name.findName(); - const parts = name.split(' '); + it('should return a male gender-specific name with given firstName and lastName', () => { + faker.locale = 'mk'; + + const male_specific = [ + ...faker.definitions.name.male_prefix, + 'firstName', + 'lastName', + ...faker.definitions.name.suffix, + ]; - expect(parts.length).greaterThanOrEqual(3); - expect(parts[parts.length - 1]).toBe('PhD'); + const fullName = faker.name.findName('firstName', 'lastName', 0); + + const parts = fullName.split(' '); + for (const part of parts) { + expect(male_specific).toContain(part); + } }); - it.todo( - 'needs to work with specific locales and respect the fallbacks', - () => { - faker.locale = 'en_US'; - // this will throw if this is broken - const name = faker.name.findName(); - // TODO @Shinigami92 2022-01-20: This test doesn't check anything + it('should return a female gender-specific name with given firstName and lastName', () => { + faker.locale = 'mk'; + + const male_specific = [ + ...faker.definitions.name.female_prefix, + 'firstName', + 'lastName', + ...faker.definitions.name.suffix, + ]; + + const fullName = faker.name.findName('firstName', 'lastName', 1); + + const parts = fullName.split(' '); + for (const part of parts) { + expect(male_specific).toContain(part); } - ); + }); }); describe.skip('jobTitle()', () => { From d03bdf6d65816567cecd7a7fd9ca03ce28b1f9cc Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 31 Jan 2022 20:32:12 +0100 Subject: [PATCH 3/7] chore: add jobTitle test --- test/name.spec.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/name.spec.ts b/test/name.spec.ts index c581e251470..c4e470839f6 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -353,11 +353,17 @@ describe('name', () => { }); }); - describe.skip('jobTitle()', () => { + describe('jobTitle()', () => { it('should return a job title consisting of a descriptor, area, and type', () => { const jobTitle = faker.name.jobTitle(); expect(typeof jobTitle).toBe('string'); + + const [descriptor, level, job] = jobTitle.split(' '); + + expect(faker.definitions.name.title.descriptor).toContain(descriptor); + expect(faker.definitions.name.title.level).toContain(level); + expect(faker.definitions.name.title.job).toContain(job); }); }); From c1ba610ab9c33a9ad4d0d94f6502254c261a4e7c Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 31 Jan 2022 20:37:35 +0100 Subject: [PATCH 4/7] chore: add gender test --- test/name.spec.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/name.spec.ts b/test/name.spec.ts index c4e470839f6..b06e733e35a 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -367,8 +367,20 @@ describe('name', () => { }); }); - describe.skip('gender()', () => { - // ... + describe('gender()', () => { + it('should return a default gender', () => { + const gender = faker.name.gender(); + + expect(typeof gender).toBe('string'); + expect(faker.definitions.name.gender).toContain(gender); + }); + + it('should return a binary gender', () => { + const gender = faker.name.gender(true); + + expect(typeof gender).toBe('string'); + expect(faker.definitions.name.binary_gender).toContain(gender); + }); }); describe.skip('prefix()', () => { From cd37b785f4c5248f5c2d9889925b3c0f5ea8da68 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 31 Jan 2022 21:04:19 +0100 Subject: [PATCH 5/7] chore: add prefix test --- test/name.spec.ts | 105 ++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 56 deletions(-) diff --git a/test/name.spec.ts b/test/name.spec.ts index b06e733e35a..40c02943aea 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -354,6 +354,10 @@ describe('name', () => { }); describe('jobTitle()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + it('should return a job title consisting of a descriptor, area, and type', () => { const jobTitle = faker.name.jobTitle(); @@ -368,6 +372,10 @@ describe('name', () => { }); describe('gender()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + it('should return a default gender', () => { const gender = faker.name.gender(); @@ -383,69 +391,54 @@ describe('name', () => { }); }); - describe.skip('prefix()', () => { - describe('when using a locale with gender specific name prefixes', () => { - let oldLocale: string; - - beforeEach(() => { - oldLocale = faker.locale; - faker.locale = 'TEST'; - - faker.locales.TEST = { - title: 'Test', - name: { - male_prefix: ['Mp'], - female_prefix: ['Fp'], - }, - }; - }); - - afterEach(() => { - faker.locale = oldLocale; - delete faker.locales.TEST; - }); - - it('should return male prefix', () => { - const prefix = faker.name.prefix(0); - expect(prefix).toBe('Mp'); - }); - - it('should return female prefix', () => { - const prefix = faker.name.prefix(1); - expect(prefix).toBe('Fp'); - }); - - it('should return either prefix', () => { - const prefix = faker.name.prefix(); - expect(['Mp', 'Fp']).toContain(prefix); - }); + describe('prefix()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a prefix', () => { + const prefix = faker.name.prefix(); + + expect(typeof prefix).toBe('string'); + expect(faker.definitions.name.prefix).toContain(prefix); }); - describe('when using a locale without gender specific name prefixes', () => { - let oldLocale: string; + it('should return a male prefix', () => { + faker.locale = 'mk'; + + const prefix = faker.name.prefix(0); - beforeEach(() => { - oldLocale = faker.locale; - faker.locale = 'TEST'; + expect(typeof prefix).toBe('string'); + expect(faker.definitions.name.male_prefix).toContain(prefix); + }); - faker.locales.TEST = { - title: 'Test', - name: { - prefix: ['P'], - }, - }; - }); + // TODO @Shinigami92 2022-01-31: There is a bug: https://github.com/faker-js/faker/issues/373 + it.todo('should return a male prefix with given string', () => { + faker.locale = 'mk'; + + const prefix = faker.name.prefix('male'); + + expect(typeof prefix).toBe('string'); + expect(faker.definitions.name.male_prefix).toContain(prefix); + }); - afterEach(() => { - faker.locale = oldLocale; - delete faker.locales.TEST; - }); + it('should return a female prefix', () => { + faker.locale = 'mk'; + + const prefix = faker.name.prefix(1); + + expect(typeof prefix).toBe('string'); + expect(faker.definitions.name.female_prefix).toContain(prefix); + }); + + // TODO @Shinigami92 2022-01-31: There is a bug: https://github.com/faker-js/faker/issues/373 + it.todo('should return a female prefix with given string', () => { + faker.locale = 'mk'; - it('should return a prefix', () => { - const prefix = faker.name.prefix(); + const prefix = faker.name.prefix('female'); - expect(prefix).toBe('P'); - }); + expect(typeof prefix).toBe('string'); + expect(faker.definitions.name.female_prefix).toContain(prefix); }); }); From e4e2c77b8f0aaf183e0c17c2ec3042a3f3c68c3d Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 31 Jan 2022 21:06:38 +0100 Subject: [PATCH 6/7] chore: add suffix test --- test/name.spec.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/test/name.spec.ts b/test/name.spec.ts index 40c02943aea..3ca4286c9cd 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -442,8 +442,17 @@ describe('name', () => { }); }); - describe.skip('suffix()', () => { - // ... + describe('suffix()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a suffix', () => { + const suffix = faker.name.suffix(); + + expect(typeof suffix).toBe('string'); + expect(faker.definitions.name.suffix).toContain(suffix); + }); }); describe.skip('title()', () => { From d5ceea8d458be47adb65da53b52cb4d04ac38e21 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 31 Jan 2022 21:12:37 +0100 Subject: [PATCH 7/7] chore: add job tests --- test/name.spec.ts | 58 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/test/name.spec.ts b/test/name.spec.ts index 3ca4286c9cd..2275dd02667 100644 --- a/test/name.spec.ts +++ b/test/name.spec.ts @@ -455,25 +455,65 @@ describe('name', () => { }); }); - describe.skip('title()', () => { - it('should return a random title', () => { + describe('title()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a title consisting of a descriptor, area, and type', () => { const title = faker.name.title(); expect(typeof title).toBe('string'); - expect(title.length).greaterThan(0); + + const [descriptor, level, job] = title.split(' '); + + // TODO @Shinigami92 2022-01-31: jobTitle and title are the same + expect(faker.definitions.name.title.descriptor).toContain(descriptor); + expect(faker.definitions.name.title.level).toContain(level); + expect(faker.definitions.name.title.job).toContain(job); }); }); - describe.skip('jobDescriptor()', () => { - // ... + describe('jobDescriptor()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a descriptor', () => { + const descriptor = faker.name.jobDescriptor(); + + expect(typeof descriptor).toBe('string'); + + expect(faker.definitions.name.title.descriptor).toContain(descriptor); + }); }); - describe.skip('jobArea()', () => { - // ... + describe('jobArea()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a level', () => { + const level = faker.name.jobArea(); + + expect(typeof level).toBe('string'); + + expect(faker.definitions.name.title.level).toContain(level); + }); }); - describe.skip('jobType()', () => { - // ... + describe('jobType()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a job', () => { + const job = faker.name.jobType(); + + expect(typeof job).toBe('string'); + + expect(faker.definitions.name.title.job).toContain(job); + }); }); } });