From 95b3e4d18d9b3bf0fdd3e1e44ec5347337a27869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leyla=20J=C3=A4hnig?= <77127505+xDivisionByZerox@users.noreply.github.com> Date: Sat, 19 Feb 2022 23:18:29 +0100 Subject: [PATCH] fix: test random.alphaNumeric (#517) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Leyla Jähnig --- test/random.spec.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/test/random.spec.ts b/test/random.spec.ts index 09a003f8dd6..e969817f26c 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -177,16 +177,31 @@ describe('random', () => { expect(actual).toHaveLength(5); }); - it('should be able to ban some characters', () => { + it('should be able to ban all alphabetic characters', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); const alphaText = faker.random.alphaNumeric(5, { - bannedChars: ['a', 'p'], + bannedChars, }); expect(alphaText).toHaveLength(5); - expect(alphaText).match(/[b-oq-z]/); + for (const bannedChar of bannedChars) { + expect(alphaText).not.includes(bannedChar); + } }); - it('should be able handle mistake in banned characters array', () => { + it('should be able to ban all numeric characters', () => { + const bannedChars = '0123456789'.split(''); + const alphaText = faker.random.alphaNumeric(5, { + bannedChars, + }); + + expect(alphaText).toHaveLength(5); + for (const bannedChar of bannedChars) { + expect(alphaText).not.includes(bannedChar); + } + }); + + it('should be able to handle mistake in banned characters array', () => { const alphaText = faker.random.alphaNumeric(5, { bannedChars: ['a', 'p', 'a'], }); @@ -194,6 +209,15 @@ describe('random', () => { expect(alphaText).toHaveLength(5); expect(alphaText).match(/[b-oq-z]/); }); + + it.todo('should throw if all possible characters being banned', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); + expect(() => + faker.random.alphaNumeric(5, { + bannedChars, + }) + ).toThrowError(); + }); }); describe('deprecation warnings', () => {