133 lines
4.1 KiB
TypeScript
133 lines
4.1 KiB
TypeScript
/**
|
|
* SnippetMenu (components/SnippetMenu.tsx) — per-snippet options dialog:
|
|
* view orders, edit, copy URL and delete with confirmation.
|
|
*
|
|
* The trigger has no testID, so it is located as the first TouchableOpacity
|
|
* rendered by the component (the dialog's backdrop only mounts while open).
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { Alert, Clipboard, TouchableOpacity } from 'react-native'
|
|
import { act, fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
import Toast from 'react-native-toast-message'
|
|
|
|
import { SnippetMenu } from '@/components/SnippetMenu'
|
|
import type { VendorSnippet } from '@/types/vendor-snippets'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const snippet: VendorSnippet = {
|
|
id: 5,
|
|
snippetCode: 'VS_5',
|
|
slotId: 2,
|
|
isPermanent: false,
|
|
skuIds: [10],
|
|
products: [{ id: 10, name: 'Chicken' }],
|
|
validTill: null,
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
accessUrl: 'https://app.freshyo.in/vendor-order-list?id=VS_5',
|
|
}
|
|
|
|
const onEdit = jest.fn()
|
|
const onDelete = jest.fn()
|
|
const onViewOrders = jest.fn()
|
|
|
|
const renderMenu = (overrides: Partial<VendorSnippet> = {}) =>
|
|
renderWithProviders(
|
|
<SnippetMenu
|
|
snippet={{ ...snippet, ...overrides }}
|
|
onEdit={onEdit}
|
|
onDelete={onDelete}
|
|
onViewOrders={onViewOrders}
|
|
/>
|
|
)
|
|
|
|
const openMenu = (result: ReturnType<typeof renderWithProviders>) => {
|
|
fireEvent.press(result.UNSAFE_getAllByType(TouchableOpacity)[0])
|
|
}
|
|
|
|
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?.())
|
|
}
|
|
|
|
describe('SnippetMenu', () => {
|
|
beforeEach(() => {
|
|
onEdit.mockClear()
|
|
onDelete.mockClear()
|
|
onViewOrders.mockClear()
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
jest.spyOn(Clipboard, 'setString').mockImplementation(async () => {})
|
|
})
|
|
|
|
it('opens the options dialog with every action', async () => {
|
|
const result = renderMenu()
|
|
|
|
expect(screen.queryByText('Snippet Options')).toBeNull()
|
|
openMenu(result)
|
|
|
|
expect(await screen.findByText('Snippet Options')).toBeVisible()
|
|
expect(screen.getByText('View Orders')).toBeVisible()
|
|
expect(screen.getByText('Edit')).toBeVisible()
|
|
expect(screen.getByText('Copy URL')).toBeVisible()
|
|
expect(screen.getByText('Delete')).toBeVisible()
|
|
})
|
|
|
|
it('calls onViewOrders with the snippet code', async () => {
|
|
const result = renderMenu()
|
|
openMenu(result)
|
|
|
|
fireEvent.press(await screen.findByText('View Orders'))
|
|
expect(onViewOrders).toHaveBeenCalledWith('VS_5')
|
|
})
|
|
|
|
it('calls onEdit with the snippet', async () => {
|
|
const result = renderMenu()
|
|
openMenu(result)
|
|
|
|
fireEvent.press(await screen.findByText('Edit'))
|
|
expect(onEdit).toHaveBeenCalledWith(expect.objectContaining({ id: 5 }))
|
|
})
|
|
|
|
it('asks for confirmation before deleting and calls onDelete', async () => {
|
|
const result = renderMenu()
|
|
openMenu(result)
|
|
|
|
fireEvent.press(await screen.findByText('Delete'))
|
|
|
|
expect(Alert.alert).toHaveBeenCalledWith(
|
|
'Delete Snippet',
|
|
'Are you sure you want to delete this vendor snippet?',
|
|
expect.any(Array)
|
|
)
|
|
expect(onDelete).not.toHaveBeenCalled()
|
|
|
|
pressLastAlertButton('Delete')
|
|
expect(onDelete).toHaveBeenCalledWith(5)
|
|
})
|
|
|
|
it('copies the access URL to the clipboard', async () => {
|
|
const result = renderMenu()
|
|
openMenu(result)
|
|
|
|
fireEvent.press(await screen.findByText('Copy URL'))
|
|
|
|
await waitFor(() =>
|
|
expect(Clipboard.setString).toHaveBeenCalledWith(snippet.accessUrl)
|
|
)
|
|
expect(Toast.show).toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: 'success', text1: 'URL copied to clipboard' })
|
|
)
|
|
})
|
|
|
|
it('alerts when there is no URL to copy', async () => {
|
|
const result = renderMenu({ accessUrl: undefined })
|
|
openMenu(result)
|
|
|
|
fireEvent.press(await screen.findByText('Copy URL'))
|
|
|
|
expect(Alert.alert).toHaveBeenCalledWith('Error', 'No URL available to copy')
|
|
expect(Clipboard.setString).not.toHaveBeenCalled()
|
|
})
|
|
})
|