Skip to content

Commit 22fc298

Browse files
fix: support Object as model value type (#263)
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
1 parent 313d921 commit 22fc298

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/utils/isModelValueType.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { ModelValueType, PrimitiveValueType } from '../glossary'
2+
import { isObject } from './isObject'
23

34
function isPrimitiveValueType(value: any): value is PrimitiveValueType {
45
return (
56
typeof value === 'string' ||
67
typeof value === 'number' ||
78
typeof value === 'boolean' ||
9+
isObject(value) ||
810
value?.constructor?.name === 'Date'
911
)
1012
}

test/model/create.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ test('creates a new entity with initial values', () => {
3131
expect(exactUser.id).toEqual('abc-123')
3232
})
3333

34+
test('creates a new entity with an object property and no value specified', () => {
35+
const db = factory({
36+
user: {
37+
id: primaryKey(faker.datatype.uuid),
38+
settings: Object,
39+
},
40+
})
41+
42+
const exactUser = db.user.create({
43+
id: 'abc-123',
44+
})
45+
expect(exactUser.id).toEqual('abc-123')
46+
expect(exactUser.settings).toEqual({})
47+
})
48+
49+
test('creates a new entity with an object property and a value specified', () => {
50+
const db = factory({
51+
user: {
52+
id: primaryKey(faker.datatype.uuid),
53+
settings: Object,
54+
},
55+
})
56+
57+
const exactUser = db.user.create({
58+
id: 'abc-123',
59+
settings: { minHue: 1 },
60+
})
61+
expect(exactUser.id).toEqual('abc-123')
62+
expect(exactUser.settings).toEqual({ minHue: 1 })
63+
})
64+
3465
test('creates a new entity with an array property', () => {
3566
const db = factory({
3667
user: {

test/utils/isModelValueType.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ it('returns true when given nested primitive arrays', () => {
3636
expect(isModelValueType(['I am a string', [100]])).toBe(true)
3737
})
3838

39+
it('returns true when given a literal object', () => {
40+
expect(isModelValueType({ intensity: 1 })).toBe(true)
41+
})
42+
3943
it('returns false given an undefined', () => {
4044
expect(isModelValueType(undefined)).toBe(false)
4145
})

0 commit comments

Comments
 (0)