diff --git a/src/random.ts b/src/random.ts index f3d11f3a07a..f3c4ffd196c 100644 --- a/src/random.ts +++ b/src/random.ts @@ -58,7 +58,7 @@ export class Random { number( options?: number | { min?: number; max?: number; precision?: number } ): number { - console.log( + console.warn( 'Deprecation Warning: faker.random.number is now located in faker.datatype.number' ); return this.faker.datatype.number(options); @@ -86,7 +86,7 @@ export class Random { float( options?: number | { min?: number; max?: number; precision?: number } ): number { - console.log( + console.warn( 'Deprecation Warning: faker.random.float is now located in faker.datatype.float' ); return this.faker.datatype.float(options); @@ -186,7 +186,7 @@ export class Random { * @see faker.datatype.uuid() */ uuid(): string { - console.log( + console.warn( 'Deprecation Warning: faker.random.uuid is now located in faker.datatype.uuid' ); return this.faker.datatype.uuid(); @@ -202,7 +202,7 @@ export class Random { * @see faker.datatype.boolean() */ boolean(): boolean { - console.log( + console.warn( 'Deprecation Warning: faker.random.boolean is now located in faker.datatype.boolean' ); return this.faker.datatype.boolean(); @@ -291,7 +291,7 @@ export class Random { * @deprecated */ image(): string { - console.log( + console.warn( 'Deprecation Warning: faker.random.image is now located in faker.image.image' ); return this.faker.image.image(); @@ -316,10 +316,11 @@ export class Random { * @method faker.random.alpha * @param options // defaults to { count: 1, upcase: false, bannedChars: [] } */ + // TODO @Shinigami92 2022-02-14: Tests covered `(count, options)`, but they were never typed like that alpha( options?: | number - | { count: number; upcase?: boolean; bannedChars?: string[] } + | { count?: number; upcase?: boolean; bannedChars?: string[] } ): string { if (typeof options === 'undefined') { options = { @@ -455,7 +456,7 @@ export class Random { * @deprecated */ hexaDecimal(count?: number): string { - console.log( + console.warn( 'Deprecation Warning: faker.random.hexaDecimal is now located in faker.datatype.hexaDecimal' ); return this.faker.datatype.hexaDecimal(count); diff --git a/test/random.spec.ts b/test/random.spec.ts index d09bded0d1b..09a003f8dd6 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -1,80 +1,25 @@ import { describe, expect, it, vi } from 'vitest'; import { faker } from '../src'; -import { Mersenne } from '../src/mersenne'; - -const mersenne = new Mersenne(); - -describe('random.js', () => { - describe('number', () => { - it('random.number() uses datatype module and prints deprecation warning', () => { - const spy_console_log = vi.spyOn(console, 'log'); - const spy_datatype_number = vi.spyOn(faker.datatype, 'number'); - faker.random.number(); - expect(spy_datatype_number).toHaveBeenCalled(); - expect(spy_console_log).toHaveBeenCalledWith( - 'Deprecation Warning: faker.random.number is now located in faker.datatype.number' - ); - spy_datatype_number.mockRestore(); - spy_console_log.mockRestore(); - }); - - it('should return deterministic results when seeded with integer', () => { - faker.seed(100); - const name = faker.name.findName(); - expect(name).toBe('Eva Jenkins'); - }); - - it('should return deterministic results when seeded with 0', () => { - faker.seed(0); - const name = faker.name.findName(); - expect(name).toBe('Lola Sporer'); - }); - - it('should return deterministic results when seeded with array - one element', () => { - faker.seed([10]); - const name = faker.name.findName(); - expect(name).toBe('Duane Kshlerin'); - }); - - it('should return deterministic results when seeded with array - multiple elements', () => { - faker.seed([10, 100, 1000]); - const name = faker.name.findName(); - expect(name).toBe('Alma Shanahan'); - }); - }); - - describe('float', () => { - it('random.float() uses datatype module and prints deprecation warning', () => { - const spy_console_log = vi.spyOn(console, 'log'); - const spy_datatype_float = vi.spyOn(faker.datatype, 'float'); - faker.random.float(); - expect(spy_datatype_float).toHaveBeenCalled(); - expect(spy_console_log).toHaveBeenCalledWith( - 'Deprecation Warning: faker.random.float is now located in faker.datatype.float' - ); - spy_datatype_float.mockRestore(); - spy_console_log.mockRestore(); - }); - }); +describe('random', () => { describe('arrayElement', () => { - it('returns a random element in the array', () => { + it('should return a random element in the array', () => { const testArray = ['hello', 'to', 'you', 'my', 'friend']; - expect( - testArray.indexOf(faker.random.arrayElement(testArray)) - ).greaterThan(-1); + const actual = faker.random.arrayElement(testArray); + + expect(testArray).toContain(actual); }); - it('returns a random element in the array when there is only 1', () => { + it('should return a random element in the array when there is only 1', () => { const testArray = ['hello']; - expect( - testArray.indexOf(faker.random.arrayElement(testArray)) - ).greaterThan(-1); + const actual = faker.random.arrayElement(testArray); + + expect(actual).toBe('hello'); }); }); describe('arrayElements', () => { - it('returns a subset with random elements in the array', () => { + it('should return a subset with random elements in the array', () => { const testArray = ['hello', 'to', 'you', 'my', 'friend']; const subset = faker.random.arrayElements(testArray); @@ -88,15 +33,10 @@ describe('random.js', () => { }); // Check uniqueness - subset.forEach((element) => { - expect(!Object.prototype.hasOwnProperty.call(subset, element)).toBe( - true - ); - subset[element] = true; - }, {}); + expect(subset).toHaveLength(new Set(subset).size); }); - it('returns a subset of fixed length with random elements in the array', () => { + it('should return a subset of fixed length with random elements in the array', () => { const testArray = ['hello', 'to', 'you', 'my', 'friend']; const subset = faker.random.arrayElements(testArray, 3); @@ -109,147 +49,173 @@ describe('random.js', () => { }); // Check uniqueness - subset.forEach((element) => { - expect(!Object.prototype.hasOwnProperty.call(subset, element)).toBe( - true - ); - subset[element] = true; - }, {}); + expect(subset).toHaveLength(new Set(subset).size); }); }); - describe('UUID', () => { - it('random.uuid() uses datatype module and prints deprecation warning', () => { - const spy_console_log = vi.spyOn(console, 'log'); - const spy_datatype_uuid = vi.spyOn(faker.datatype, 'uuid'); - faker.random.uuid(); - expect(spy_datatype_uuid).toHaveBeenCalled(); - expect(spy_console_log).toHaveBeenCalledWith( - 'Deprecation Warning: faker.random.uuid is now located in faker.datatype.uuid' - ); - spy_datatype_uuid.mockRestore(); - spy_console_log.mockRestore(); + describe('objectElement', () => { + it('should return a random value', () => { + const testObject = { + hello: 'to', + you: 'my', + friend: '!', + }; + const actual = faker.random.objectElement(testObject); + + expect(Object.values(testObject)).toContain(actual); + }); + + it('should return a random key', () => { + const testObject = { + hello: 'to', + you: 'my', + friend: '!', + }; + const actual = faker.random.objectElement(testObject, 'key'); + + expect(Object.keys(testObject)).toContain(actual); }); }); - describe('boolean', () => { - it('random.boolean() uses datatype module and prints deprecation warning', () => { - const spy_console_log = vi.spyOn(console, 'log'); - const spy_datatype_boolean = vi.spyOn(faker.datatype, 'boolean'); - faker.random.boolean(); - expect(spy_datatype_boolean).toHaveBeenCalled(); - expect(spy_console_log).toHaveBeenCalledWith( - 'Deprecation Warning: faker.random.boolean is now located in faker.datatype.boolean' - ); - spy_datatype_boolean.mockRestore(); - spy_console_log.mockRestore(); + describe('word', () => { + it('should return a random word', () => { + const actual = faker.random.word(); + + expect(actual).toBeTruthy(); + expect(typeof actual).toBe('string'); }); }); - describe('semver', () => { - const semver = faker.system.semver(); + describe('words', () => { + it('should return random words', () => { + const actual = faker.random.words(); - it('should generate a string', () => { - expect(typeof semver).toBe('string'); + expect(actual).toBeTruthy(); + expect(typeof actual).toBe('string'); + + const words = actual.split(' '); + expect(words.length).greaterThanOrEqual(1); + expect(words.length).lessThanOrEqual(3); }); - it('should generate a valid semver', () => { - expect(semver).match(/^\d+\.\d+\.\d+$/); + it('should return random words', () => { + const actual = faker.random.words(5); + + expect(actual).toBeTruthy(); + expect(typeof actual).toBe('string'); + + const words = actual.split(' '); + expect(words).toHaveLength(5); }); }); - describe('alpha', () => { - const alpha = faker.random.alpha; + describe('locale', () => { + it('should return a random locale', () => { + const actual = faker.random.locale(); + + expect(actual).toBeTruthy(); + expect(typeof actual).toBe('string'); + expect(Object.keys(faker.locales)).toContain(actual); + }); + }); + describe('alpha', () => { it('should return single letter when no count provided', () => { - expect(alpha()).toHaveLength(1); + const actual = faker.random.alpha(); + + expect(actual).toHaveLength(1); }); it('should return lowercase letter when no upcase option provided', () => { - expect(alpha()).match(/[a-z]/); + const actual = faker.random.alpha(); + + expect(actual).match(/[a-z]/); }); it('should return uppercase when upcase option is true', () => { - expect( - alpha( - // @ts-expect-error - { upcase: true } - ) - ).match(/[A-Z]/); + const actual = faker.random.alpha({ upcase: true }); + expect(actual).match(/[A-Z]/); }); it('should generate many random letters', () => { - expect(alpha(5)).toHaveLength(5); + const actual = faker.random.alpha(5); + + expect(actual).toHaveLength(5); }); it('should be able to ban some characters', () => { - const alphaText = alpha( - 5, - // @ts-expect-error - { bannedChars: ['a', 'p'] } - ); - expect(alphaText).toHaveLength(5); - expect(alphaText).match(/[b-oq-z]/); + const actual = faker.random.alpha({ + count: 5, + bannedChars: ['a', 'p'], + }); + + expect(actual).toHaveLength(5); + expect(actual).match(/[b-oq-z]/); }); + it('should be able handle mistake in banned characters array', () => { - const alphaText = alpha( - 5, - // @ts-expect-error - { bannedChars: ['a', 'a', 'p'] } - ); + const alphaText = faker.random.alpha({ + count: 5, + bannedChars: ['a', 'a', 'p'], + }); + expect(alphaText).toHaveLength(5); expect(alphaText).match(/[b-oq-z]/); }); }); describe('alphaNumeric', () => { - const alphaNumeric = faker.random.alphaNumeric; - it('should generate single character when no additional argument was provided', () => { - expect(alphaNumeric()).toHaveLength(1); + const actual = faker.random.alphaNumeric(); + + expect(actual).toHaveLength(1); }); it('should generate many random characters', () => { - expect(alphaNumeric(5)).toHaveLength(5); + const actual = faker.random.alphaNumeric(5); + + expect(actual).toHaveLength(5); }); it('should be able to ban some characters', () => { - const alphaText = alphaNumeric(5, { bannedChars: ['a', 'p'] }); + const alphaText = faker.random.alphaNumeric(5, { + bannedChars: ['a', 'p'], + }); + expect(alphaText).toHaveLength(5); expect(alphaText).match(/[b-oq-z]/); }); + it('should be able handle mistake in banned characters array', () => { - const alphaText = alphaNumeric(5, { bannedChars: ['a', 'p', 'a'] }); + const alphaText = faker.random.alphaNumeric(5, { + bannedChars: ['a', 'p', 'a'], + }); + expect(alphaText).toHaveLength(5); expect(alphaText).match(/[b-oq-z]/); }); }); - describe('hexaDecimal', () => { - it('random.hexaDecimal() uses datatype module and prints deprecation warning', () => { - const spy_console_log = vi.spyOn(console, 'log'); - const spy_datatype_hexaDecimal = vi.spyOn(faker.datatype, 'hexaDecimal'); - faker.random.hexaDecimal(); - expect(spy_datatype_hexaDecimal).toHaveBeenCalled(); - expect(spy_console_log).toHaveBeenCalledWith( - 'Deprecation Warning: faker.random.hexaDecimal is now located in faker.datatype.hexaDecimal' - ); - spy_datatype_hexaDecimal.mockRestore(); - spy_console_log.mockRestore(); - }); - }); - - describe('image', () => { - it('random.image() uses image module and prints deprecation warning', () => { - const spy_console_log = vi.spyOn(console, 'log'); - const spy_image_image = vi.spyOn(faker.image, 'image'); - faker.random.image(); - expect(spy_image_image).toHaveBeenCalled(); - expect(spy_console_log).toHaveBeenCalledWith( - 'Deprecation Warning: faker.random.image is now located in faker.image.image' - ); - spy_image_image.mockRestore(); - spy_console_log.mockRestore(); - }); + describe('deprecation warnings', () => { + it.each([ + ['number', 'datatype.number'], + ['float', 'datatype.float'], + ['uuid', 'datatype.uuid'], + ['boolean', 'datatype.boolean'], + ['image', 'image.image'], + ['hexaDecimal', 'datatype.hexaDecimal'], + ])( + 'should warn user that function random.%s is deprecated', + (functionName, newLocation) => { + const spy = vi.spyOn(console, 'warn'); + + faker.random[functionName](); + + expect(spy).toHaveBeenCalledWith( + `Deprecation Warning: faker.random.${functionName} is now located in faker.${newLocation}` + ); + spy.mockRestore(); + } + ); }); });