|
| 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 | + * An empty `tree` list view draws the view's empty state (objectstack#28966). |
| 11 | + * |
| 12 | + * ## The defect this pins |
| 13 | + * |
| 14 | + * `ListView` drew `ListEmptyState` (the authored `emptyState`, otherwise the |
| 15 | + * localized `list.*` copy) only for the `grid` and `gallery` visualizations. A |
| 16 | + * tree view was handed the empty set and `ObjectTree`, which reads no |
| 17 | + * `emptyState`, drew the generic `common.noData` line instead. An administrator |
| 18 | + * who authored guidance on a view saw it on the grid and lost it the moment the |
| 19 | + * same view was a tree, as Setup's business units are by default. |
| 20 | + * |
| 21 | + * ## Which rows discriminate |
| 22 | + * |
| 23 | + * RED against the unfixed condition: every row that waits for |
| 24 | + * `[data-testid="empty-state"]` on a tree view (the tree stand-in was mounted |
| 25 | + * instead). GREEN in both worlds: the CONTROL with a row, which proves the |
| 26 | + * tree visualization is the one mounted, so an empty state drawn for some |
| 27 | + * other reason (a view that silently fell back to the grid) cannot pass the |
| 28 | + * other rows vacuously. |
| 29 | + * |
| 30 | + * The tree is a stand-in registered under `object-tree`: this package does not |
| 31 | + * depend on `plugin-tree`, and what is pinned is `ListView`'s branch, which |
| 32 | + * decides whether the tree is mounted at all. |
| 33 | + */ |
| 34 | + |
| 35 | +import * as React from 'react'; |
| 36 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 37 | +import { render, cleanup, waitFor, screen } from '@testing-library/react'; |
| 38 | +import { ComponentRegistry } from '@object-ui/core'; |
| 39 | +import { I18nProvider, LocalizationProvider } from '@object-ui/i18n'; |
| 40 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 41 | +import type { DataSource, ListViewSchema } from '@object-ui/types'; |
| 42 | +import { ListView } from '../ListView'; |
| 43 | + |
| 44 | +ComponentRegistry.register( |
| 45 | + 'object-tree', |
| 46 | + () => <div data-testid="object-tree-standin" />, |
| 47 | + { namespace: 'test', label: 'object-tree stand-in', category: 'view' }, |
| 48 | +); |
| 49 | + |
| 50 | +const ZH = { defaultLanguage: 'zh', detectBrowserLanguage: false } as const; |
| 51 | + |
| 52 | +/** The built-in `zh` pack's `list.*` first-run rows. */ |
| 53 | +const ZH_FIRST_RUN_TITLE = '这里还没有数据'; |
| 54 | +const ZH_FIRST_RUN_MESSAGE = '创建第一条记录即可开始。'; |
| 55 | + |
| 56 | +const OBJECT_DEF = { |
| 57 | + name: 'business_unit', |
| 58 | + fields: { |
| 59 | + name: { type: 'text', label: 'Name' }, |
| 60 | + parent: { type: 'lookup', reference: 'business_unit', label: 'Parent' }, |
| 61 | + }, |
| 62 | +}; |
| 63 | + |
| 64 | +const dataSourceAnswering = (rows: Record<string, unknown>[]) => ({ |
| 65 | + find: vi.fn().mockResolvedValue(rows), |
| 66 | + findOne: vi.fn(), |
| 67 | + create: vi.fn(), |
| 68 | + update: vi.fn(), |
| 69 | + delete: vi.fn(), |
| 70 | + getObjectSchema: vi.fn().mockResolvedValue(OBJECT_DEF), |
| 71 | +}); |
| 72 | + |
| 73 | +const EMPTY_STATE = '[data-testid="empty-state"]'; |
| 74 | +const TREE = '[data-testid="object-tree-standin"]'; |
| 75 | +const headingOf = (state: Element) => state.querySelector('h3')?.textContent ?? null; |
| 76 | +const messageOf = (state: Element) => state.querySelector('p')?.textContent ?? null; |
| 77 | + |
| 78 | +type Locale = 'zh-pack' | { inline: string } | 'none'; |
| 79 | + |
| 80 | +function withLocale(ui: React.ReactElement, locale: Locale) { |
| 81 | + if (locale === 'zh-pack') { |
| 82 | + return <I18nProvider config={ZH} persistLanguage={false}>{ui}</I18nProvider>; |
| 83 | + } |
| 84 | + if (locale === 'none') return ui; |
| 85 | + return <LocalizationProvider value={{ locale: locale.inline }}>{ui}</LocalizationProvider>; |
| 86 | +} |
| 87 | + |
| 88 | +function mountListView( |
| 89 | + viewType: 'grid' | 'tree', |
| 90 | + extra: Partial<ListViewSchema>, |
| 91 | + locale: Locale, |
| 92 | + rows: Record<string, unknown>[] = [], |
| 93 | +) { |
| 94 | + const ds = dataSourceAnswering(rows); |
| 95 | + const schema = { |
| 96 | + type: 'list-view', |
| 97 | + objectName: 'business_unit', |
| 98 | + viewType, |
| 99 | + columns: ['name'], |
| 100 | + ...(viewType === 'tree' ? { tree: { parentField: 'parent' } } : {}), |
| 101 | + ...extra, |
| 102 | + } as ListViewSchema; |
| 103 | + return render( |
| 104 | + withLocale( |
| 105 | + <SchemaRendererProvider dataSource={ds as unknown as DataSource}> |
| 106 | + <ListView schema={schema} dataSource={ds as unknown as DataSource} /> |
| 107 | + </SchemaRendererProvider>, |
| 108 | + locale, |
| 109 | + ), |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +async function emptyStateOf( |
| 114 | + viewType: 'grid' | 'tree', |
| 115 | + extra: Partial<ListViewSchema>, |
| 116 | + locale: Locale, |
| 117 | +): Promise<HTMLElement> { |
| 118 | + const { container } = mountListView(viewType, extra, locale); |
| 119 | + await waitFor(() => expect(container.querySelector(EMPTY_STATE)).not.toBeNull()); |
| 120 | + return container.querySelector(EMPTY_STATE) as HTMLElement; |
| 121 | +} |
| 122 | + |
| 123 | +afterEach(() => cleanup()); |
| 124 | + |
| 125 | +describe('an empty tree list view draws the view empty state (objectstack#28966)', () => { |
| 126 | + it('CONTROL: with a row, the tree visualization is the one mounted, and no empty state', async () => { |
| 127 | + const { container } = mountListView('tree', {}, 'none', [{ id: 'b1', name: 'Head office' }]); |
| 128 | + await waitFor(() => expect(container.querySelector(TREE)).not.toBeNull()); |
| 129 | + expect(container.querySelector(EMPTY_STATE)).toBeNull(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('an authored plain-string `emptyState` is drawn as written, in place of the tree', async () => { |
| 133 | + const state = await emptyStateOf( |
| 134 | + 'tree', |
| 135 | + { emptyState: { title: 'No business units yet', message: 'Add the head office first.' } }, |
| 136 | + 'none', |
| 137 | + ); |
| 138 | + expect(headingOf(state)).toBe('No business units yet'); |
| 139 | + expect(messageOf(state)).toBe('Add the head office first.'); |
| 140 | + expect(state.ownerDocument.querySelector(TREE)).toBeNull(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('an authored `emptyState` inline locale map paints its zh-CN entries', async () => { |
| 144 | + const state = await emptyStateOf( |
| 145 | + 'tree', |
| 146 | + { |
| 147 | + emptyState: { |
| 148 | + title: { en: 'No business units yet', 'zh-CN': '还没有业务单元' }, |
| 149 | + message: { en: 'Add the head office first.', 'zh-CN': '请先添加总部。' }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + { inline: 'zh-CN' }, |
| 153 | + ); |
| 154 | + expect(headingOf(state)).toBe('还没有业务单元'); |
| 155 | + expect(messageOf(state)).toBe('请先添加总部。'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('zh, nothing authored: the localized first-run copy, not the tree generic line', async () => { |
| 159 | + const state = await emptyStateOf('tree', {}, 'zh-pack'); |
| 160 | + expect(headingOf(state)).toBe(ZH_FIRST_RUN_TITLE); |
| 161 | + expect(messageOf(state)).toBe(ZH_FIRST_RUN_MESSAGE); |
| 162 | + expect(state.ownerDocument.body.textContent).not.toContain('暂无数据'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('the list toolbar stays on screen above the empty tree', async () => { |
| 166 | + // The wide toolbar (objectui#12423: the compact form is the default), so |
| 167 | + // its Filter and Refresh buttons are named controls a test can find. The |
| 168 | + // empty state replaces the visualization below the toolbar, never the |
| 169 | + // toolbar: the reader keeps Filter to widen the query and Refresh. |
| 170 | + const state = await emptyStateOf( |
| 171 | + 'tree', |
| 172 | + { compactToolbar: false, emptyState: { title: 'No business units yet' } } as Partial<ListViewSchema>, |
| 173 | + 'none', |
| 174 | + ); |
| 175 | + expect(screen.getByTestId('toolbar-filter-trigger')).toBeTruthy(); |
| 176 | + expect(screen.getByTestId('refresh-button')).toBeTruthy(); |
| 177 | + expect(headingOf(state)).toBe('No business units yet'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('the tree and the grid draw the SAME empty state, node for node', async () => { |
| 181 | + const view: Partial<ListViewSchema> = { |
| 182 | + emptyState: { title: { en: 'Nothing here', 'zh-CN': '这里什么都没有' }, message: 'Start with one.' }, |
| 183 | + }; |
| 184 | + const grid = (await emptyStateOf('grid', view, { inline: 'zh-CN' })).outerHTML; |
| 185 | + cleanup(); |
| 186 | + const tree = (await emptyStateOf('tree', view, { inline: 'zh-CN' })).outerHTML; |
| 187 | + expect(tree).toBe(grid); |
| 188 | + // Non-vacuity: what both drew is the authored, resolved copy. |
| 189 | + expect(grid).toContain('这里什么都没有'); |
| 190 | + }); |
| 191 | +}); |
0 commit comments