135 lines
4.3 KiB
TypeScript
135 lines
4.3 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 15 — SnippetMenu (`tests/components/snippet-menu.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert, Clipboard } from 'react-native'
|
|
import { fireEvent, render } from '@testing-library/react-native'
|
|
|
|
import { SnippetMenu } from '@/components/SnippetMenu'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { SNIPPET_1 } from '../fixtures'
|
|
|
|
function openMenu(view: ReturnType<typeof render>) {
|
|
const trigger = view.UNSAFE_getAllByProps({ name: 'dots-three-vertical' })[0]
|
|
fireEvent.press(trigger)
|
|
}
|
|
|
|
describe('SnippetMenu (ASMENU-01..05)', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
it('ASMENU-01: the menu lists View Orders / Edit / Copy URL / Delete', () => {
|
|
const view = render(
|
|
<SnippetMenu
|
|
snippet={SNIPPET_1 as any}
|
|
onEdit={jest.fn()}
|
|
onDelete={jest.fn()}
|
|
onViewOrders={jest.fn()}
|
|
/>
|
|
)
|
|
|
|
openMenu(view)
|
|
|
|
expect(view.getByText('Snippet Options')).toBeTruthy()
|
|
expect(view.getByText('View Orders')).toBeTruthy()
|
|
expect(view.getByText('Edit')).toBeTruthy()
|
|
expect(view.getByText('Copy URL')).toBeTruthy()
|
|
expect(view.getByText('Delete')).toBeTruthy()
|
|
})
|
|
|
|
it('ASMENU-02: Copy URL without an access URL alerts and copies nothing', async () => {
|
|
const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
const copySpy = jest.spyOn(Clipboard, 'setString').mockImplementation(async () => undefined)
|
|
const view = render(
|
|
<SnippetMenu
|
|
snippet={{ ...SNIPPET_1, accessUrl: null } as any}
|
|
onEdit={jest.fn()}
|
|
onDelete={jest.fn()}
|
|
onViewOrders={jest.fn()}
|
|
/>
|
|
)
|
|
|
|
openMenu(view)
|
|
fireEvent.press(view.getByText('Copy URL'))
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'No URL available to copy')
|
|
expect(copySpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ASMENU-03: Copy URL writes the access URL to the clipboard', async () => {
|
|
const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
const copySpy = jest.spyOn(Clipboard, 'setString').mockImplementation(async () => undefined)
|
|
const view = render(
|
|
<SnippetMenu
|
|
snippet={SNIPPET_1 as any}
|
|
onEdit={jest.fn()}
|
|
onDelete={jest.fn()}
|
|
onViewOrders={jest.fn()}
|
|
/>
|
|
)
|
|
|
|
openMenu(view)
|
|
fireEvent.press(view.getByText('Copy URL'))
|
|
|
|
expect(copySpy).toHaveBeenCalledWith('https://access.test/ABC123')
|
|
expect(alertSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ASMENU-04: Delete confirms, then calls onDelete with the snippet id', () => {
|
|
const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
const onDelete = jest.fn()
|
|
const view = render(
|
|
<SnippetMenu
|
|
snippet={SNIPPET_1 as any}
|
|
onEdit={jest.fn()}
|
|
onDelete={onDelete}
|
|
onViewOrders={jest.fn()}
|
|
/>
|
|
)
|
|
|
|
openMenu(view)
|
|
fireEvent.press(view.getByText('Delete'))
|
|
|
|
expect(alertSpy).toHaveBeenCalledTimes(1)
|
|
const [title, message, buttons] = alertSpy.mock.calls[0]
|
|
expect(title).toBe('Delete Snippet')
|
|
expect(message).toBe('Are you sure you want to delete this vendor snippet?')
|
|
expect(buttons?.map((button) => button.text)).toEqual(['Cancel', 'Delete'])
|
|
expect(onDelete).not.toHaveBeenCalled()
|
|
|
|
buttons?.find((button) => button.text === 'Delete')?.onPress?.()
|
|
expect(onDelete).toHaveBeenCalledWith(1)
|
|
})
|
|
|
|
it('ASMENU-05: no menu option is currently disabled and a disabled option would no-op', () => {
|
|
const onEdit = jest.fn()
|
|
const onViewOrders = jest.fn()
|
|
const onDelete = jest.fn()
|
|
const view = render(
|
|
<SnippetMenu
|
|
snippet={SNIPPET_1 as any}
|
|
onEdit={onEdit}
|
|
onDelete={onDelete}
|
|
onViewOrders={onViewOrders}
|
|
/>
|
|
)
|
|
|
|
openMenu(view)
|
|
|
|
// The source supports `option.disabled` (handleOptionPress returns early and
|
|
// the row gets `disabled`), but every built-in option is enabled today.
|
|
const titles = ['View Orders', 'Edit', 'Copy URL', 'Delete']
|
|
titles.forEach((title) => {
|
|
expect(view.getByText(title).parent?.props.disabled).toBeFalsy()
|
|
})
|
|
|
|
fireEvent.press(view.getByText('View Orders'))
|
|
expect(onViewOrders).toHaveBeenCalledWith('ABC123')
|
|
expect(onEdit).not.toHaveBeenCalled()
|
|
expect(onDelete).not.toHaveBeenCalled()
|
|
})
|
|
})
|