|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * The discussion header shows no count when a feed read was refused |
| 6 | + * (objectstack#27820). |
| 7 | + * |
| 8 | + * The timeline's heading drew `(N)` from the rows it holds. A refused comment |
| 9 | + * read leaves no rows, so a member who may not read the comments saw |
| 10 | + * `Discussion (0)` right above "You don't have permission to view comments on |
| 11 | + * this record." — a count the server never gave. The host's refusal signal |
| 12 | + * (`commentsDenied` / `activityDenied`, the same one that draws the refusal |
| 13 | + * line) now withholds the count; a readable feed keeps it, `(0)` included. |
| 14 | + * |
| 15 | + * Read through the real `en` pack and the `record:chatter` block under a host |
| 16 | + * `DiscussionContext`, the way the record page mounts it. |
| 17 | + */ |
| 18 | + |
| 19 | +import * as React from 'react'; |
| 20 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 21 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 22 | +import { I18nProvider } from '@object-ui/i18n'; |
| 23 | +import { DiscussionContextProvider, RecordContextProvider } from '@object-ui/react'; |
| 24 | +import type { FeedItem } from '@object-ui/types'; |
| 25 | +import { RecordChatterRenderer } from '../renderers/record-chatter'; |
| 26 | +import { RecordChatterPanel } from '../RecordChatterPanel'; |
| 27 | + |
| 28 | +const COMMENTS_DENIED = "You don't have permission to view comments on this record."; |
| 29 | +const ACTIVITY_DENIED = "You don't have permission to view activity on this record."; |
| 30 | + |
| 31 | +const comment = (n: number) => |
| 32 | + ({ |
| 33 | + id: `c-${n}`, |
| 34 | + type: 'comment', |
| 35 | + actor: 'Ada', |
| 36 | + body: `Comment ${n}`, |
| 37 | + createdAt: `2026-01-0${n}T00:00:00.000Z`, |
| 38 | + }) satisfies FeedItem; |
| 39 | + |
| 40 | +type HostItems = React.ComponentProps<typeof DiscussionContextProvider>['items']; |
| 41 | + |
| 42 | +function mountChatter(ctx: { items: HostItems; commentsDenied?: boolean; activityDenied?: boolean }) { |
| 43 | + return render( |
| 44 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}> |
| 45 | + <RecordContextProvider objectName="crm_account" recordId="rec-1" data={{ id: 'rec-1' }}> |
| 46 | + <DiscussionContextProvider loading={false} {...ctx}> |
| 47 | + <RecordChatterRenderer schema={{} as never} /> |
| 48 | + </DiscussionContextProvider> |
| 49 | + </RecordContextProvider> |
| 50 | + </I18nProvider>, |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +const heading = () => screen.getByRole('heading', { level: 2 }); |
| 55 | + |
| 56 | +afterEach(() => cleanup()); |
| 57 | + |
| 58 | +describe('the discussion header withholds its count when a feed read was refused (objectstack#27820)', () => { |
| 59 | + it('a refused comment read: the header shows no count, the refusal line still shows', () => { |
| 60 | + mountChatter({ items: [], commentsDenied: true }); |
| 61 | + |
| 62 | + expect(heading().textContent?.trim()).toBe('Discussion'); |
| 63 | + expect(heading().textContent).not.toMatch(/\(\d+\)/); |
| 64 | + expect(screen.queryByTestId('activity-count')).toBeNull(); |
| 65 | + expect(screen.getByTestId('activity-access-denied').textContent).toContain(COMMENTS_DENIED); |
| 66 | + }); |
| 67 | + |
| 68 | + it('a refused comment read beside readable activity rows still shows no count', () => { |
| 69 | + const row = { |
| 70 | + id: 'a-1', |
| 71 | + type: 'field_change', |
| 72 | + actor: 'Grace', |
| 73 | + body: 'Stage moved to qualified', |
| 74 | + createdAt: '2026-01-02T00:00:00.000Z', |
| 75 | + } satisfies FeedItem; |
| 76 | + mountChatter({ items: [row], commentsDenied: true }); |
| 77 | + |
| 78 | + expect(screen.getByText('Stage moved to qualified')).toBeTruthy(); |
| 79 | + expect(heading().textContent).not.toMatch(/\(\d+\)/); |
| 80 | + expect(screen.getByTestId('activity-access-denied').textContent).toContain(COMMENTS_DENIED); |
| 81 | + }); |
| 82 | + |
| 83 | + it('a refused activity read shows no count either', () => { |
| 84 | + mountChatter({ items: [], activityDenied: true }); |
| 85 | + |
| 86 | + expect(heading().textContent).not.toMatch(/\(\d+\)/); |
| 87 | + expect(screen.getByTestId('activity-access-denied').textContent).toContain(ACTIVITY_DENIED); |
| 88 | + }); |
| 89 | + |
| 90 | + it('control: a readable record with no comments shows (0)', () => { |
| 91 | + mountChatter({ items: [] }); |
| 92 | + |
| 93 | + expect(heading()).toHaveTextContent(/^Discussion\s*\(0\)$/); |
| 94 | + expect(screen.queryByTestId('activity-access-denied')).toBeNull(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('control: a readable record with 3 comments shows (3)', () => { |
| 98 | + mountChatter({ items: [comment(1), comment(2), comment(3)] }); |
| 99 | + |
| 100 | + expect(heading()).toHaveTextContent(/^Discussion\s*\(3\)$/); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('the collapsed discussion toggle withholds its count the same way (objectstack#27820)', () => { |
| 105 | + const mountCollapsed = (props: { commentsDenied?: boolean }) => |
| 106 | + render( |
| 107 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}> |
| 108 | + <RecordChatterPanel |
| 109 | + items={[]} |
| 110 | + config={{ position: 'bottom', collapsible: true, defaultCollapsed: true } as never} |
| 111 | + {...props} |
| 112 | + /> |
| 113 | + </I18nProvider>, |
| 114 | + ); |
| 115 | + |
| 116 | + it('refused: the toggle names the panel without a count', () => { |
| 117 | + mountCollapsed({ commentsDenied: true }); |
| 118 | + |
| 119 | + const toggle = screen.getByRole('button', { name: 'Open discussion panel' }); |
| 120 | + expect(toggle.textContent).not.toMatch(/\(\d+\)/); |
| 121 | + }); |
| 122 | + |
| 123 | + it('control: readable, the toggle keeps its (0)', () => { |
| 124 | + mountCollapsed({}); |
| 125 | + |
| 126 | + expect(screen.getByRole('button', { name: 'Show Discussion (0)' })).toBeTruthy(); |
| 127 | + }); |
| 128 | +}); |
0 commit comments