Skip to content

Commit

Permalink
Modify all tests to remove Deta references
Browse files Browse the repository at this point in the history
  • Loading branch information
Awesome-E committed Sep 8, 2024
1 parent 5128c10 commit b12999a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 81 deletions.
11 changes: 0 additions & 11 deletions .jest/mock-deta.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
const { createDefaultSettings } = require('../src/listeners/config')
// const deta = require('deta')

const mockBaseGet = jest.fn(id => createDefaultSettings({ id }))
const mockBasePut = jest.fn()

// jest.mock('deta', () => ({
// Deta: jest.fn(() => ({
// Base: jest.fn(() => ({
// get: (...args) => mockBaseGet(...args),
// put: (...args) => mockBasePut(...args)
// }))
// }))
// }))

jest.mock('../src/modules/database', () => ({
serverSettingsDB: {
get: (...args) => mockBaseGet(...args),
Expand All @@ -21,6 +11,5 @@ jest.mock('../src/modules/database', () => ({
}))

module.exports = {
Base: { get: mockBaseGet, put: mockBasePut },
serverSettingsDB: { get: mockBaseGet, put: mockBasePut }
}
8 changes: 4 additions & 4 deletions __tests__/commands/chat-input/attendance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Attendance Record Command', () => {
guild.voiceStates.cache.set('1', createVoiceState(channel))
await recordAttendance(interaction, client)
expect(guild.fetch).toBeCalled()
expect(detaMock.Base.put).toBeCalledWith(expectedConfig)
expect(detaMock.serverSettingsDB.put).toBeCalledWith(expectedConfig)
})
})

