/** * Send notifications * (app/(drawer)/dashboard/send-notifications/index.tsx) — eligible-user * dropdown, the send-to-all confirmation and the sendNotification mutation. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert, TouchableOpacity } from 'react-native' import { act, fireEvent, screen, waitFor } from '@testing-library/react-native' import MaterialIcons from '@expo/vector-icons/MaterialIcons' import SendNotifications from '@/app/(drawer)/dashboard/send-notifications/index' import { resetStorage, setupApiMocks } from '../mocks/api' import { expectProcedureCalled, lastProcedureInput, procCount, } from '../mocks/procedures' import { routerMock } from '../mocks/router' import { renderWithProviders } from '../helpers/render' const usersResponse = { users: [ { id: 1, name: 'Alice', mobile: '9000000001', isEligibleForNotif: true }, { id: 2, name: 'Bob', mobile: '9000000002', isEligibleForNotif: false }, ], } const pressLastAlertButton = (label: string) => { const calls = (Alert.alert as jest.Mock).mock.calls const call = calls[calls.length - 1] const buttons = (call?.[2] ?? []) as { text: string; onPress?: () => void }[] act(() => buttons.find((b) => b.text === label)?.onPress?.()) } const fillMessage = (title = 'Hello', message = 'World') => { fireEvent.changeText(screen.getByPlaceholderText('Enter notification title...'), title) fireEvent.changeText(screen.getByPlaceholderText('Enter notification message...'), message) } describe('Send notifications', () => { beforeEach(async () => { await resetStorage() setupApiMocks() jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('shows the loading state while users load', () => { setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': () => new Promise(() => {}) }, }) renderWithProviders() expect(screen.getByText('Loading users...')).toBeVisible() }) it('renders the form and loads eligible users only', async () => { setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse }, }) renderWithProviders() expect(await screen.findByText('Send Notifications')).toBeVisible() expect(screen.getByPlaceholderText('Enter notification title...')).toBeVisible() expect(screen.getByPlaceholderText('Enter notification message...')).toBeVisible() expect(screen.getByText('All Users')).toBeVisible() fireEvent.press(screen.getByText('Select users')) expect(await screen.findByText('9000000001 - Alice')).toBeVisible() expect(screen.queryByText('9000000002 - Bob')).toBeNull() }) it('keeps the send button disabled until both fields are filled', async () => { setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse }, }) const result = renderWithProviders() await screen.findByText('Send to All Users') const sendDisabled = () => result.UNSAFE_getAllByType(TouchableOpacity).at(-1)?.props.disabled expect(sendDisabled()).toBe(true) fillMessage() await waitFor(() => expect(sendDisabled()).toBe(false)) }) it('confirms and sends to all users when none are selected', async () => { const api = setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse, 'admin.user.sendNotification': { success: true, message: 'queued' }, }, }) renderWithProviders() await screen.findByText('Send to All Users') fillMessage(' Hello ', ' World ') fireEvent.press(screen.getByText('Send to All Users')) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith( 'Send to All Users?', expect.any(String), expect.any(Array) ) ) pressLastAlertButton('Send') await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.user.sendNotification')) expect(lastProcedureInput(api.fetchMock, 'admin.user.sendNotification')).toEqual({ userIds: [], title: 'Hello', text: 'World', }) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith('Success', 'Notification sent successfully!') ) }) it('cancels the send-to-all confirmation without mutating', async () => { const api = setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse, 'admin.user.sendNotification': { success: true, message: 'queued' }, }, }) renderWithProviders() await screen.findByText('Send to All Users') fillMessage() fireEvent.press(screen.getByText('Send to All Users')) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith( 'Send to All Users?', expect.any(String), expect.any(Array) ) ) pressLastAlertButton('Cancel') expect(procCount(api.fetchMock, 'admin.user.sendNotification')).toBe(0) }) it('sends to the selected user without a confirmation', async () => { const api = setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse, 'admin.user.sendNotification': { success: true, message: 'queued' }, }, }) renderWithProviders() await screen.findByText('Send to All Users') fireEvent.press(screen.getByText('Select users')) fireEvent.press(await screen.findByText('9000000001 - Alice')) fireEvent.press(screen.getByText('Done')) // Once a user is picked the button switches label. await waitFor(() => expect(screen.getByText('Send Notification')).toBeVisible()) fillMessage() fireEvent.press(screen.getByText('Send Notification')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.user.sendNotification')) expect(lastProcedureInput(api.fetchMock, 'admin.user.sendNotification')).toEqual({ userIds: [1], title: 'Hello', text: 'World', }) }) it('shows an error alert when the mutation fails', async () => { setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse, 'admin.user.sendNotification': () => { throw new Error('nope') }, }, }) renderWithProviders() await screen.findByText('Send to All Users') fireEvent.press(screen.getByText('Select users')) fireEvent.press(await screen.findByText('9000000001 - Alice')) fireEvent.press(screen.getByText('Done')) await waitFor(() => expect(screen.getByText('Send Notification')).toBeVisible()) fillMessage() fireEvent.press(screen.getByText('Send Notification')) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith('Error', expect.any(String)) ) }) it('shows the sending label while the mutation is pending', async () => { setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse, 'admin.user.sendNotification': () => new Promise(() => {}), }, }) renderWithProviders() await screen.findByText('Send to All Users') fireEvent.press(screen.getByText('Select users')) fireEvent.press(await screen.findByText('9000000001 - Alice')) fireEvent.press(screen.getByText('Done')) await waitFor(() => expect(screen.getByText('Send Notification')).toBeVisible()) fillMessage() fireEvent.press(screen.getByText('Send Notification')) await waitFor(() => expect(screen.getByText('Sending...')).toBeVisible()) }) it('refetches users with the debounced search query', async () => { const api = setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse }, }) renderWithProviders() await screen.findByText('Send to All Users') fireEvent.press(screen.getByText('Select users')) fireEvent.changeText(screen.getByTestId('undefined-search'), 'ali') act(() => { jest.advanceTimersByTime(2000) }) await waitFor(() => expect(lastProcedureInput(api.fetchMock, 'admin.user.getUsersForNotification')).toEqual({ search: 'ali', }) ) }) it('goes back from the header', async () => { setupApiMocks({ procedures: { 'admin.user.getUsersForNotification': usersResponse }, }) const result = renderWithProviders() await screen.findByText('Send Notifications') const backIcon = result.UNSAFE_getAllByType(MaterialIcons).find( (node) => node.props.name === 'chevron-left' )! fireEvent.press(backIcon) expect(routerMock.back).toHaveBeenCalled() }) })