node-event-bot/tests/repetition.test.ts

97 lines
3.2 KiB
TypeScript

import { findInScheduleTypes } from '../server/helper/typeFind'
import { supportedSchedule } from '../server/types/scheduledEventTypes'
import { createEventInGuild, getRepetitonInfo } from '../server/handler/repeatingEvents/helper'
import { RepetitonInfo } from '../server/types/scheduledEventTypes'
import { handleRepeatingEvent } from '../server/handler/repeatingEvents/repeatingEvents.controller'
import { GuildScheduledEventCreateOptions } from 'discord.js'
describe('Schedule names are parsed correctly', () => {
const dailyValue: supportedSchedule = 'daily'
const weeklyValue: supportedSchedule = 'weekly'
const monthlyValue: supportedSchedule = 'monthly'
test('Easy schedule names', () => {
expect(findInScheduleTypes('daily')).toEqual(dailyValue)
expect(findInScheduleTypes('weekly')).toEqual(weeklyValue)
expect(findInScheduleTypes('monthly')).toEqual(monthlyValue)
})
test('Medium schedule names', () => {
expect(findInScheduleTypes('Daily')).toEqual(dailyValue)
expect(findInScheduleTypes('Weekly')).toEqual(weeklyValue)
expect(findInScheduleTypes('Monthly')).toEqual(monthlyValue)
expect(findInScheduleTypes('DAILY')).toEqual(dailyValue)
expect(findInScheduleTypes('WEEKLy')).toEqual(weeklyValue)
expect(findInScheduleTypes('MONTHly')).toEqual(monthlyValue)
})
})
describe('Parsing of Repetition Info from Description String', () => {
test('Happy Path', () => {
const inputString = '$rep:daily:1/3'
const expectedInfo: RepetitonInfo = {
totalAmount: 3,
alreadyOccured: 1,
schedule: 'daily'
}
expect(getRepetitonInfo(inputString)).toEqual(expectedInfo)
})
})
const oldEvent = {
"id": "965576921410859018",
"guildId": "907936880190967850",
"channelId": "907936880190967854",
"creatorId": "191951058111692800",
"name": "Created event",
"description": "$rep:daily:2022-05-22",
"createdTimestamp": 1650294000782,
"createdAt": new Date("2022-04-01"),
"scheduledStartTimestamp": 1650294000782,
"scheduledEndTimestamp": null,
"privacyLevel": "GUILD_ONLY",
"status": "ACTIVE",
"entityType": "VOICE",
"entityId": null,
"userCount": null,
"creator": null,
guild: {}
}
const newEvent = {
"id": "965576921410859018",
"guildId": "907936880190967850",
"channelId": "907936880190967854",
"creatorId": "191951058111692800",
"createdTimestamp": 1650294000782,
"name": "Created event",
"description": "$rep:daily:2022-05-22",
"scheduledStartTimestamp": 1650294000782,
"scheduledEndTimestamp": null,
"privacyLevel": "GUILD_ONLY",
"status": "COMPLETED",
"entityType": "VOICE",
"entityId": null,
"userCount": null,
"creator": null,
guild: {}
}
jest.mock('../server/handler/repeatingEvents/helper.ts', () => ({
...(jest.requireActual('../server/handler/repeatingEvents/helper.ts')),
createEventInGuild: jest.fn().mockImplementation((opt: any) => {
return
})
}))
test('handleRepeatingEvent', () => {
const expectedOptions: GuildScheduledEventCreateOptions = {
channel: "",
description: "",
name: newEvent.name,
entityType: <'VOICE'>newEvent.entityType,
privacyLevel: <'GUILD_ONLY'>newEvent.privacyLevel,
reason: 'Repetition',
scheduledStartTime: ""
}
//@ts-ignore
handleRepeatingEvent(oldEvent, newEvent)
expect(createEventInGuild).toHaveBeenCalledWith({}, expectedOptions)
})