|
| 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 | + * objectstack#37270 — the stage menu's dialog: a row a refusal names reads the |
| 9 | + * record the move would leave, also when no collected row is shown. A move to |
| 10 | + * a stage that needs nothing opens the dialog with no fields; the server then |
| 11 | + * refuses it naming a radio whose options read `record.stage`. The row it adds |
| 12 | + * offers the options visible on the moved record, not the empty state. |
| 13 | + * |
| 14 | + * `@object-ui/*` resolves to source through the root vitest alias table. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as React from 'react'; |
| 18 | +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; |
| 19 | +import { render, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react'; |
| 20 | +import '@testing-library/jest-dom'; |
| 21 | +import { InlineEditProvider, RecordContextProvider } from '@object-ui/react'; |
| 22 | + |
| 23 | +const { permsStub } = vi.hoisted(() => ({ |
| 24 | + permsStub: { |
| 25 | + isLoaded: true, |
| 26 | + checkField: () => true, |
| 27 | + check: () => ({ allowed: true }), |
| 28 | + getFieldPermissions: () => [], |
| 29 | + getRowFilter: () => undefined, |
| 30 | + getObjectApiOperations: () => undefined, |
| 31 | + roles: [], |
| 32 | + userId: null, |
| 33 | + systemPermissions: undefined, |
| 34 | + hasCapabilities: () => true, |
| 35 | + can: () => true, |
| 36 | + cannot: () => false, |
| 37 | + }, |
| 38 | +})); |
| 39 | + |
| 40 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 41 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 42 | + return { ...actual, usePermissions: () => permsStub as any }; |
| 43 | +}); |
| 44 | + |
| 45 | +import { RecordStageMenu } from '../RecordStageMenu'; |
| 46 | + |
| 47 | +const OPPORTUNITY = { |
| 48 | + name: 'crm_opportunity', |
| 49 | + fields: { |
| 50 | + name: { type: 'text', label: 'Name' }, |
| 51 | + stage: { |
| 52 | + type: 'select', |
| 53 | + label: 'Stage', |
| 54 | + options: [ |
| 55 | + { value: 'prospecting', label: 'Prospecting' }, |
| 56 | + { value: 'negotiation', label: 'Negotiation' }, |
| 57 | + { value: 'closed_lost', label: 'Closed Lost' }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + // No `requiredWhen`: the client cannot foresee it, only the server names it. |
| 61 | + channel: { |
| 62 | + type: 'radio', |
| 63 | + label: 'Channel', |
| 64 | + options: [ |
| 65 | + { value: 'negotiation_option', label: 'Negotiation option', visibleWhen: 'record.stage == "negotiation"' }, |
| 66 | + { value: 'lost_option', label: 'Lost option', visibleWhen: 'record.stage == "closed_lost"' }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + }, |
| 70 | +}; |
| 71 | + |
| 72 | +beforeEach(() => { |
| 73 | + vi.stubGlobal('fetch', vi.fn(async () => ({ ok: false, status: 501, json: async () => ({}) }))); |
| 74 | +}); |
| 75 | + |
| 76 | +afterEach(() => { |
| 77 | + cleanup(); |
| 78 | + vi.unstubAllGlobals(); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('#37270 a refusal-named row in the stage dialog reads the moved record', () => { |
| 82 | + it('with no collected row shown, the added row offers the options visible on the record the move leaves', async () => { |
| 83 | + const refusal = Object.assign(new Error('VALIDATION_FAILED'), { |
| 84 | + code: 'VALIDATION_FAILED', |
| 85 | + fields: [{ field: 'channel', code: 'required', message: 'Channel is required to negotiate' }], |
| 86 | + }); |
| 87 | + const dataSource = { update: vi.fn(async () => { throw refusal; }) }; |
| 88 | + render( |
| 89 | + <RecordContextProvider |
| 90 | + objectName="crm_opportunity" |
| 91 | + recordId="o1" |
| 92 | + data={{ id: 'o1', name: 'Wayne deal', stage: 'prospecting', channel: null, updated_at: 'v1' }} |
| 93 | + objectSchema={OPPORTUNITY} |
| 94 | + dataSource={dataSource as any} |
| 95 | + reread={vi.fn(async () => {})} |
| 96 | + > |
| 97 | + <InlineEditProvider canEdit> |
| 98 | + <RecordStageMenu field="stage" label="Prospecting" tag={<span>Prospecting</span>} /> |
| 99 | + </InlineEditProvider> |
| 100 | + </RecordContextProvider>, |
| 101 | + ); |
| 102 | + fireEvent.pointerDown(screen.getByRole('button', { name: /^Stage: / }), { button: 0, ctrlKey: false, pointerType: 'mouse' }); |
| 103 | + const menu = screen.getByRole('menu'); |
| 104 | + fireEvent.click(within(menu).getAllByRole('menuitem').find((el) => el.textContent?.startsWith('Negotiation'))!); |
| 105 | + let dialog = await screen.findByRole('dialog'); |
| 106 | + expect(dialog.querySelector('[data-required-fields-field]')).toBeNull(); |
| 107 | + fireEvent.click(within(dialog).getByRole('button', { name: 'Move' })); |
| 108 | + await waitFor(() => expect(dataSource.update).toHaveBeenCalledTimes(1)); |
| 109 | + dialog = await screen.findByRole('dialog'); |
| 110 | + const row = await waitFor(() => { |
| 111 | + const el = dialog.querySelector('[data-required-fields-field="channel"]') as HTMLElement | null; |
| 112 | + expect(el).not.toBeNull(); |
| 113 | + return el as HTMLElement; |
| 114 | + }); |
| 115 | + expect(row.querySelector('[data-testid="radio-empty-channel"]')).toBeNull(); |
| 116 | + expect(within(row).getByText('Negotiation option')).toBeInTheDocument(); |
| 117 | + // Judged on the moved record, not opened up. |
| 118 | + expect(within(row).queryByText('Lost option')).toBeNull(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments