|
| 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 | + * objectstack#25898 — the list empty state tells "no records you can see" and |
| 11 | + * "nothing of yours yet" apart from "no records". |
| 12 | + * |
| 13 | + * Measured on the console: a sales rep who may read none of 38 cases opened |
| 14 | + * the case list and was told "Nothing here yet / Create your first record to |
| 15 | + * get started." beside no New button; and every "my …" view (`owner` equals |
| 16 | + * the current user) told an owner of nothing "No records match this view's |
| 17 | + * filter." |
| 18 | + * |
| 19 | + * Three cases, chosen by what the list knows: |
| 20 | + * - no filter, and the caller holds the create grant: today's first-run copy |
| 21 | + * and the add-record action; |
| 22 | + * - no filter, and the caller is KNOWN not to hold it (the `listNew` row of the |
| 23 | + * affordance-to-grant map, once permissions have loaded): "No records you can |
| 24 | + * see", and no add-record action even when the view enables one; |
| 25 | + * - the view's declared filter names the signed-in user: "Nothing of yours |
| 26 | + * yet", with the action only under the create grant. |
| 27 | + * |
| 28 | + * SUITE DIRECTION, predicted before running: the read-only and "my view" cases |
| 29 | + * are RED against the base commit (first-run copy / view-filter copy, and the |
| 30 | + * action drawn for a read-only caller); the create-grant, no-provider and |
| 31 | + * plain-filter cases are green in both worlds and pin that the fix leaves them |
| 32 | + * alone. |
| 33 | + */ |
| 34 | + |
| 35 | +import * as React from 'react'; |
| 36 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 37 | +import { render, waitFor, cleanup } from '@testing-library/react'; |
| 38 | +import { ListView } from '../ListView'; |
| 39 | +import { filterNamesCurrentUser } from '../ListEmptyState'; |
| 40 | +import { SchemaRendererProvider, FilterScopeProvider } from '@object-ui/react'; |
| 41 | +import { MePermissionsProvider, type MePermissionsResponse } from '@object-ui/permissions'; |
| 42 | +import type { ListViewSchema } from '@object-ui/types'; |
| 43 | + |
| 44 | +afterEach(cleanup); |
| 45 | + |
| 46 | +const OBJECT = 'crm_case'; |
| 47 | +const USER = 'u-rep'; |
| 48 | + |
| 49 | +function emptyDataSource() { |
| 50 | + return { |
| 51 | + find: vi.fn().mockResolvedValue([]), |
| 52 | + findOne: vi.fn(), |
| 53 | + create: vi.fn(), |
| 54 | + update: vi.fn(), |
| 55 | + delete: vi.fn(), |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +const READ_ONLY = { allowCreate: false, allowRead: true, allowEdit: false, allowDelete: false }; |
| 60 | +const FULL = { allowCreate: true, allowRead: true, allowEdit: true, allowDelete: true }; |
| 61 | + |
| 62 | +const envelope = (bits: Record<string, boolean>): MePermissionsResponse => ({ |
| 63 | + authenticated: true, |
| 64 | + userId: USER, |
| 65 | + tenantId: null, |
| 66 | + roles: ['member'], |
| 67 | + permissionSets: ['member'], |
| 68 | + objects: { [OBJECT]: bits }, |
| 69 | + fields: {}, |
| 70 | +}); |
| 71 | + |
| 72 | +/** A `list-view` node as an author writes it (FilterNode-array filters). */ |
| 73 | +const authored = (node: Record<string, unknown>): ListViewSchema => |
| 74 | + ({ type: 'list-view', objectName: OBJECT, fields: ['name'], addRecord: { enabled: true }, ...node }) as unknown as ListViewSchema; |
| 75 | + |
| 76 | +async function emptyState( |
| 77 | + schema: ListViewSchema, |
| 78 | + { bits, currentUserId }: { bits?: Record<string, boolean>; currentUserId?: string } = {}, |
| 79 | +): Promise<HTMLElement> { |
| 80 | + const ds = emptyDataSource(); |
| 81 | + let tree: React.ReactNode = ( |
| 82 | + <SchemaRendererProvider dataSource={ds as any}> |
| 83 | + <ListView schema={schema} dataSource={ds as any} onAddRecord={() => {}} /> |
| 84 | + </SchemaRendererProvider> |
| 85 | + ); |
| 86 | + if (currentUserId) tree = <FilterScopeProvider currentUserId={currentUserId}>{tree}</FilterScopeProvider>; |
| 87 | + if (bits) tree = <MePermissionsProvider initialPermissions={envelope(bits)}>{tree}</MePermissionsProvider>; |
| 88 | + const { container } = render(<>{tree}</>); |
| 89 | + await waitFor(() => { |
| 90 | + expect(container.querySelector('[data-testid="empty-state"]')).not.toBeNull(); |
| 91 | + }); |
| 92 | + return container.querySelector('[data-testid="empty-state"]') as HTMLElement; |
| 93 | +} |
| 94 | + |
| 95 | +const addAction = (panel: HTMLElement) => panel.querySelector('[data-testid="empty-state-add-record"]'); |
| 96 | + |
| 97 | +describe('ListView empty state — no access vs no records (objectstack#25898)', () => { |
| 98 | + it('a caller without the create grant is told there is nothing they can see, and offered no Create', async () => { |
| 99 | + const panel = await emptyState(authored({}), { bits: READ_ONLY }); |
| 100 | + expect(panel.textContent).toMatch(/No records you can see/); |
| 101 | + expect(panel.textContent).toMatch(/ask your administrator/); |
| 102 | + expect(panel.textContent).not.toMatch(/Nothing here yet/); |
| 103 | + expect(panel.textContent).not.toMatch(/Create your first record/); |
| 104 | + expect(addAction(panel)).toBeNull(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('a caller with the create grant still gets the first-run copy and the action', async () => { |
| 108 | + const panel = await emptyState(authored({}), { bits: FULL }); |
| 109 | + expect(panel.textContent).toMatch(/Nothing here yet/); |
| 110 | + expect(panel.textContent).toMatch(/Create your first record/); |
| 111 | + expect(addAction(panel)).not.toBeNull(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('with no permission provider the copy is unchanged (fail-open)', async () => { |
| 115 | + const panel = await emptyState(authored({})); |
| 116 | + expect(panel.textContent).toMatch(/Nothing here yet/); |
| 117 | + expect(addAction(panel)).not.toBeNull(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('a "my" view — its filter resolved to the caller\'s id — says the caller has none yet', async () => { |
| 121 | + const panel = await emptyState( |
| 122 | + authored({ filter: [['owner', '=', USER]] }), |
| 123 | + { bits: FULL, currentUserId: USER }, |
| 124 | + ); |
| 125 | + expect(panel.textContent).toMatch(/Nothing of yours yet/); |
| 126 | + expect(panel.textContent).not.toMatch(/No records match this view/); |
| 127 | + expect(addAction(panel)).not.toBeNull(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('a "my" view for a caller without the create grant offers no Create', async () => { |
| 131 | + const panel = await emptyState( |
| 132 | + authored({ filter: [['owner', '=', USER]] }), |
| 133 | + { bits: READ_ONLY, currentUserId: USER }, |
| 134 | + ); |
| 135 | + expect(panel.textContent).toMatch(/Nothing of yours yet/); |
| 136 | + expect(addAction(panel)).toBeNull(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('a filter that does not name the caller keeps the view-filter copy', async () => { |
| 140 | + const panel = await emptyState( |
| 141 | + authored({ filter: [['status', '=', 'open']] }), |
| 142 | + { bits: FULL, currentUserId: USER }, |
| 143 | + ); |
| 144 | + expect(panel.textContent).toMatch(/No matching records/); |
| 145 | + expect(panel.textContent).toMatch(/No records match this view/); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('filterNamesCurrentUser (objectstack#25898)', () => { |
| 150 | + it('finds the caller in every filter shape a list receives', () => { |
| 151 | + expect(filterNamesCurrentUser([['owner', '=', USER]], USER)).toBe(true); |
| 152 | + expect(filterNamesCurrentUser([{ field: 'owner', operator: 'equals', value: USER }], USER)).toBe(true); |
| 153 | + expect(filterNamesCurrentUser({ owner: { $eq: USER } }, USER)).toBe(true); |
| 154 | + expect(filterNamesCurrentUser(['and', ['status', '=', 'open'], ['owner', '=', USER]], USER)).toBe(true); |
| 155 | + }); |
| 156 | + |
| 157 | + it('reads the unresolved placeholder in both spellings', () => { |
| 158 | + expect(filterNamesCurrentUser([['owner', '=', '{current_user_id}']], undefined)).toBe(true); |
| 159 | + expect(filterNamesCurrentUser({ owner: '${current_user_id}' }, null)).toBe(true); |
| 160 | + }); |
| 161 | + |
| 162 | + it('answers false for a filter about someone or something else, and with no id to compare', () => { |
| 163 | + expect(filterNamesCurrentUser([['owner', '=', 'u-other']], USER)).toBe(false); |
| 164 | + expect(filterNamesCurrentUser([['status', '=', 'open']], USER)).toBe(false); |
| 165 | + expect(filterNamesCurrentUser([['owner', '=', '']], '')).toBe(false); |
| 166 | + expect(filterNamesCurrentUser(undefined, USER)).toBe(false); |
| 167 | + expect(filterNamesCurrentUser([['note', '=', 'user-{current_user_id}']], USER)).toBe(false); |
| 168 | + }); |
| 169 | +}); |
0 commit comments