Skip to content

Commit

Permalink
feat: allow passing Object construtor to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
StanleySathler committed Sep 10, 2023
1 parent a39b026 commit c0ce51c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/utils/isModelValueType.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import isObjectLike from 'lodash/isObjectLike'
import { ModelValueType, PrimitiveValueType } from '../glossary'

function isPrimitiveValueType(value: any): value is PrimitiveValueType {
return (
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean' ||
isObjectLike(value) ||
value?.constructor?.name === 'Date'
)
}

export function isModelValueType(value: any): value is ModelValueType {
return isPrimitiveValueType(value) || Array.isArray(value)
return isPrimitiveValueType(value)
}
16 changes: 16 additions & 0 deletions test/model/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ test('creates a new entity with initial values', () => {
expect(exactUser.id).toEqual('abc-123')
})

test('creates a new entity with an object property', () => {
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: {
Expand Down
4 changes: 4 additions & 0 deletions test/utils/isModelValueType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit c0ce51c

Please sign in to comment.