|
| 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#25876 — a collapsed navigation group shows how many entries it |
| 11 | + * holds. |
| 12 | + * |
| 13 | + * Before: a closed group drew its label and a chevron and nothing else, so a |
| 14 | + * closed group holding one entry looked exactly like one holding ten, and new |
| 15 | + * users never opened it. Now, while the group is collapsed, a count badge sits |
| 16 | + * beside the label. |
| 17 | + * |
| 18 | + * The count is what opening the group would reveal: the entries that pass the |
| 19 | + * same per-item guards the renderer applies (`visible`, `requiredPermissions`, |
| 20 | + * the capability gates, an `action` needing a dispatcher), recursing into |
| 21 | + * nested groups. Separators and the groups themselves are not entries. The |
| 22 | + * badge is `aria-hidden`, so the disclosure's accessible name stays the label |
| 23 | + * alone (the disclosure already announces collapsed / expanded). |
| 24 | + */ |
| 25 | + |
| 26 | +import '@testing-library/jest-dom/vitest'; |
| 27 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 28 | +import React from 'react'; |
| 29 | +import { cleanup, render, screen, within } from '@testing-library/react'; |
| 30 | +import userEvent from '@testing-library/user-event'; |
| 31 | +import { MemoryRouter } from 'react-router-dom'; |
| 32 | +import type { NavigationItem } from '@object-ui/types'; |
| 33 | +import { Sidebar, SidebarContent, SidebarProvider } from '@object-ui/components'; |
| 34 | +import { NavigationRenderer } from '../NavigationRenderer'; |
| 35 | + |
| 36 | +const BASE = '/apps/crm'; |
| 37 | + |
| 38 | +const entry = (id: string, extra: Partial<NavigationItem> = {}): NavigationItem => |
| 39 | + ({ id, type: 'object', objectName: id, label: id.toUpperCase(), ...extra }) as NavigationItem; |
| 40 | + |
| 41 | +/** |
| 42 | + * Seven nodes, four of them reachable: two plain entries and the two inside |
| 43 | + * the nested group. Not counted: an entry hidden by its `visible` expression, |
| 44 | + * an entry whose permission is denied, a separator, an `action` with no |
| 45 | + * dispatcher, a nested entry hidden by `visible`, and the nested group itself. |
| 46 | + */ |
| 47 | +const children = (): NavigationItem[] => [ |
| 48 | + entry('nav_campaigns'), |
| 49 | + entry('nav_leads'), |
| 50 | + entry('nav_hidden', { visible: 'hide' } as Partial<NavigationItem>), |
| 51 | + entry('nav_denied', { requiredPermissions: ['marketing.admin'] } as Partial<NavigationItem>), |
| 52 | + { id: 'sep_1', type: 'separator' } as NavigationItem, |
| 53 | + { id: 'act_import', type: 'action', label: 'Import', actionDef: { actionName: 'import' } } as unknown as NavigationItem, |
| 54 | + { |
| 55 | + id: 'grp_nested', |
| 56 | + type: 'group', |
| 57 | + label: 'Forecasts', |
| 58 | + children: [entry('nav_forecast'), entry('nav_quota'), entry('nav_nested_hidden', { visible: 'hide' } as Partial<NavigationItem>)], |
| 59 | + } as NavigationItem, |
| 60 | +]; |
| 61 | + |
| 62 | +const group = (id: string, label: string, extra: Partial<NavigationItem> = {}): NavigationItem => |
| 63 | + ({ id, type: 'group', label, children: children(), ...extra }) as NavigationItem; |
| 64 | + |
| 65 | +const evaluateVisibility = (expr: unknown) => expr !== 'hide'; |
| 66 | +const checkPermission = (perms: string[]) => !perms.includes('marketing.admin'); |
| 67 | + |
| 68 | +function renderNav(items: NavigationItem[], path = BASE) { |
| 69 | + return render( |
| 70 | + <MemoryRouter initialEntries={[path]}> |
| 71 | + <SidebarProvider defaultOpen> |
| 72 | + <NavigationRenderer |
| 73 | + items={items} |
| 74 | + basePath={BASE} |
| 75 | + evaluateVisibility={evaluateVisibility} |
| 76 | + checkPermission={checkPermission} |
| 77 | + /> |
| 78 | + </SidebarProvider> |
| 79 | + </MemoryRouter>, |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +const disclosure = (label: string) => screen.getByRole('button', { name: label }); |
| 84 | +/** Any count drawn inside a disclosure: a run of digits as an element's whole text. */ |
| 85 | +const countIn = (button: HTMLElement) => within(button).queryByText(/^\d+$/); |
| 86 | + |
| 87 | +afterEach(cleanup); |
| 88 | + |
| 89 | +describe('NavigationRenderer — a collapsed group shows its reachable child count (objectstack#25876)', () => { |
| 90 | + it('a collapsed group shows the guarded count: hidden, denied, separator, dispatcher-less action and the nested group itself are not counted; nested entries are', () => { |
| 91 | + renderNav([group('grp_marketing', 'Marketing', { expanded: false })]); |
| 92 | + const button = disclosure('Marketing'); |
| 93 | + expect(button).toHaveAttribute('aria-expanded', 'false'); |
| 94 | + |
| 95 | + const count = countIn(button); |
| 96 | + expect(count, 'a collapsed group draws a count').not.toBeNull(); |
| 97 | + expect(count).toHaveTextContent(/^4$/); |
| 98 | + expect(count).toHaveAttribute('aria-hidden', 'true'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('the count is the badge, not part of the disclosure\'s accessible name', () => { |
| 102 | + renderNav([group('grp_marketing', 'Marketing', { expanded: false })]); |
| 103 | + // Exact name: the label alone, the badge's digits are not read into it. |
| 104 | + expect(screen.getByRole('button', { name: 'Marketing' })).toBeInTheDocument(); |
| 105 | + expect(screen.queryByRole('button', { name: /4/ })).not.toBeInTheDocument(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('an expanded group shows no count; collapsing it shows the count, expanding hides it again', async () => { |
| 109 | + const user = userEvent.setup(); |
| 110 | + renderNav([group('grp_marketing', 'Marketing')]); |
| 111 | + const button = disclosure('Marketing'); |
| 112 | + expect(button).toHaveAttribute('aria-expanded', 'true'); |
| 113 | + expect(countIn(button)).toBeNull(); |
| 114 | + |
| 115 | + await user.click(button); |
| 116 | + expect(button).toHaveAttribute('aria-expanded', 'false'); |
| 117 | + expect(countIn(button)).toHaveTextContent(/^4$/); |
| 118 | + |
| 119 | + await user.click(button); |
| 120 | + expect(button).toHaveAttribute('aria-expanded', 'true'); |
| 121 | + expect(countIn(button)).toBeNull(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('a nested group collapsed inside an open one shows its own count', async () => { |
| 125 | + const user = userEvent.setup(); |
| 126 | + renderNav([group('grp_marketing', 'Marketing')]); |
| 127 | + const nested = disclosure('Forecasts'); |
| 128 | + expect(countIn(nested)).toBeNull(); |
| 129 | + await user.click(nested); |
| 130 | + expect(countIn(nested)).toHaveTextContent(/^2$/); |
| 131 | + }); |
| 132 | + |
| 133 | + it('a group declared closed that holds the current route is forced open, so it shows no count', () => { |
| 134 | + renderNav([group('grp_marketing', 'Marketing', { expanded: false })], `${BASE}/nav_leads`); |
| 135 | + const button = disclosure('Marketing'); |
| 136 | + expect(button).toHaveAttribute('aria-expanded', 'true'); |
| 137 | + expect(countIn(button)).toBeNull(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('collapsed to icons: the count carries the very icon-mode hiding classes the group label carries', () => { |
| 141 | + const { container } = render( |
| 142 | + <MemoryRouter initialEntries={[BASE]}> |
| 143 | + <SidebarProvider defaultOpen={false}> |
| 144 | + <Sidebar collapsible="icon"> |
| 145 | + <SidebarContent> |
| 146 | + <NavigationRenderer |
| 147 | + items={[group('grp_marketing', 'Marketing', { expanded: false })]} |
| 148 | + basePath={BASE} |
| 149 | + evaluateVisibility={evaluateVisibility} |
| 150 | + checkPermission={checkPermission} |
| 151 | + /> |
| 152 | + </SidebarContent> |
| 153 | + </Sidebar> |
| 154 | + </SidebarProvider> |
| 155 | + </MemoryRouter>, |
| 156 | + ); |
| 157 | + const peer = container.querySelector('.group.peer[data-state]'); |
| 158 | + expect(peer).toHaveAttribute('data-state', 'collapsed'); |
| 159 | + expect(peer).toHaveAttribute('data-collapsible', 'icon'); |
| 160 | + |
| 161 | + const label = disclosure('Marketing'); |
| 162 | + const tokens = (el: Element) => el.className.split(/\s+/).filter(Boolean); |
| 163 | + // The tokens this file adds to the label to hide it in icon mode (pointer, |
| 164 | + // focus / accessibility tree, visibility transition), read off the label. |
| 165 | + // The primitive's own `-mt-8` / `opacity-0` move and fade the label's box, |
| 166 | + // which already carries the count; they are the primitive's, not the file's. |
| 167 | + const prefix = 'group-data-[state=collapsed]:group-data-[collapsible=icon]:'; |
| 168 | + const hiding = tokens(label).filter( |
| 169 | + (t) => t.startsWith(prefix) && t !== `${prefix}-mt-8` && t !== `${prefix}opacity-0`, |
| 170 | + ); |
| 171 | + expect(hiding).toHaveLength(3); |
| 172 | + expect(hiding).toEqual(expect.arrayContaining([ |
| 173 | + expect.stringMatching(/:invisible$/), |
| 174 | + expect.stringMatching(/:pointer-events-none$/), |
| 175 | + ])); |
| 176 | + |
| 177 | + const count = countIn(label); |
| 178 | + expect(count).not.toBeNull(); |
| 179 | + for (const t of hiding) expect(tokens(count!)).toContain(t); |
| 180 | + }); |
| 181 | +}); |
0 commit comments