Skip to content
Open
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
49 changes: 49 additions & 0 deletions tests/relations/many-to-many.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,52 @@ it('scopes a nested many-to-many relation update to the targeted record', async

expect(posts.all().map((post) => post.title)).toEqual(['Updated', 'Second'])
})

it('updates a self referencing many to many relation', async () => {
const userSchema = z.object({
id: z.number(),
get children() {
return z.array(userSchema).optional()
},
get parents() {
return z.array(userSchema).optional()
},
})

const users = new Collection({ schema: userSchema })

users.defineRelations(({ many }) => ({
children: many(users, { role: 'children' }),
parents: many(users, { role: 'children' }),
}))

const child = await users.create({
id: 1,
})

const parent = await users.create({
id: 2,
children: [child],
})

expect.soft(parent.children).toEqual([expect.objectContaining({ id: 1 })])
expect.soft(child.children).toEqual([])

expect.soft(child.parents).toEqual(expect.objectContaining({ id: 2 }))
expect.soft(parent.parents).toEqual([])

const parent2 = await users.create({
id: 3,
})

const child2 = await users.create({
id: 4,
parents: [child],
})

expect.soft(parent2.children).toEqual([expect.objectContaining({ id: 1 })])
expect.soft(child2.children).toEqual([])

expect.soft(child2.parents).toEqual(expect.objectContaining({ id: 2 }))
expect.soft(parent2.parents).toEqual([])
})
34 changes: 34 additions & 0 deletions tests/relations/many-to-one.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,37 @@ it("scopes nested updates to the updated owner's foreign record", async () => {

expect(countries.all()).toEqual([{ code: 'uk' }, { code: 'ca' }])
})

it('updates a self referencing many to one relation', async () => {
const userSchema = z.object({
id: z.number(),
get children() {
return z.array(userSchema).optional()
},
get parent() {
return userSchema.optional()
},
})

const users = new Collection({ schema: userSchema })

users.defineRelations(({ one, many }) => ({
children: many(users, { role: 'children' }),
parent: one(users, { role: 'children' }),
}))

const parent = await users.create({
id: 2,
})

const child = await users.create({
id: 1,
parent,
})

expect.soft(parent.children).toEqual([expect.objectContaining({ id: 1 })])
expect.soft(child.children).toEqual([])

expect.soft(child.parent).toEqual(expect.objectContaining({ id: 2 }))
expect.soft(parent.parent).toBeUndefined()
})
33 changes: 33 additions & 0 deletions tests/relations/one-to-many.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,36 @@ it('scopes a nested one-to-many relation update to the targeted record', async (

expect(posts.all().map((post) => post.title)).toEqual(['Updated', 'Second'])
})

it('updates a self referencing one to many relation', async () => {
const userSchema = z.object({
id: z.number(),
get children() {
return z.array(userSchema).optional()
},
get parent() {
return userSchema.optional()
},
})

const users = new Collection({ schema: userSchema })

users.defineRelations(({ one, many }) => ({
children: many(users, { role: 'children' }),
parent: one(users, { role: 'children' }),
}))

const child = await users.create({
id: 1,
})
const parent = await users.create({
id: 2,
children: [child],
})

expect.soft(parent.children).toEqual([expect.objectContaining({ id: 1 })])
expect.soft(child.children).toEqual([])

expect.soft(child.parent).toEqual(expect.objectContaining({ id: 2 }))
expect.soft(parent.parent).toBeUndefined()
})
34 changes: 34 additions & 0 deletions tests/relations/one-to-one.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,37 @@ it('removes relation listeners when the owner record is deleted', async () => {
'Detaches relation listeners when the owner record is deleted',
).toBe(baseline)
})

it('updates a self referencing one to one relation', async () => {
const userSchema = z.object({
id: z.number(),
get child() {
return userSchema.optional()
},
get parent() {
return userSchema.optional()
},
})

const users = new Collection({ schema: userSchema })

users.defineRelations(({ one }) => ({
child: one(users, { role: 'children' }),
parent: one(users, { role: 'children' }),
}))

const parent = await users.create({
id: 2,
})

const child = await users.create({
id: 1,
parent,
})

expect.soft(parent.child).toEqual(expect.objectContaining({ id: 1 }))
expect.soft(child.child).toBeUndefined()

expect.soft(child.parent).toEqual(expect.objectContaining({ id: 2 }))
expect.soft(parent.parent).toBeUndefined()
})