From 22fc29836f4690441d75dab38f524ec8421569fd Mon Sep 17 00:00:00 2001 From: Stanley Sathler Date: Mon, 9 Sep 2024 08:30:32 -0300 Subject: [PATCH] fix: support `Object` as model value type (#263) Co-authored-by: Artem Zakharchenko --- src/utils/isModelValueType.ts | 2 ++ test/model/create.test.ts | 31 +++++++++++++++++++++++++++++ test/utils/isModelValueType.test.ts | 4 ++++ 3 files changed, 37 insertions(+) diff --git a/src/utils/isModelValueType.ts b/src/utils/isModelValueType.ts index 705ff035..f7f5e241 100644 --- a/src/utils/isModelValueType.ts +++ b/src/utils/isModelValueType.ts @@ -1,10 +1,12 @@ import { ModelValueType, PrimitiveValueType } from '../glossary' +import { isObject } from './isObject' function isPrimitiveValueType(value: any): value is PrimitiveValueType { return ( typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || + isObject(value) || value?.constructor?.name === 'Date' ) } diff --git a/test/model/create.test.ts b/test/model/create.test.ts index e53b0a91..601dc332 100644 --- a/test/model/create.test.ts +++ b/test/model/create.test.ts @@ -31,6 +31,37 @@ test('creates a new entity with initial values', () => { expect(exactUser.id).toEqual('abc-123') }) +test('creates a new entity with an object property and no value specified', () => { + const db = factory({ + user: { + id: primaryKey(faker.datatype.uuid), + settings: Object, + }, + }) + + const exactUser = db.user.create({ + id: 'abc-123', + }) + expect(exactUser.id).toEqual('abc-123') + expect(exactUser.settings).toEqual({}) +}) + +test('creates a new entity with an object property and a value specified', () => { + const db = factory({ + user: { + id: primaryKey(faker.datatype.uuid), + settings: Object, + }, + }) + + const exactUser = db.user.create({ + id: 'abc-123', + settings: { minHue: 1 }, + }) + expect(exactUser.id).toEqual('abc-123') + expect(exactUser.settings).toEqual({ minHue: 1 }) +}) + test('creates a new entity with an array property', () => { const db = factory({ user: { diff --git a/test/utils/isModelValueType.test.ts b/test/utils/isModelValueType.test.ts index ee647b10..5f14fe8a 100644 --- a/test/utils/isModelValueType.test.ts +++ b/test/utils/isModelValueType.test.ts @@ -36,6 +36,10 @@ it('returns true when given nested primitive arrays', () => { expect(isModelValueType(['I am a string', [100]])).toBe(true) }) +it('returns true when given a literal object', () => { + expect(isModelValueType({ intensity: 1 })).toBe(true) +}) + it('returns false given an undefined', () => { expect(isModelValueType(undefined)).toBe(false) })