Expand All @@ -95,7 +95,7 @@ describe('Attendance List Command', () => {

const config = createDefaultSettings(guild)
config.attendance = {} // Exists, but no events
detaMock.Base.get.mockReturnValue(config)
detaMock.serverSettingsDB.get.mockReturnValue(config)

await listAttendance(this.interaction, client)
expect(this.interaction.reply).toBeCalledWith(expectedReply)
Expand All @@ -104,14 +104,14 @@ describe('Attendance List Command', () => {
it('Replies with the correct counts when events exist', async () => {
const config = createDefaultSettings(guild)
config.attendance = { [today + ' \u2013 name']: ['user-id', 'u2'], 'Bad name': [] }
detaMock.Base.get.mockReturnValue(config)
detaMock.serverSettingsDB.get.mockReturnValue(config)
const expectedReply = { content: `__**${today} \u2013 name:**__ 2\n__**Bad name:**__ 0` }

await listAttendance(this.interaction, client)
expect(this.interaction.reply).toBeCalledWith(expectedReply)

config.attendance = { 'Empty Only': [] }
detaMock.Base.get.mockReturnValue(config)
detaMock.serverSettingsDB.get.mockReturnValue(config)
const expected2 = { content: '__**Empty Only:**__ 0' }

await listAttendance(this.interaction, client)
Expand Down
24 changes: 12 additions & 12 deletions __tests__/commands/chat-input/inviteroles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ guild.invites.fetch.mockImplementation(async () => guild.invites.cache)
const invite = discordMock.createInvite(client, { channel, guild, code: 'INV', id: '11' }, guild)
guild.invites.cache.set(invite.id, invite)

detaMock.Base.get.mockReturnValue({ inviteRoles: [] })
detaMock.serverSettingsDB.get.mockReturnValue({ inviteRoles: [] })

const permissions = PermissionFlagsBits.ViewChannel
const role = discordMock.createRole(client, { name: 'ten', id: '10', permissions }, guild)
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('Inviteroles add command', () => {
})

it('Replies with an error if the rule already exists', async () => {
detaMock.Base.get.mockReturnValueOnce({ inviteRoles: [{ name: 'tens' }] })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ inviteRoles: [{ name: 'tens' }] })
const expectedReply = {
embeds: [{
color: 0xff0000,
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('Inviteroles add command', () => {
updated_at: 1234
}
await addInviteRule(this.interaction, client, false)
expect(detaMock.Base.put).toBeCalledWith({ inviteRoles: [expectedConfig] })
expect(detaMock.serverSettingsDB.put).toBeCalledWith({ inviteRoles: [expectedConfig] })
})
})

Expand Down Expand Up @@ -188,7 +188,7 @@ describe('Inviteroles details command', () => {
expect(this.interaction.reply).toBeCalledWith(expectedNoMatchReply)
this.interaction.reply.mockClear()

detaMock.Base.get.mockReturnValueOnce({ inviteRoles: [] })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ inviteRoles: [] })
delete this.options.name
const expectedNoRulesReply = {
content: 'Details of all role assignments:',
Expand All @@ -203,7 +203,7 @@ describe('Inviteroles details command', () => {
})

it('Replies with all invite role assignment rules if no name is specified', async () => {
detaMock.Base.get.mockReturnValueOnce({ inviteRoles: [] })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ inviteRoles: [] })
this.options = {
name: 'tens',
rename: 'ten-people',
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('Inviteroles delete command', () => {
})

it('Replies with an error if that invite role assignment does not exist', async () => {
detaMock.Base.get.mockReturnValueOnce({ inviteRoles: [] })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ inviteRoles: [] })
const expectedNoMatchReply = {
embeds: [{
color: 0xff0000,
Expand All @@ -267,7 +267,7 @@ describe('Inviteroles delete command', () => {
})

it('Replies with the rule that was deleted', async () => {
detaMock.Base.get.mockReturnValueOnce({ inviteRoles: [this.rule] })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ inviteRoles: [this.rule] })
const expectedNoMatchReply = {
embeds: [{
title: 'Deleted `tens`',
Expand All @@ -284,9 +284,9 @@ describe('Inviteroles delete command', () => {
})

it('Removees the role assigment rule from the database', async () => {
detaMock.Base.get.mockReturnValueOnce({ inviteRoles: [this.rule] })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ inviteRoles: [this.rule] })
await removeInviteRule(this.interaction, client)
expect(detaMock.Base.put).toBeCalledWith({ inviteRoles: [] })
expect(detaMock.serverSettingsDB.put).toBeCalledWith({ inviteRoles: [] })
})
})

Expand Down Expand Up @@ -318,7 +318,7 @@ describe('Inviteroles update command', () => {
invites: invite.code
}
this.interaction.reply.mockClear()
detaMock.Base.put.mockClear()
detaMock.serverSettingsDB.put.mockClear()
})

it('Replies with an error if the rule name is invalid', async () => {
Expand Down Expand Up @@ -419,7 +419,7 @@ describe('Inviteroles update command', () => {

it('Updates the invite role assignment rule in the database', async () => {
this.options['remove-roles'] = 'None'
detaMock.Base.get.mockReturnValueOnce({ inviteRoles: [this.rule] })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ inviteRoles: [this.rule] })
const expectedConfig = {
name: 'ten-people',
invites: ['INV'],
Expand All @@ -432,6 +432,6 @@ describe('Inviteroles update command', () => {
}
this.interaction.reply.mockImplementation(x => console.log(x))
await addInviteRule(this.interaction, client, true)
expect(detaMock.Base.put).toBeCalledWith({ inviteRoles: [expectedConfig] })
expect(detaMock.serverSettingsDB.put).toBeCalledWith({ inviteRoles: [expectedConfig] })
})
})
64 changes: 32 additions & 32 deletions __tests__/commands/chat-input/leaderboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function testNonExistent (thisArg, fn) {
content: 'No such leaderboard with name leaderboard exists',
ephemeral: true
}
detaMock.Base.get.mockReturnValue({})
detaMock.serverSettingsDB.get.mockReturnValue({})
await fn(thisArg.interaction, client)
expect(thisArg.interaction.reply).toBeCalledWith(expectedReply)

detaMock.Base.get.mockReturnValueOnce({ leaderboards: { blah: {} } })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ leaderboards: { blah: {} } })
await fn(thisArg.interaction, client)
expect(thisArg.interaction.reply).toBeCalledWith(expectedReply)
}
Expand All @@ -42,9 +42,9 @@ describe('Leaderboard Create Command', () => {
channel.send.mockReturnValue({ id: '1' })
})
beforeEach(() => {
detaMock.Base.get.mockClear()
detaMock.Base.put.mockReset()
detaMock.Base.get.mockReturnValue({})
detaMock.serverSettingsDB.get.mockClear()
detaMock.serverSettingsDB.put.mockReset()
detaMock.serverSettingsDB.get.mockReturnValue({})
this.interaction.options.getString
.mockReturnValueOnce('leaderboard') // Name
.mockReturnValueOnce('Leaderboard') // Title
Expand All @@ -53,7 +53,7 @@ describe('Leaderboard Create Command', () => {
})

afterAll(() => {
detaMock.Base.get.mockReset()
detaMock.serverSettingsDB.get.mockReset()
this.interaction.options.getSubcommand.mockClear()
channel.send.mockClear()
})
Expand All @@ -62,7 +62,7 @@ describe('Leaderboard Create Command', () => {
})

it('Replies with an error if a leaderboard with that name already exists', async () => {
detaMock.Base.get.mockReturnValueOnce({
detaMock.serverSettingsDB.get.mockReturnValueOnce({
leaderboards: { leaderboard: { title: 'test' } }
})
await createLeaderboard(this.interaction, client)
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('Leaderboard Create Command', () => {
}
this.interaction.options.getChannel.mockReturnValueOnce(undefined)
await createLeaderboard(this.interaction, client)
expect(detaMock.Base.put).toBeCalledWith(expectedDB)
expect(detaMock.serverSettingsDB.put).toBeCalledWith(expectedDB)
})

it('Sends a message to the target channel if specified', async () => {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('Leaderboard Create Command', () => {
scores: {}
}
await createLeaderboard(this.interaction, client)
expect(detaMock.Base.put).toBeCalledWith(expectedDB)
expect(detaMock.serverSettingsDB.put).toBeCalledWith(expectedDB)
})
})

Expand All @@ -140,14 +140,14 @@ describe('Leaderboard Delete Command', () => {
channel.send.mockReturnValue({ id: '1' })
})
beforeEach(() => {
detaMock.Base.get.mockClear()
detaMock.Base.put.mockReset()
detaMock.Base.get.mockReturnValue({})
detaMock.serverSettingsDB.get.mockClear()
detaMock.serverSettingsDB.put.mockReset()
detaMock.serverSettingsDB.get.mockReturnValue({})
this.interaction.options.getString.mockReturnValue('leaderboard')
})

afterAll(() => {
detaMock.Base.get.mockReset()
detaMock.serverSettingsDB.get.mockReset()
this.interaction.options.getSubcommand.mockClear()
channel.send.mockClear()
})
Expand All @@ -163,7 +163,7 @@ describe('Leaderboard Delete Command', () => {
title: 'Leaderboard',
type: 'user'
}
detaMock.Base.get.mockReturnValueOnce({ leaderboards: { leaderboard } })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ leaderboards: { leaderboard } })
const expectedReply = {
embeds: [{
title: 'Leaderboard Deleted: Leaderboard',
Expand All @@ -184,10 +184,10 @@ describe('Leaderboard Delete Command', () => {
title: 'Leaderboard',
type: 'user'
}
detaMock.Base.get.mockReturnValueOnce({ leaderboards: { leaderboard } })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ leaderboards: { leaderboard } })
await deleteLeaderboard(this.interaction, client)
const expectedDB = { leaderboards: {} }
expect(detaMock.Base.put).toBeCalledWith(expectedDB)
expect(detaMock.serverSettingsDB.put).toBeCalledWith(expectedDB)
})

it('Deletes the existing leaderboard post, if it exists', async () => {
Expand All @@ -203,7 +203,7 @@ describe('Leaderboard Delete Command', () => {
channelID: '2',
messageID: '1'
}
detaMock.Base.get.mockReturnValueOnce({ leaderboards: { leaderboard } })
detaMock.serverSettingsDB.get.mockReturnValueOnce({ leaderboards: { leaderboard } })

await deleteLeaderboard(this.interaction, client)
expect(delFn).toBeCalled()
Expand All @@ -229,14 +229,14 @@ describe('Leaderboard Repost Command', () => {
}
})
beforeEach(() => {
detaMock.Base.get.mockClear()
detaMock.Base.put.mockReset()
detaMock.Base.get.mockReturnValue({ leaderboards: { leaderboard: this.leaderboard } })
detaMock.serverSettingsDB.get.mockClear()
detaMock.serverSettingsDB.put.mockReset()
detaMock.serverSettingsDB.get.mockReturnValue({ leaderboards: { leaderboard: this.leaderboard } })
this.interaction.options.getString.mockReturnValue('leaderboard')
})

afterAll(() => {
detaMock.Base.get.mockReset()
detaMock.serverSettingsDB.get.mockReset()
this.interaction.options.getSubcommand.mockClear()
channel.send.mockClear()
})
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('Leaderboard Repost Command', () => {
scores: {}
}
await repostLeaderboard(this.interaction, client)
expect(detaMock.Base.put).toBeCalledWith(expectedDB)
expect(detaMock.serverSettingsDB.put).toBeCalledWith(expectedDB)
})
})

Expand All @@ -306,14 +306,14 @@ describe('Leaderboard Reset Command', () => {
}
})
beforeEach(() => {
detaMock.Base.get.mockClear()
detaMock.Base.put.mockReset()
detaMock.Base.get.mockReturnValue({ leaderboards: { leaderboard: this.leaderboard } })
detaMock.serverSettingsDB.get.mockClear()
detaMock.serverSettingsDB.put.mockReset()
detaMock.serverSettingsDB.get.mockReturnValue({ leaderboards: { leaderboard: this.leaderboard } })
this.interaction.options.getString.mockReturnValue('leaderboard')
})

afterAll(() => {
detaMock.Base.get.mockReset()
detaMock.serverSettingsDB.get.mockReset()
this.interaction.options.getSubcommand.mockReset()
channel.send.mockClear()
})
Expand Down Expand Up @@ -379,7 +379,7 @@ describe('Leaderboard Reset Command', () => {
scores: {}
}
await resetLeaderboard(this.interaction, client)
expect(detaMock.Base.put).toBeCalledWith(expectedDB)
expect(detaMock.serverSettingsDB.put).toBeCalledWith(expectedDB)
})
})

Expand All @@ -400,15 +400,15 @@ describe('Leaderboard Update Command (Post Leaderboards)', () => {
}
})
beforeEach(() => {
detaMock.Base.get.mockClear()
detaMock.Base.put.mockReset()
detaMock.Base.get.mockReturnValue({ leaderboards: { leaderboard: this.leaderboard } })
detaMock.serverSettingsDB.get.mockClear()
detaMock.serverSettingsDB.put.mockReset()
detaMock.serverSettingsDB.get.mockReturnValue({ leaderboards: { leaderboard: this.leaderboard } })
this.interaction.options.getString.mockReturnValueOnce('leaderboard') // name
this.leaderboard.scores = {}
})

afterAll(() => {
detaMock.Base.get.mockReset()
detaMock.serverSettingsDB.get.mockReset()
this.interaction.options.getSubcommand.mockClear()
channel.send.mockClear()
})
Expand Down Expand Up @@ -491,6 +491,6 @@ describe('Leaderboard Update Command (Post Leaderboards)', () => {
}

await updatePostsLeaderboard(this.interaction, client)
expect(detaMock.Base.put).toBeCalledWith(expectedDB)
expect(detaMock.serverSettingsDB.put).toBeCalledWith(expectedDB)
})
})
10 changes: 5 additions & 5 deletions __tests__/commands/chat-input/points.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ describe('Points Command', () => {
this.interaction.options.getChannel.mockReturnValue(channel)
})
beforeEach(() => {
detaMock.Base.get.mockClear()
detaMock.Base.get.mockReturnValue({})
detaMock.serverSettingsDB.get.mockClear()
detaMock.serverSettingsDB.get.mockReturnValue({})
})

afterEach(() => {
detaMock.Base.get.mockReset()
detaMock.serverSettingsDB.get.mockReset()
this.interaction.reply.mockClear()
})

Expand All @@ -50,7 +50,7 @@ describe('Points Command', () => {
})

it('Returns the correct amount of points for each task', async () => {
detaMock.Base.get.mockReturnValue({
detaMock.serverSettingsDB.get.mockReturnValue({
messageCounter: { counts: { u1: 1 } },
attendance: { event: ['u2'] }, // a different user
leaderboards: { weekly: { scores: { u1: [] } } } // zero
Expand All @@ -71,7 +71,7 @@ describe('Points Command', () => {
await respondWithPoints(this.interaction, client)
expect(this.interaction.reply).toBeCalledWith(expectedReply)

detaMock.Base.get.mockReturnValue({
detaMock.serverSettingsDB.get.mockReturnValue({
messageCounter: { counts: { u1: 3 } },
attendance: { event: ['u1'] },
leaderboards: { weekly: { scores: { u1: [{}, {}] } } } // 2 posts of some sort
Expand Down
Loading

0 comments on commit b12999a

Please sign in to comment.