diff --git a/__tests__/models/member.js b/__tests__/models/member.js index c8c6358..d3bcb96 100644 --- a/__tests__/models/member.js +++ b/__tests__/models/member.js @@ -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([]) + }) + }) }) diff --git a/__tests__/models/payment.js b/__tests__/models/payment.js index 8ac5118..b08767e 100644 --- a/__tests__/models/payment.js +++ b/__tests__/models/payment.js @@ -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([]) + }) + }) })