From 2d0aecc54993e1c78a37c290a19421b5c26b4b74 Mon Sep 17 00:00:00 2001 From: Ryan Smee Date: Fri, 6 May 2022 13:26:16 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=94=A5=20Add=20tests=20for=20uniq?= =?UTF-8?q?ue=20validators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (Still need to do tests for objectIsUnique) ✅ Closes: #220 --- .../src/tests/core/date-is-unique.spec.ts | 32 +++++++++++++ .../core/object-with-id-is-unique.spec.ts | 46 +++++++++++++++++++ .../core/primitive-value-is-unique.spec.ts | 32 +++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 packages/falso/src/tests/core/date-is-unique.spec.ts create mode 100644 packages/falso/src/tests/core/object-with-id-is-unique.spec.ts create mode 100644 packages/falso/src/tests/core/primitive-value-is-unique.spec.ts diff --git a/packages/falso/src/tests/core/date-is-unique.spec.ts b/packages/falso/src/tests/core/date-is-unique.spec.ts new file mode 100644 index 000000000..030c30f53 --- /dev/null +++ b/packages/falso/src/tests/core/date-is-unique.spec.ts @@ -0,0 +1,32 @@ +import { dateIsUnique } from '../../lib/core/unique-validators'; +import { randPastDate } from '@ngneat/falso'; + +describe('dateIsUnique', () => { + let dates: Date[]; + let date1: Date; + let date2: Date; + let date3: Date; + + beforeEach(() => { + date1 = randPastDate(); + date2 = randPastDate(); + date3 = randPastDate(); + dates = [date1, date2]; + }); + + describe('date is unique', () => { + it('should return true', () => { + const result = dateIsUnique(date3, dates); + + expect(result).toEqual(true); + }); + }); + + describe('date is not unique', () => { + it('should return false', () => { + const result = dateIsUnique(date2, dates); + + expect(result).toEqual(false); + }); + }); +}); diff --git a/packages/falso/src/tests/core/object-with-id-is-unique.spec.ts b/packages/falso/src/tests/core/object-with-id-is-unique.spec.ts new file mode 100644 index 000000000..3f23c4652 --- /dev/null +++ b/packages/falso/src/tests/core/object-with-id-is-unique.spec.ts @@ -0,0 +1,46 @@ +import { objectWithIdIsUnique } from '../../lib/core/unique-validators'; +import { randFullName, randNumber } from '@ngneat/falso'; + +describe('objectWithIdIsUnique', () => { + let objects: any[]; + let object1: any; + let object2: any; + let object3: any; + + beforeEach(() => { + const name = randFullName(); + const age = randNumber({ min: 10, max: 90 }); + object1 = { + id: '1', + name, + age, + }; + object2 = { + id: '2', + name, + age, + }; + object3 = { + id: '3', + name, + age, + }; + objects = [object1, object2]; + }); + + describe('object id is unique', () => { + it('should return true', () => { + const result = objectWithIdIsUnique(object3, objects); + + expect(result).toEqual(true); + }); + }); + + describe('object id is not unique', () => { + it('should return false', () => { + const result = objectWithIdIsUnique(object2, objects); + + expect(result).toEqual(false); + }); + }); +}); diff --git a/packages/falso/src/tests/core/primitive-value-is-unique.spec.ts b/packages/falso/src/tests/core/primitive-value-is-unique.spec.ts new file mode 100644 index 000000000..abfbd08d2 --- /dev/null +++ b/packages/falso/src/tests/core/primitive-value-is-unique.spec.ts @@ -0,0 +1,32 @@ +import { primitiveValueIsUnique } from '../../lib/core/unique-validators'; +import { randNumber } from '@ngneat/falso'; + +describe('primitiveValueIsUnique', () => { + let items: number[]; + let item1: number; + let item2: number; + let item3: number; + + beforeEach(() => { + item1 = randNumber(); + item2 = randNumber(); + item3 = randNumber(); + items = [item1, item2]; + }); + + describe('item is unique', () => { + it('should return true', () => { + const result = primitiveValueIsUnique(item3, items); + + expect(result).toEqual(true); + }); + }); + + describe('item is not unique', () => { + it('should return false', () => { + const result = primitiveValueIsUnique(item2, items); + + expect(result).toEqual(false); + }); + }); +});