Skip to content

Commit

Permalink
Add tests for associations behaviour on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
groupsky committed Oct 29, 2018
1 parent 6d28796 commit d83fad3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions __tests__/models/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,30 @@ describe('model member', () => {
})
})
})

describe('associations', () => {
test('familyMembers: on delete removes from parent', async () => {
const child = await ah.api.models.member.create(generateMember())
const parent = await ah.api.models.member.create(generateMember())
await parent.setFamilyMembers([child.id])

expect(await parent.getFamilyMembers()).toEqual([expect.objectContaining({ id: child.id })])

child.destroy({ force: true })

expect(await parent.getFamilyMembers()).toEqual([])
})

test('familyMasters: on delete removes from child', async () => {
const child = await ah.api.models.member.create(generateMember())
const parent = await ah.api.models.member.create(generateMember())
await parent.setFamilyMembers([child.id])

expect(await child.getFamilyMasters()).toEqual([expect.objectContaining({ id: parent.id })])

parent.destroy({ force: true })

expect(await child.getFamilyMasters()).toEqual([])
})
})
})
23 changes: 23 additions & 0 deletions __tests__/models/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,27 @@ describe('model payment', () => {
expect(payments).toEqual(expect.arrayContaining([expect.objectContaining({ id: payment2.id })]))
})
})

describe('associations', () => {
test('billingMember: on delete sets to null', async () => {
const member = await ah.api.models.member.create(generateMember())
const payment = await ah.api.models.payment.create(generatePayment({ billingMemberId: member.id }))

expect(await payment.getBillingMember()).toEqual(expect.objectContaining({ id: member.id }))
await member.destroy({ force: true })
expect(await payment.getBillingMember()).toBe(null)
})

test('members: on delete removes from payment', async () => {
const member = await ah.api.models.member.create(generateMember())
const payment = await ah.api.models.payment.create(generatePayment({
billingMemberId: member.id,
members: [member.id]
}))

expect(await payment.getMembers()).toEqual([expect.objectContaining({ id: member.id })])
await member.destroy({ force: true })
expect(await payment.getMembers()).toEqual([])
})
})
})

0 comments on commit d83fad3

Please sign in to comment.