|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A lookup whose METADATA carries `dependsOn` scopes its candidates — read |
| 5 | + * through the DECLARED type (objectui#6153, instance 1; maintainer ruling A, |
| 6 | + * 2026-09-02). |
| 7 | + * |
| 8 | + * `LookupField.dependsOn.test.tsx` (#2215) proves the gate and the hard |
| 9 | + * `$filter` through the legacy `depends_on` spelling on an `as any` literal. |
| 10 | + * This file proves the same two facts through the spec's field-level spelling |
| 11 | + * on ANNOTATED `LookupFieldMetadata` literals with NO cast — the |
| 12 | + * excess-property check refused `dependsOn` on this exact document before the |
| 13 | + * declaration landed (compile half), and the widget still gates and scopes on |
| 14 | + * it (runtime half). Both spellings stay readable until objectui#7357 retires |
| 15 | + * the snake_case twin; that card, not this one, drops the arm. |
| 16 | + */ |
| 17 | + |
| 18 | +import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'; |
| 19 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 20 | +import type { LookupFieldMetadata } from '@object-ui/types'; |
| 21 | +import { LookupField } from './LookupField'; |
| 22 | + |
| 23 | +const contacts = [ |
| 24 | + { id: 'c1', name: 'Nora Field', account: 'a1' }, |
| 25 | + { id: 'c2', name: 'Oscar Grant', account: 'a2' }, |
| 26 | +]; |
| 27 | + |
| 28 | +function makeDataSource() { |
| 29 | + const find = vi.fn(async (_obj: string, params: { $filter?: Record<string, unknown> } | undefined) => { |
| 30 | + const account = params?.$filter?.account ?? params?.$filter?.account_id; |
| 31 | + const data = account ? contacts.filter((c) => c.account === account) : contacts; |
| 32 | + return { data, total: data.length }; |
| 33 | + }); |
| 34 | + return { find } as never; |
| 35 | +} |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + // jsdom has no matchMedia; the people-picker branch's useIsMobile needs it. |
| 39 | + Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1280 }); |
| 40 | + window.matchMedia = ((query: string) => ({ |
| 41 | + matches: false, |
| 42 | + media: query, |
| 43 | + onchange: null, |
| 44 | + addListener: () => {}, |
| 45 | + removeListener: () => {}, |
| 46 | + addEventListener: () => {}, |
| 47 | + removeEventListener: () => {}, |
| 48 | + dispatchEvent: () => false, |
| 49 | + })) as never; |
| 50 | +}); |
| 51 | + |
| 52 | +// The literal `content/docs/fields/lookup.mdx` teaches — annotated, uncast. |
| 53 | +const contactField: LookupFieldMetadata = { |
| 54 | + type: 'lookup', |
| 55 | + name: 'contact', |
| 56 | + label: 'Contact', |
| 57 | + reference_to: 'contacts', |
| 58 | + reference_field: 'name', |
| 59 | + dependsOn: [{ field: 'account', param: 'account_id' }], |
| 60 | +}; |
| 61 | + |
| 62 | +const shorthandField: LookupFieldMetadata = { |
| 63 | + ...contactField, |
| 64 | + dependsOn: ['account'], |
| 65 | +}; |
| 66 | + |
| 67 | +// `dependentValues` is a HOST prop (the form renderer's channel), not metadata. |
| 68 | +const host = (dependentValues: Record<string, unknown>) => ({ dependentValues }) as Record<string, unknown>; |
| 69 | + |
| 70 | +describe('LookupField — `dependsOn` off the DECLARED metadata type (objectui#6153)', () => { |
| 71 | + it('gates the trigger while the controlling field is empty', () => { |
| 72 | + render( |
| 73 | + <LookupField |
| 74 | + field={contactField} |
| 75 | + value={undefined} |
| 76 | + onChange={vi.fn()} |
| 77 | + readonly={false} |
| 78 | + dataSource={makeDataSource()} |
| 79 | + {...host({ account: null })} |
| 80 | + />, |
| 81 | + ); |
| 82 | + const trigger = screen.getByTestId('lookup-trigger-gated'); |
| 83 | + expect(trigger).toBeDisabled(); |
| 84 | + expect(trigger).toHaveTextContent(/select account first/i); |
| 85 | + }); |
| 86 | + |
| 87 | + it('scopes the candidate query by the `{ field, param }` entry once the parent is set', async () => { |
| 88 | + const ds = makeDataSource(); |
| 89 | + render( |
| 90 | + <LookupField |
| 91 | + field={contactField} |
| 92 | + value={undefined} |
| 93 | + onChange={vi.fn()} |
| 94 | + readonly={false} |
| 95 | + dataSource={ds} |
| 96 | + {...host({ account: 'a1' })} |
| 97 | + />, |
| 98 | + ); |
| 99 | + |
| 100 | + const trigger = screen.getByRole('button', { name: /select/i }); |
| 101 | + expect(trigger).not.toBeDisabled(); |
| 102 | + await act(async () => { |
| 103 | + fireEvent.click(trigger); |
| 104 | + }); |
| 105 | + |
| 106 | + await waitFor(() => { |
| 107 | + expect((ds as { find: ReturnType<typeof vi.fn> }).find).toHaveBeenCalledWith( |
| 108 | + 'contacts', |
| 109 | + expect.objectContaining({ $filter: expect.objectContaining({ account_id: 'a1' }) }), |
| 110 | + ); |
| 111 | + }); |
| 112 | + await waitFor(() => { |
| 113 | + expect(screen.getByText('Nora Field')).toBeInTheDocument(); |
| 114 | + expect(screen.queryByText('Oscar Grant')).not.toBeInTheDocument(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('the shorthand `[name]` entry filters by the sibling name itself', async () => { |
| 119 | + const ds = makeDataSource(); |
| 120 | + render( |
| 121 | + <LookupField |
| 122 | + field={shorthandField} |
| 123 | + value={undefined} |
| 124 | + onChange={vi.fn()} |
| 125 | + readonly={false} |
| 126 | + dataSource={ds} |
| 127 | + {...host({ account: 'a1' })} |
| 128 | + />, |
| 129 | + ); |
| 130 | + await act(async () => { |
| 131 | + fireEvent.click(screen.getByRole('button', { name: /select/i })); |
| 132 | + }); |
| 133 | + await waitFor(() => { |
| 134 | + expect((ds as { find: ReturnType<typeof vi.fn> }).find).toHaveBeenCalledWith( |
| 135 | + 'contacts', |
| 136 | + expect.objectContaining({ $filter: expect.objectContaining({ account: 'a1' }) }), |
| 137 | + ); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments