|
| 1 | +import type { SapphireClient } from '@sapphire/framework'; |
| 2 | +import { Events } from 'discord.js'; |
| 3 | +import { copyClass, emitEvent, mockUser } from '@answeroverflow/discordjs-mock'; |
| 4 | +import { setupAnswerOverflowBot } from '~discord-bot/test/sapphire-mock'; |
| 5 | +import { |
| 6 | + createDiscordAccount, |
| 7 | + findDiscordAccountById, |
| 8 | +} from '@answeroverflow/db'; |
| 9 | +import { toAODiscordAccount } from '~discord-bot/utils/conversions'; |
| 10 | + |
| 11 | +let client: SapphireClient; |
| 12 | + |
| 13 | +beforeEach(async () => { |
| 14 | + client = await setupAnswerOverflowBot(); |
| 15 | +}); |
| 16 | + |
| 17 | +describe('Guild Member Update', () => { |
| 18 | + it('should update an existing user', async () => { |
| 19 | + const oldUser = mockUser(client, { |
| 20 | + username: 'old name', |
| 21 | + }); |
| 22 | + await createDiscordAccount(toAODiscordAccount(oldUser)); |
| 23 | + const newUser = copyClass(oldUser, client, { |
| 24 | + username: 'new name', |
| 25 | + }); |
| 26 | + await emitEvent(client, Events.UserUpdate, oldUser, newUser); |
| 27 | + const updated = await findDiscordAccountById(oldUser.id); |
| 28 | + expect(updated).toBeDefined(); |
| 29 | + expect(updated?.name).toBe('new name'); |
| 30 | + }); |
| 31 | + it("should not update a user that doesn't exist", async () => { |
| 32 | + const oldUser = mockUser(client, { |
| 33 | + username: 'old name', |
| 34 | + }); |
| 35 | + const newUser = copyClass(oldUser, client, { |
| 36 | + username: 'new name', |
| 37 | + }); |
| 38 | + await emitEvent(client, Events.UserUpdate, oldUser, newUser); |
| 39 | + const updated = await findDiscordAccountById(oldUser.id); |
| 40 | + expect(updated).toBeNull(); |
| 41 | + }); |
| 42 | +}); |
0 commit comments