From d1b00cf86fc04da9d2601af7a1dca260639786ba Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 1 Feb 2022 13:47:29 +0100 Subject: [PATCH] test: rewrite hacker tests --- test/hacker.spec.ts | 197 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 test/hacker.spec.ts diff --git a/test/hacker.spec.ts b/test/hacker.spec.ts new file mode 100644 index 00000000000..d666b290c5f --- /dev/null +++ b/test/hacker.spec.ts @@ -0,0 +1,197 @@ +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { faker } from '../dist/cjs'; + +const seededRuns = [ + { + seed: 42, + expectations: { + abbreviation: { + noArgs: 'PCI', + }, + adjective: { + noArgs: 'cross-platform', + }, + noun: { + noArgs: 'array', + }, + verb: { + noArgs: 'navigate', + }, + ingverb: { + noArgs: 'copying', + }, + phrase: { + noArgs: + 'Try to transmit the SAS microchip, maybe it will quantify the mobile monitor!', + }, + }, + }, + { + seed: 1337, + expectations: { + abbreviation: { + noArgs: 'AGP', + }, + adjective: { + noArgs: 'open-source', + }, + noun: { + noArgs: 'port', + }, + verb: { + noArgs: 'compress', + }, + ingverb: { + noArgs: 'compressing', + }, + phrase: { + noArgs: + 'Try to generate the COM program, maybe it will connect the back-end port!', + }, + }, + }, + { + seed: 1211, + expectations: { + abbreviation: { + noArgs: 'JSON', + }, + adjective: { + noArgs: 'solid state', + }, + noun: { + noArgs: 'capacitor', + }, + verb: { + noArgs: 'reboot', + }, + ingverb: { + noArgs: 'programming', + }, + phrase: { + noArgs: + "I'll back up the neural RSS program, that should panel the SCSI matrix!", + }, + }, + }, +]; + +const NON_SEEDED_BASED_RUN = 5; + +const functionNames = [ + 'abbreviation', + 'adjective', + 'noun', + 'verb', + 'ingverb', + 'phrase', +]; + +describe('name', () => { + afterEach(() => { + faker.locale = 'en'; + }); + + for (const { seed, expectations } of seededRuns) { + describe(`seed: ${seed}`, () => { + for (const functionName of functionNames) { + it(`${functionName}()`, () => { + faker.seed(seed); + + const actual = faker.hacker[functionName](); + expect(actual).toEqual(expectations[functionName].noArgs); + }); + } + }); + } + + // 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('abbreviation()', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a random abbreviation from array', () => { + const abbreviation = faker.hacker.abbreviation(); + + expect(typeof abbreviation).toBe('string'); + expect(abbreviation.length).greaterThan(0); + expect(faker.definitions.hacker.abbreviation).toContain(abbreviation); + }); + }); + + describe('adjective', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a random adjective from array', () => { + const adjective = faker.hacker.adjective(); + + expect(typeof adjective).toBe('string'); + expect(adjective.length).greaterThan(0); + expect(faker.definitions.hacker.adjective).toContain(adjective); + }); + }); + + describe('noun', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a random noun from array', () => { + const noun = faker.hacker.noun(); + + expect(typeof noun).toBe('string'); + expect(noun.length).greaterThan(0); + expect(faker.definitions.hacker.noun).toContain(noun); + }); + }); + + describe('verb', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a random verb from array', () => { + const verb = faker.hacker.verb(); + + expect(typeof verb).toBe('string'); + expect(verb.length).greaterThan(0); + expect(faker.definitions.hacker.verb).toContain(verb); + }); + }); + + describe('ingverb', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a random ingverb from array', () => { + const ingverb = faker.hacker.ingverb(); + + expect(typeof ingverb).toBe('string'); + expect(ingverb.length).greaterThan(0); + expect(faker.definitions.hacker.ingverb).toContain(ingverb); + }); + }); + + describe('phrase', () => { + beforeEach(() => { + faker.locale = 'en'; + }); + + it('should return a random phrase from array', () => { + const phrase = faker.hacker.phrase(); + + expect(typeof phrase).toBe('string'); + expect(phrase.length).greaterThan(0); + }); + }); + } + }); +});