|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * useNavActionDispatch — nav `action` items reach the action runtime |
| 11 | + * (framework#4509). |
| 12 | + * |
| 13 | + * The contract has three parts, and the third is the one the bug was made of: |
| 14 | + * resolve `actionDef.actionName` against `action` metadata, dispatch the |
| 15 | + * resolved def through the runner, and FAIL LOUDLY when either step comes up |
| 16 | + * empty — a silent return would reproduce the dead click through a new route. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 20 | +import React from 'react'; |
| 21 | +import { renderHook, act } from '@testing-library/react'; |
| 22 | + |
| 23 | +const execute = vi.fn(); |
| 24 | +const getItem = vi.fn(); |
| 25 | +const toastError = vi.fn(); |
| 26 | + |
| 27 | +vi.mock('sonner', () => ({ toast: { error: (...a: unknown[]) => toastError(...a) } })); |
| 28 | +vi.mock('@object-ui/react', () => ({ useAction: () => ({ execute }) })); |
| 29 | +vi.mock('../../providers/MetadataProvider', () => ({ useMetadata: () => ({ getItem }) })); |
| 30 | + |
| 31 | +import { useNavActionDispatch } from '../useNavActionDispatch'; |
| 32 | + |
| 33 | +const navItem = (over: Record<string, unknown> = {}) => ({ |
| 34 | + id: 'nav_export', |
| 35 | + type: 'action', |
| 36 | + label: 'Export Data', |
| 37 | + actionDef: { actionName: 'export_data' }, |
| 38 | + ...over, |
| 39 | +}) as never; |
| 40 | + |
| 41 | +async function dispatch(item: unknown) { |
| 42 | + const { result } = renderHook(() => useNavActionDispatch()); |
| 43 | + await act(async () => { |
| 44 | + result.current(item as never); |
| 45 | + // The handler is fire-and-forget by design (the renderer's onClick is |
| 46 | + // synchronous); let its promise chain settle. |
| 47 | + await Promise.resolve(); |
| 48 | + await Promise.resolve(); |
| 49 | + await Promise.resolve(); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +beforeEach(() => { |
| 54 | + execute.mockReset().mockResolvedValue(undefined); |
| 55 | + getItem.mockReset(); |
| 56 | + toastError.mockReset(); |
| 57 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('useNavActionDispatch', () => { |
| 61 | + it('resolves the action by name and dispatches the resolved definition', async () => { |
| 62 | + getItem.mockResolvedValue({ name: 'export_data', type: 'api', target: '/exports', label: 'Export' }); |
| 63 | + |
| 64 | + await dispatch(navItem()); |
| 65 | + |
| 66 | + expect(getItem).toHaveBeenCalledWith('action', 'export_data'); |
| 67 | + expect(execute).toHaveBeenCalledTimes(1); |
| 68 | + const [dispatched] = execute.mock.calls[0]; |
| 69 | + // The whole def rides the dispatch — the runner reads type/target/label. |
| 70 | + expect(dispatched).toMatchObject({ name: 'export_data', type: 'api', target: '/exports' }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("moves a declared `params` ARRAY to `actionParams` (the runner's dialog input)", async () => { |
| 74 | + getItem.mockResolvedValue({ |
| 75 | + name: 'export_data', |
| 76 | + type: 'api', |
| 77 | + params: [{ name: 'format', type: 'text' }], |
| 78 | + }); |
| 79 | + |
| 80 | + await dispatch(navItem()); |
| 81 | + |
| 82 | + const [dispatched] = execute.mock.calls[0]; |
| 83 | + expect(dispatched.actionParams).toEqual([{ name: 'format', type: 'text' }]); |
| 84 | + // `params` on the dispatch is the VALUE bag, not the param declarations — |
| 85 | + // leaving the array there would make the runner treat it as values. |
| 86 | + expect(Array.isArray(dispatched.params)).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it("passes the nav item's own actionDef.params through as the value bag", async () => { |
| 90 | + getItem.mockResolvedValue({ name: 'export_data', type: 'api' }); |
| 91 | + |
| 92 | + await dispatch(navItem({ actionDef: { actionName: 'export_data', params: { format: 'csv' } } })); |
| 93 | + |
| 94 | + const [dispatched] = execute.mock.calls[0]; |
| 95 | + expect(dispatched.params).toEqual({ format: 'csv' }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('warns and toasts — without dispatching — when the action is not defined', async () => { |
| 99 | + getItem.mockResolvedValue(null); |
| 100 | + |
| 101 | + await dispatch(navItem()); |
| 102 | + |
| 103 | + expect(execute).not.toHaveBeenCalled(); |
| 104 | + expect(toastError).toHaveBeenCalledTimes(1); |
| 105 | + expect(String(toastError.mock.calls[0][0])).toContain('export_data'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('warns and toasts when the nav item names no action at all', async () => { |
| 109 | + await dispatch(navItem({ actionDef: undefined })); |
| 110 | + |
| 111 | + expect(getItem).not.toHaveBeenCalled(); |
| 112 | + expect(execute).not.toHaveBeenCalled(); |
| 113 | + expect(toastError).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it('surfaces a resolution failure instead of swallowing it', async () => { |
| 117 | + getItem.mockRejectedValue(new Error('offline')); |
| 118 | + |
| 119 | + await dispatch(navItem()); |
| 120 | + |
| 121 | + expect(execute).not.toHaveBeenCalled(); |
| 122 | + expect(toastError).toHaveBeenCalledTimes(1); |
| 123 | + }); |
| 124 | + |
| 125 | + it('surfaces a failure thrown by the action itself', async () => { |
| 126 | + getItem.mockResolvedValue({ name: 'export_data', type: 'api' }); |
| 127 | + execute.mockRejectedValue(new Error('boom')); |
| 128 | + |
| 129 | + await dispatch(navItem()); |
| 130 | + |
| 131 | + expect(toastError).toHaveBeenCalledTimes(1); |
| 132 | + }); |
| 133 | +}); |
0 commit comments