327 lines
11 KiB
TypeScript
327 lines
11 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 14.2 — Send notifications (`tests/pages/send-notifications.test.tsx`).
|
|
*
|
|
* Only the page's admin mutation (`admin.user.sendNotification`) is asserted —
|
|
* no push-notification service/provider is exercised.
|
|
*/
|
|
import React from 'react'
|
|
import { Alert, TouchableOpacity } from 'react-native'
|
|
import { act, fireEvent, waitFor } from '@testing-library/react-native'
|
|
|
|
import SendNotifications from '@/app/(drawer)/dashboard/send-notifications/index'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
lastMutationInput,
|
|
procedureMutationCalls,
|
|
procedureQueryCalls,
|
|
setProcedure,
|
|
} from '../mocks/trpc-client'
|
|
import { router } from '../mocks/expo-router-mock'
|
|
import { NOTIFICATION_USERS } from '../fixtures'
|
|
|
|
const ELIGIBLE_USERS = NOTIFICATION_USERS.map((user) => ({ ...user, isEligibleForNotif: true }))
|
|
|
|
const TITLE_PLACEHOLDER = 'Enter notification title...'
|
|
const MESSAGE_PLACEHOLDER = 'Enter notification message...'
|
|
|
|
function setEligibleUsers(users = ELIGIBLE_USERS) {
|
|
setProcedure('admin.user.getUsersForNotification', { data: { users, count: users.length } })
|
|
}
|
|
|
|
describe('Send notifications (ANOTIF-01..16)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
async function renderPage() {
|
|
const view = renderWithProviders(<SendNotifications />, { auth: 'in' })
|
|
await flush()
|
|
return view
|
|
}
|
|
|
|
type View = Awaited<ReturnType<typeof renderPage>>
|
|
|
|
function fillForm(view: View, title = 'Hello', message = 'World') {
|
|
fireEvent.changeText(view.getByPlaceholderText(TITLE_PLACEHOLDER), title)
|
|
fireEvent.changeText(view.getByPlaceholderText(MESSAGE_PLACEHOLDER), message)
|
|
}
|
|
|
|
async function pressSendButton(view: View) {
|
|
fireEvent.press(view.getByTestId('send-notification-button'))
|
|
await flush(2)
|
|
}
|
|
|
|
async function openDropdown(view: View) {
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown'))
|
|
await flush()
|
|
}
|
|
|
|
async function pressAlertButton(label: 'Cancel' | 'Send') {
|
|
const call = alertSpy.mock.calls.filter((args) => Array.isArray(args[2])).pop()
|
|
const button = (call?.[2] ?? []).find((item: any) => item.text === label)
|
|
await act(async () => {
|
|
button?.onPress?.()
|
|
})
|
|
await flush(2)
|
|
}
|
|
|
|
it('ANOTIF-01: shows the loading state while eligible users load', async () => {
|
|
setProcedure('admin.user.getUsersForNotification', { mode: 'pending' })
|
|
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('Loading users...')).toBeTruthy()
|
|
})
|
|
|
|
it('ANOTIF-02: renders the title, message, user dropdown and send button', async () => {
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('Send Notifications')).toBeTruthy()
|
|
expect(view.getByText('Title')).toBeTruthy()
|
|
expect(view.getByText('Message')).toBeTruthy()
|
|
expect(view.getByText('Select Users (Optional)')).toBeTruthy()
|
|
expect(view.getByText('Leave empty to send to all users')).toBeTruthy()
|
|
expect(view.getByText('All Users')).toBeTruthy()
|
|
expect(view.getByText('Send to All Users')).toBeTruthy()
|
|
expect(view.getByPlaceholderText(TITLE_PLACEHOLDER)).toBeTruthy()
|
|
expect(view.getByPlaceholderText(MESSAGE_PLACEHOLDER)).toBeTruthy()
|
|
expect(view.getByTestId('notification-user-dropdown')).toBeTruthy()
|
|
})
|
|
|
|
it('ANOTIF-03: an empty title alerts "Please enter a title"', async () => {
|
|
const view = await renderPage()
|
|
|
|
// The button is disabled until both fields are filled, so invoke the
|
|
// handler the touchable would run (the validation branch is UI-reachable
|
|
// only if the disabled guard is bypassed).
|
|
const button = view
|
|
.UNSAFE_getAllByType(TouchableOpacity)
|
|
.find((node) => node.props.testID === 'send-notification-button')
|
|
await act(async () => {
|
|
button?.props.onPress?.()
|
|
})
|
|
await flush()
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Please enter a title')
|
|
expect(procedureMutationCalls('admin.user.sendNotification')).toHaveLength(0)
|
|
})
|
|
|
|
it('ANOTIF-04: an empty message alerts "Please enter a message"', async () => {
|
|
const view = await renderPage()
|
|
fireEvent.changeText(view.getByPlaceholderText(TITLE_PLACEHOLDER), 'Hello')
|
|
|
|
const button = view
|
|
.UNSAFE_getAllByType(TouchableOpacity)
|
|
.find((node) => node.props.testID === 'send-notification-button')
|
|
await act(async () => {
|
|
button?.props.onPress?.()
|
|
})
|
|
await flush()
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Please enter a message')
|
|
expect(procedureMutationCalls('admin.user.sendNotification')).toHaveLength(0)
|
|
})
|
|
|
|
it('ANOTIF-05: no eligible users leaves the dropdown empty but still allows send-to-all', async () => {
|
|
const view = await renderPage()
|
|
|
|
expect(view.getByText('Send to All Users')).toBeTruthy()
|
|
expect(view.getByText('All Users')).toBeTruthy()
|
|
|
|
await openDropdown(view)
|
|
|
|
expect(view.queryByTestId('notification-user-dropdown-option-5')).toBeNull()
|
|
expect(view.queryByTestId('notification-user-dropdown-option-6')).toBeNull()
|
|
})
|
|
|
|
it('ANOTIF-06: Send to All asks for confirmation and Cancel sends nothing', async () => {
|
|
const view = await renderPage()
|
|
fillForm(view)
|
|
|
|
await pressSendButton(view)
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Send to All Users?',
|
|
'This will send the notification to all users with push tokens. Continue?',
|
|
expect.any(Array)
|
|
)
|
|
const buttons = alertSpy.mock.calls.find((args) => Array.isArray(args[2]))?.[2] as any[]
|
|
expect(buttons.map((button) => button.text)).toEqual(['Cancel', 'Send'])
|
|
|
|
await pressAlertButton('Cancel')
|
|
|
|
expect(procedureMutationCalls('admin.user.sendNotification')).toHaveLength(0)
|
|
})
|
|
|
|
it('ANOTIF-07: confirming all sends an empty userIds payload with trimmed title/text', async () => {
|
|
const view = await renderPage()
|
|
fillForm(view, ' Hello ', ' World ')
|
|
|
|
await pressSendButton(view)
|
|
await pressAlertButton('Send')
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.user.sendNotification')).toEqual({
|
|
userIds: [],
|
|
title: 'Hello',
|
|
text: 'World',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('ANOTIF-08: selected users are sent as their ids', async () => {
|
|
setEligibleUsers()
|
|
const view = await renderPage()
|
|
|
|
await openDropdown(view)
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-5'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-6'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-done'))
|
|
await flush()
|
|
|
|
fillForm(view)
|
|
await pressSendButton(view)
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.user.sendNotification')).toEqual({
|
|
userIds: [5, 6],
|
|
title: 'Hello',
|
|
text: 'World',
|
|
})
|
|
)
|
|
})
|
|
|
|
it('ANOTIF-09: one selected user is displayed as mobile - name', async () => {
|
|
setEligibleUsers()
|
|
const view = await renderPage()
|
|
|
|
await openDropdown(view)
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-5'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-done'))
|
|
await flush()
|
|
|
|
expect(view.getAllByText('9876543210 - Test User').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.queryByText('1 user selected')).toBeNull()
|
|
})
|
|
|
|
it('ANOTIF-10: multiple selections display "N users selected"', async () => {
|
|
setEligibleUsers()
|
|
const view = await renderPage()
|
|
|
|
await openDropdown(view)
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-5'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-6'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-done'))
|
|
await flush()
|
|
|
|
expect(view.getByText('2 users selected')).toBeTruthy()
|
|
})
|
|
|
|
it('ANOTIF-11: searching the dropdown refetches users with the query', async () => {
|
|
setEligibleUsers()
|
|
const view = await renderPage()
|
|
|
|
await openDropdown(view)
|
|
|
|
fireEvent.changeText(view.getByTestId('notification-user-dropdown-search'), '987')
|
|
|
|
// The dropdown debounces `onSearch` by 2s; wait for the refetch it triggers.
|
|
await waitFor(
|
|
() => {
|
|
const searches = procedureQueryCalls('admin.user.getUsersForNotification').map(
|
|
(input) => input?.search
|
|
)
|
|
expect(searches).toContain('987')
|
|
},
|
|
{ timeout: 4000 }
|
|
)
|
|
})
|
|
|
|
it('ANOTIF-12: a pending send shows "Sending..." and blocks a second submit', async () => {
|
|
setEligibleUsers()
|
|
setProcedure('admin.user.sendNotification', { mode: 'pending' })
|
|
const view = await renderPage()
|
|
|
|
await openDropdown(view)
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-5'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-done'))
|
|
await flush()
|
|
|
|
fillForm(view)
|
|
await pressSendButton(view)
|
|
|
|
expect(view.getByText('Sending...')).toBeTruthy()
|
|
|
|
fireEvent.press(view.getByTestId('send-notification-button'))
|
|
await flush()
|
|
|
|
expect(procedureMutationCalls('admin.user.sendNotification')).toHaveLength(1)
|
|
})
|
|
|
|
it('ANOTIF-13: success alerts and resets the form and selection', async () => {
|
|
const view = await renderPage()
|
|
fillForm(view, 'Hello', 'World')
|
|
|
|
await pressSendButton(view)
|
|
await pressAlertButton('Send')
|
|
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith('Success', 'Notification sent successfully!')
|
|
)
|
|
expect(view.getByPlaceholderText(TITLE_PLACEHOLDER).props.value).toBe('')
|
|
expect(view.getByPlaceholderText(MESSAGE_PLACEHOLDER).props.value).toBe('')
|
|
expect(view.getByText('All Users')).toBeTruthy()
|
|
})
|
|
|
|
it('ANOTIF-14: a failed send alerts the mutation error', async () => {
|
|
setEligibleUsers()
|
|
setProcedure('admin.user.sendNotification', { mode: 'error', error: new Error('send failed') })
|
|
const view = await renderPage()
|
|
|
|
await openDropdown(view)
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-5'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-done'))
|
|
await flush()
|
|
|
|
fillForm(view)
|
|
await pressSendButton(view)
|
|
|
|
await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'send failed'))
|
|
})
|
|
|
|
it('ANOTIF-15: a failed send alerts twice (mutation onError + catch) — current gap', async () => {
|
|
setEligibleUsers()
|
|
setProcedure('admin.user.sendNotification', { mode: 'error', error: new Error('send failed') })
|
|
const view = await renderPage()
|
|
|
|
await openDropdown(view)
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-option-5'))
|
|
fireEvent.press(view.getByTestId('notification-user-dropdown-done'))
|
|
await flush()
|
|
|
|
fillForm(view)
|
|
await pressSendButton(view)
|
|
|
|
await waitFor(() => {
|
|
const errorAlerts = alertSpy.mock.calls.filter(
|
|
(args) => args[0] === 'Error' && args[1] === 'send failed'
|
|
)
|
|
expect(errorAlerts).toHaveLength(2)
|
|
})
|
|
})
|
|
|
|
it('ANOTIF-16: the back button calls router.back', async () => {
|
|
const view = await renderPage()
|
|
|
|
fireEvent.press(view.getByTestId('send-notifications-back'))
|
|
|
|
expect(router.back).toHaveBeenCalled()
|
|
})
|
|
})
|