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: an entity with relations can be created also without them #79

Merged
merged 3 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ Creates an entity for the model.
const user = db.user.create()
```

When called without arguments, `.create()` will populate the entity properties using the getter functions you've specified in the model definition.

You can also provide a partial initial values when creating an entity:

```js
const user = db.user.create({
firstName: 'John',
})
```

> Note that all model properties _are optional_, including [relational properties](#model-relationships).

#### `findFirst`

Returns the first entity that satisfies the given query.
Expand Down
3 changes: 3 additions & 0 deletions src/model/defineRelationalProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export function defineRelationalProperties(
`defining relational property "${entity.__type}.${property}"`,
relation,
)

if (!(property in initialValues)) return properties
kettanaito marked this conversation as resolved.
Show resolved Hide resolved

// Take the relational entity reference from the initial values.
const entityRefs: Entity<any, any>[] = [].concat(initialValues[property])

Expand Down
16 changes: 16 additions & 0 deletions test/relations/many-to-one.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,19 @@ test('supports querying through nested relational properties', () => {

expect(result).toHaveLength(3)
})

test('should not throw error if an entity with many-to-one relation is created without it', () => {
const db = factory({
user: {
id: primaryKey(random.uuid),
firstName: name.firstName,
},
post: {
id: primaryKey(random.uuid),
title: random.words,
author: oneOf('user'),
},
})

expect(() => db.post.create()).not.toThrow()
})
15 changes: 15 additions & 0 deletions test/relations/one-to-many.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@ test('supports querying through one-to-many relation', () => {
const userIds = users.map((user) => user.id)
expect(userIds).toEqual(['user-1', 'user-3'])
})

test('should not throw error if an entity with one-to-many relation is created without it', () => {
const db = factory({
user: {
id: primaryKey(random.uuid),
posts: manyOf('post'),
},
post: {
id: primaryKey(random.uuid),
title: random.words,
},
})

expect(() => db.user.create()).not.toThrow()
})
18 changes: 17 additions & 1 deletion test/relations/one-to-one.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { random } from 'faker'
import { factory, primaryKey, oneOf } from '@mswjs/data'

test.only('supports one-to-one relation', () => {
test('supports one-to-one relation', () => {
const db = factory({
country: {
id: primaryKey(random.uuid),
Expand Down Expand Up @@ -57,3 +57,19 @@ test('supports querying through a one-to-one relational property', () => {
})
expect(capital).toHaveProperty('name', 'Washington')
})

test('should not throw error if an entity with one-to-one relation is created without it', () => {
const db = factory({
country: {
id: primaryKey(random.uuid),
name: random.words,
},
capital: {
id: primaryKey(random.uuid),
name: random.word,
country: oneOf('country'),
},
})

expect(() => db.capital.create()).not.toThrow()
})