|
| 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 | + * objectstack#26613 — a `page:tabs` panel's count channel. |
| 9 | + * |
| 10 | + * A tab holding `list-view` blocks is badged with the number of records those |
| 11 | + * blocks' own queries matched: each block reports through |
| 12 | + * `usePageTabCountReporter`, and the badge shows the sum. So the tab can be |
| 13 | + * badged before it is opened, such a tab is mounted while inactive (hidden by |
| 14 | + * its `data-state`); a tab with an authored `count`, or with no list block, |
| 15 | + * keeps mounting on open only. |
| 16 | + * |
| 17 | + * The `list-view` here is a stand-in that reports the count it is handed, so |
| 18 | + * this file pins the strip's half of the channel alone; the real block's half |
| 19 | + * is pinned in plugin-list (`ListViewBlock.cappedPageBlock-26613.test.tsx`). |
| 20 | + * |
| 21 | + * What is pinned: |
| 22 | + * (a) each tab's badge is its blocks' reported counts, summed, the inactive |
| 23 | + * tabs included, which are mounted to report; |
| 24 | + * (b) an authored `count` wins, and its tab is not mounted while inactive; |
| 25 | + * (c) a tab with no list block is not mounted while inactive; |
| 26 | + * (d) a block that unmounts withdraws its count; |
| 27 | + * (e) outside a tab panel the reporter is `undefined`. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 31 | +import { render, cleanup, waitFor, act } from '@testing-library/react'; |
| 32 | +import React from 'react'; |
| 33 | +import { SchemaRenderer } from '@object-ui/react'; |
| 34 | +import { ComponentRegistry } from '@object-ui/core'; |
| 35 | +import '../renderers'; |
| 36 | +import { usePageTabCountReporter } from '../index'; |
| 37 | + |
| 38 | +let mounted: string[] = []; |
| 39 | +let reporterSeen: Array<boolean> = []; |
| 40 | + |
| 41 | +/** Reports the count it is handed, once mounted, and records that it mounted. */ |
| 42 | +const ReportingList: React.FC<any> = ({ schema }) => { |
| 43 | + const report = usePageTabCountReporter(); |
| 44 | + reporterSeen.push(report !== undefined); |
| 45 | + React.useEffect(() => { |
| 46 | + mounted.push(schema.id); |
| 47 | + if (typeof schema.reportCount === 'number') report?.(schema.reportCount); |
| 48 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 49 | + }, [schema.reportCount]); |
| 50 | + return <div data-testid={`list-${schema.id}`} />; |
| 51 | +}; |
| 52 | +ComponentRegistry.register('list-view', ReportingList, { namespace: 'test', label: 'Reporting list', category: 'view' }); |
| 53 | + |
| 54 | +const list = (id: string, reportCount: number) => ({ type: 'list-view', id, properties: { reportCount } }); |
| 55 | + |
| 56 | +const triggers = (container: HTMLElement) => |
| 57 | + Array.from(container.querySelectorAll('[role="tab"]')).map((t) => t.textContent); |
| 58 | + |
| 59 | +afterEach(() => { |
| 60 | + cleanup(); |
| 61 | + mounted = []; |
| 62 | + reporterSeen = []; |
| 63 | +}); |
| 64 | + |
| 65 | +describe('page:tabs badges a tab with its list blocks\' counts (objectstack#26613)', () => { |
| 66 | + it('(a) sums each tab\'s reports, the inactive tabs mounted to report', async () => { |
| 67 | + const { container } = render( |
| 68 | + <SchemaRenderer |
| 69 | + schema={{ |
| 70 | + type: 'page:tabs', |
| 71 | + id: 'home_tabs', |
| 72 | + items: [ |
| 73 | + { label: 'Leads', value: 'leads', children: [list('leads', 21)] }, |
| 74 | + { label: 'Work', value: 'work', children: [list('deals', 8), list('tasks', 3)] }, |
| 75 | + ], |
| 76 | + } as never} |
| 77 | + />, |
| 78 | + ); |
| 79 | + await waitFor(() => expect(triggers(container)).toEqual(['Leads21', 'Work11'])); |
| 80 | + expect(mounted.sort()).toEqual(['deals', 'leads', 'tasks']); |
| 81 | + // The inactive panel is in the tree, marked inactive for its `hidden` class. |
| 82 | + const work = container.querySelector('[role="tabpanel"][data-state="inactive"]'); |
| 83 | + expect(work?.className).toContain('data-[state=inactive]:hidden'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('(b) an authored count wins, and its tab mounts on open only', async () => { |
| 87 | + const { container } = render( |
| 88 | + <SchemaRenderer |
| 89 | + schema={{ |
| 90 | + type: 'page:tabs', |
| 91 | + id: 'home_tabs', |
| 92 | + items: [ |
| 93 | + { label: 'Leads', value: 'leads', children: [list('leads', 21)] }, |
| 94 | + { label: 'Deals', value: 'deals', count: 99, children: [list('deals', 8)] }, |
| 95 | + ], |
| 96 | + } as never} |
| 97 | + />, |
| 98 | + ); |
| 99 | + await waitFor(() => expect(triggers(container)).toEqual(['Leads21', 'Deals99'])); |
| 100 | + expect(mounted).toEqual(['leads']); |
| 101 | + }); |
| 102 | + |
| 103 | + it('(c) a tab with no list block mounts on open only', async () => { |
| 104 | + let probes = 0; |
| 105 | + const Probe: React.FC = () => { |
| 106 | + React.useEffect(() => { |
| 107 | + probes += 1; |
| 108 | + }, []); |
| 109 | + return null; |
| 110 | + }; |
| 111 | + ComponentRegistry.register('test-probe-26613', Probe, { namespace: 'test', label: 'Probe', category: 'view' }); |
| 112 | + const { container } = render( |
| 113 | + <SchemaRenderer |
| 114 | + schema={{ |
| 115 | + type: 'page:tabs', |
| 116 | + id: 'home_tabs', |
| 117 | + items: [ |
| 118 | + { label: 'Leads', value: 'leads', children: [list('leads', 21)] }, |
| 119 | + { label: 'Notes', value: 'notes', children: [{ type: 'test-probe-26613', id: 'probe' }] }, |
| 120 | + ], |
| 121 | + } as never} |
| 122 | + />, |
| 123 | + ); |
| 124 | + await waitFor(() => expect(triggers(container)).toEqual(['Leads21', 'Notes'])); |
| 125 | + expect(probes).toBe(0); |
| 126 | + }); |
| 127 | + |
| 128 | + it('(d) a block that unmounts withdraws its count', async () => { |
| 129 | + const schema = (children: unknown[]) => ({ |
| 130 | + type: 'page:tabs', |
| 131 | + id: 'home_tabs', |
| 132 | + items: [ |
| 133 | + { label: 'Work', value: 'work', children }, |
| 134 | + { label: 'Other', value: 'other', children: [] }, |
| 135 | + ], |
| 136 | + }); |
| 137 | + const view = render(<SchemaRenderer schema={schema([list('deals', 8), list('tasks', 3)]) as never} />); |
| 138 | + await waitFor(() => expect(triggers(view.container)).toEqual(['Work11', 'Other'])); |
| 139 | + act(() => { |
| 140 | + view.rerender(<SchemaRenderer schema={schema([list('deals', 8)]) as never} />); |
| 141 | + }); |
| 142 | + await waitFor(() => expect(triggers(view.container)).toEqual(['Work8', 'Other'])); |
| 143 | + }); |
| 144 | + |
| 145 | + it('(e) outside a tab panel the reporter is undefined', () => { |
| 146 | + render(<SchemaRenderer schema={list('bare', 5) as never} />); |
| 147 | + expect(reporterSeen.length).toBeGreaterThan(0); |
| 148 | + expect(reporterSeen.every((seen) => seen === false)).toBe(true); |
| 149 | + }); |
| 150 | +}); |
0 commit comments