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

feat: add test for null nested BelongsTo relationship #1009

Open
wants to merge 4 commits into
base: v18
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6083,6 +6083,67 @@ test.group('Base Model | toObject', (group) => {
$extras: {},
})
})

test('add preloaded belongsTo relationship to toObject result', async ({ assert }) => {
class Category extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public name: string

@column()
public parentId: number | null

@belongsTo(() => Category)
public parent: BelongsTo<typeof Category>
}

class Post extends BaseModel {
@column()
public title: string

@hasOne(() => Category)
public category: HasOne<typeof Category>
}

const category = new Category()
category.name = 'Tutorials'
category.id = 1
category.parentId = null

const subCategory = new Category()
subCategory.name = 'Lucid'
subCategory.id = 2
category.parentId = category.id

const post = new Post()
post.title = 'Adonis 101'

subCategory.$setRelated('parent', category)
post.$setRelated('category', subCategory)

assert.deepEqual(subCategory.toObject(), {
name: 'Lucid',
id: 2,
parentId: 1,
parent: {
name: 'Tutorials',
id: 1,
parentId: null,
$extras: {},
},
$extras: {},
})

assert.deepEqual(category.toObject(), {
name: 'Tutorials',
id: 1,
parentId: null,
parent: null,
$extras: {},
})
})
})

test.group('Base model | inheritance', (group) => {
Expand Down