import { diff } from "jest-diff"; import { expect } from "vitest"; import type { Slot } from "@calcom/trpc/server/routers/viewer/slots/types"; export const expectedSlotsForSchedule = { IstWorkHours: { interval: { "1hr": { allPossibleSlotsStartingAt430: [ "04:30:00.000Z", "05:30:00.000Z", "06:30:00.000Z", "07:30:00.000Z", "08:30:00.000Z", "09:30:00.000Z", "10:30:00.000Z", "11:30:00.000Z", ], allPossibleSlotsStartingAt4: [ "04:00:00.000Z", "05:00:00.000Z", "06:00:00.000Z", "07:00:00.000Z", "08:00:00.000Z", "09:00:00.000Z", "10:00:00.000Z", "11:00:00.000Z", ], }, }, }, }; declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace jest { interface Matchers { toHaveTimeSlots(expectedSlots: string[], date: { dateString: string; doExactMatch?: boolean }): R; toHaveNoTimeSlots(date: { dateString: string }): R; } } } expect.extend({ toHaveTimeSlots( schedule: { slots: Record }, expectedSlots: string[], { dateString, doExactMatch }: { dateString: string; doExactMatch: boolean } ) { if (!schedule.slots[`${dateString}`]) { return { pass: false, message: () => `has no timeslots for ${dateString}`, }; } if ( !schedule.slots[`${dateString}`] .map((slot) => slot.time) .every((actualSlotTime, index) => { return `${dateString}T${expectedSlots[index]}` === actualSlotTime; }) ) { return { pass: false, message: () => `has incorrect timeslots for ${dateString}.\n\r ${diff( expectedSlots.map((expectedSlot) => `${dateString}T${expectedSlot}`), schedule.slots[`${dateString}`].map((slot) => slot.time) )}`, }; } if (doExactMatch) { return { pass: expectedSlots.length === schedule.slots[`${dateString}`].length, message: () => `number of slots don't match for ${dateString}. Expected ${expectedSlots.length} but got ${ schedule.slots[`${dateString}`].length }`, }; } return { pass: true, message: () => "has correct timeslots ", }; }, toHaveNoTimeSlots(schedule: { slots: Record }, { dateString }: { dateString: string }) { if (!schedule.slots[`${dateString}`] || schedule.slots[`${dateString}`].length === 0) { return { pass: true, message: () => `has no timeslots for ${dateString}`, }; } return { pass: false, message: () => `has timeslots for ${dateString}`, }; }, }); export { expect } from "vitest";