Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support Object as model value type #263

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/utils/isModelValueType.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import isObjectLike from 'lodash/isObjectLike'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch - turns out we had a test set as .only too, which is also fixed now 🚀

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) ||
kettanaito marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -30,6 +30,37 @@ test('creates a new entity with initial values', () => {
expect(exactUser.id).toEqual('abc-123')
})

test.only('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