|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import Reminders from '@/pages/Reminders'; |
| 5 | + |
| 6 | +const toastMock = jest.fn(); |
| 7 | +jest.mock('@/hooks/use-toast', () => ({ |
| 8 | + useToast: () => ({ toast: toastMock }), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('@/components/ui/button', () => ({ |
| 12 | + Button: ({ children, ...props }: React.PropsWithChildren & React.ButtonHTMLAttributes<HTMLButtonElement>) => ( |
| 13 | + <button {...props}>{children}</button> |
| 14 | + ), |
| 15 | +})); |
| 16 | +jest.mock('@/components/ui/input', () => ({ |
| 17 | + Input: ({ ...props }: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />, |
| 18 | +})); |
| 19 | +jest.mock('@/components/ui/label', () => ({ |
| 20 | + Label: ({ children, ...props }: React.PropsWithChildren & React.LabelHTMLAttributes<HTMLLabelElement>) => ( |
| 21 | + <label {...props}>{children}</label> |
| 22 | + ), |
| 23 | +})); |
| 24 | +jest.mock('@/components/ui/dialog', () => ({ |
| 25 | + Dialog: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 26 | + DialogContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 27 | + DialogHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 28 | + DialogTitle: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 29 | + DialogDescription: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 30 | + DialogTrigger: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 31 | + DialogFooter: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 32 | +})); |
| 33 | +jest.mock('@/components/ui/alert-dailog', () => ({ |
| 34 | + AlertDialog: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 35 | + AlertDialogTrigger: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 36 | + AlertDialogContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 37 | + AlertDialogHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 38 | + AlertDialogTitle: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 39 | + AlertDialogDescription: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 40 | + AlertDialogFooter: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 41 | + AlertDialogCancel: ({ children }: React.PropsWithChildren) => <button>{children}</button>, |
| 42 | + AlertDialogAction: ({ children, ...props }: React.PropsWithChildren & React.ButtonHTMLAttributes<HTMLButtonElement>) => <button {...props}>{children}</button>, |
| 43 | +})); |
| 44 | + |
| 45 | +const listRemindersMock = jest.fn(); |
| 46 | +const createReminderMock = jest.fn(); |
| 47 | +const deleteReminderMock = jest.fn(); |
| 48 | +const runDueMock = jest.fn(); |
| 49 | +const scheduleBillRemindersMock = jest.fn(); |
| 50 | +const reportAutopayResultMock = jest.fn(); |
| 51 | +jest.mock('@/api/reminders', () => ({ |
| 52 | + listReminders: (...args: unknown[]) => listRemindersMock(...args), |
| 53 | + createReminder: (...args: unknown[]) => createReminderMock(...args), |
| 54 | + deleteReminder: (...args: unknown[]) => deleteReminderMock(...args), |
| 55 | + runDue: (...args: unknown[]) => runDueMock(...args), |
| 56 | + scheduleBillReminders: (...args: unknown[]) => scheduleBillRemindersMock(...args), |
| 57 | + reportAutopayResult: (...args: unknown[]) => reportAutopayResultMock(...args), |
| 58 | +})); |
| 59 | + |
| 60 | +const listBillsMock = jest.fn(); |
| 61 | +jest.mock('@/api/bills', () => ({ |
| 62 | + listBills: (...args: unknown[]) => listBillsMock(...args), |
| 63 | +})); |
| 64 | + |
| 65 | +describe('Reminders integration', () => { |
| 66 | + beforeEach(() => { |
| 67 | + jest.clearAllMocks(); |
| 68 | + listRemindersMock.mockResolvedValue([]); |
| 69 | + listBillsMock.mockResolvedValue([ |
| 70 | + { id: 1, name: 'Electricity', autopay_enabled: true, channel_email: true, channel_whatsapp: true }, |
| 71 | + ]); |
| 72 | + scheduleBillRemindersMock.mockResolvedValue({ created: 6 }); |
| 73 | + reportAutopayResultMock.mockResolvedValue({ created: 2 }); |
| 74 | + createReminderMock.mockResolvedValue({ id: 10 }); |
| 75 | + deleteReminderMock.mockResolvedValue({}); |
| 76 | + runDueMock.mockResolvedValue({ processed: 0 }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('schedules bill reminders from bill scheduler panel', async () => { |
| 80 | + render(<Reminders />); |
| 81 | + await waitFor(() => expect(listBillsMock).toHaveBeenCalled()); |
| 82 | + await screen.findByText(/smart bill scheduling/i); |
| 83 | + |
| 84 | + await userEvent.type(screen.getByLabelText(/reminder offsets/i), '7,3,1'); |
| 85 | + await userEvent.click(screen.getByRole('button', { name: /schedule bill reminders/i })); |
| 86 | + |
| 87 | + await waitFor(() => expect(scheduleBillRemindersMock).toHaveBeenCalled()); |
| 88 | + expect(scheduleBillRemindersMock).toHaveBeenCalledWith(1, [7, 3, 1]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('sends autopay result follow-up', async () => { |
| 92 | + render(<Reminders />); |
| 93 | + await waitFor(() => expect(listBillsMock).toHaveBeenCalled()); |
| 94 | + await screen.findByText(/smart bill scheduling/i); |
| 95 | + |
| 96 | + await userEvent.click(screen.getByRole('button', { name: /autopay success/i })); |
| 97 | + await waitFor(() => expect(reportAutopayResultMock).toHaveBeenCalledWith(1, 'SUCCESS')); |
| 98 | + }); |
| 99 | +}); |
0 commit comments