Skip to content

Commit

Permalink
fix: support Object as model value type (#263)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Zakharchenko <[email protected]>
  • Loading branch information
StanleySathler and kettanaito committed Sep 9, 2024
1 parent 313d921 commit 22fc298
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils/isModelValueType.ts
Original file line number Diff line number Diff line change
@@ -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'
)
}
Expand Down
31 changes: 31 additions & 0 deletions test/model/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
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 22fc298

Please sign in to comment.