|
| 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 | + * `position-ref` is a grant picker, and a grant cannot name an audience anchor |
| 11 | + * (objectui#12200). `everyone` and `guest` (ADR-0090 D5/D9) are held |
| 12 | + * implicitly by every authenticated or anonymous principal, the server refuses |
| 13 | + * a `sys_user_position` row that names one, and yet the position registry |
| 14 | + * lists both beside the assignable positions. These pins hold: |
| 15 | + * |
| 16 | + * - the picker leaves out exactly the spec's `AUDIENCE_ANCHOR_POSITIONS`; |
| 17 | + * - a registry that lists nothing BUT anchors offers no picker; |
| 18 | + * - a stored anchor still shows, by its label beside its name, unflagged, and |
| 19 | + * is not offered when the row holds another value; |
| 20 | + * - `permission-ref` filters nothing: a permission set named like an anchor is |
| 21 | + * an ordinary set. |
| 22 | + */ |
| 23 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 24 | +import React from 'react'; |
| 25 | +import { render, screen, waitFor, fireEvent, cleanup } from '@testing-library/react'; |
| 26 | +import '@testing-library/jest-dom'; |
| 27 | +import { AUDIENCE_ANCHOR_POSITIONS } from '@objectstack/spec'; |
| 28 | +import { PositionRefField, PermissionRefField } from '../RegistryRefField'; |
| 29 | + |
| 30 | +/** The assignable positions, by label beside name, as the picker reads them. */ |
| 31 | +const ASSIGNABLE = ['auditor', 'Sales Manager (sales_manager)', 'Sales Representative (sales_rep)']; |
| 32 | + |
| 33 | +const POSITIONS = [ |
| 34 | + { name: 'sales_rep', label: 'Sales Representative' }, |
| 35 | + { name: 'sales_manager', label: 'Sales Manager' }, |
| 36 | + { name: 'auditor' }, |
| 37 | + // The anchors, as the registry serves them: listed beside the positions. |
| 38 | + { name: 'everyone', label: 'Everyone' }, |
| 39 | + { name: 'guest', label: 'Guest' }, |
| 40 | +]; |
| 41 | + |
| 42 | +function lister(items: Array<{ name: string; label?: string }>) { |
| 43 | + return { listMetadataItems: vi.fn(async () => items), find: vi.fn() } as any; |
| 44 | +} |
| 45 | + |
| 46 | +function mount( |
| 47 | + Widget: typeof PositionRefField, |
| 48 | + { dataSource, value = '', readonly = false }: { dataSource: unknown; value?: string; readonly?: boolean }, |
| 49 | +) { |
| 50 | + const onChange = vi.fn(); |
| 51 | + render( |
| 52 | + <Widget |
| 53 | + value={value} |
| 54 | + onChange={onChange} |
| 55 | + readonly={readonly} |
| 56 | + field={{ name: 'position', type: 'text', label: 'Position' } as any} |
| 57 | + dataSource={dataSource} |
| 58 | + id="f-position" |
| 59 | + />, |
| 60 | + ); |
| 61 | + return { onChange }; |
| 62 | +} |
| 63 | + |
| 64 | +/** The option texts of the open picker, in order. */ |
| 65 | +async function openOptions(): Promise<string[]> { |
| 66 | + fireEvent.click(await screen.findByRole('combobox')); |
| 67 | + await screen.findAllByRole('option'); |
| 68 | + return screen.getAllByRole('option').map((option) => option.textContent ?? ''); |
| 69 | +} |
| 70 | + |
| 71 | +afterEach(() => cleanup()); |
| 72 | + |
| 73 | +describe('position-ref leaves the audience anchors out of what it offers (objectui#12200)', () => { |
| 74 | + it('offers the assignable positions and no anchor', async () => { |
| 75 | + // The names the card asks for are the ones the spec publishes. |
| 76 | + expect([...AUDIENCE_ANCHOR_POSITIONS].sort()).toEqual(['everyone', 'guest']); |
| 77 | + |
| 78 | + mount(PositionRefField, { dataSource: lister(POSITIONS) }); |
| 79 | + |
| 80 | + expect(await openOptions()).toEqual(ASSIGNABLE); |
| 81 | + for (const anchor of AUDIENCE_ANCHOR_POSITIONS) { |
| 82 | + expect(screen.queryByText(new RegExp(`\\(${anchor}\\)$`))).toBeNull(); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('a registry that lists only the anchors offers no picker: the typed-name box', async () => { |
| 87 | + const ds = lister(POSITIONS.slice(3)); |
| 88 | + mount(PositionRefField, { dataSource: ds }); |
| 89 | + |
| 90 | + await waitFor(() => expect(ds.listMetadataItems).toHaveBeenCalledWith('position')); |
| 91 | + const box = await screen.findByRole('textbox'); |
| 92 | + await waitFor(() => expect(box).toBeEnabled()); |
| 93 | + expect(screen.queryByRole('combobox')).toBeNull(); |
| 94 | + expect(screen.queryByTestId('registry-ref-position-load-failed')).toBeNull(); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('a stored anchor still shows, and is not offered as a fresh choice (objectui#12200)', () => { |
| 99 | + it('shows a stored `everyone` by its label beside its name, unflagged, and writes nothing', async () => { |
| 100 | + const { onChange } = mount(PositionRefField, { dataSource: lister(POSITIONS), value: 'everyone' }); |
| 101 | + |
| 102 | + const trigger = await screen.findByRole('combobox'); |
| 103 | + expect(trigger).toHaveTextContent('Everyone (everyone)'); |
| 104 | + // The registry lists it, so it is not "not found". |
| 105 | + expect(trigger).not.toHaveAttribute('data-not-found'); |
| 106 | + expect(onChange).not.toHaveBeenCalled(); |
| 107 | + |
| 108 | + // Its own option, as the row's value; the other anchor is not offered. |
| 109 | + expect(await openOptions()).toEqual(['Everyone (everyone)', ...ASSIGNABLE]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('read-only: a stored anchor reads as its label beside its name', async () => { |
| 113 | + mount(PositionRefField, { dataSource: lister(POSITIONS), value: 'guest', readonly: true }); |
| 114 | + |
| 115 | + const shown = await screen.findByText('Guest (guest)'); |
| 116 | + expect(shown).not.toHaveAttribute('data-not-found'); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('permission-ref filters nothing (objectui#12200)', () => { |
| 121 | + it('lists, and writes, a permission set named like an anchor', async () => { |
| 122 | + const sets = lister([ |
| 123 | + { name: 'everyone', label: 'Everyone Baseline' }, |
| 124 | + { name: 'guest' }, |
| 125 | + { name: 'sales_user', label: 'Sales User' }, |
| 126 | + ]); |
| 127 | + const { onChange } = mount(PermissionRefField, { dataSource: sets }); |
| 128 | + |
| 129 | + expect(await openOptions()).toEqual(['Everyone Baseline (everyone)', 'guest', 'Sales User (sales_user)']); |
| 130 | + fireEvent.click(screen.getByText('Everyone Baseline (everyone)')); |
| 131 | + expect(onChange).toHaveBeenCalledWith('everyone'); |
| 132 | + }); |
| 133 | +}); |
0 commit comments