|
| 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 | + * `row-table` (#31856): the rows a page `api` binding answers, as a table, |
| 11 | + * rendered through the real `SchemaRenderer` with the binding read through the |
| 12 | + * host fetch on `SchemaRendererContext.apiFetch`. |
| 13 | + */ |
| 14 | + |
| 15 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 16 | +import { cleanup, render, screen, waitFor, within } from '@testing-library/react'; |
| 17 | +import React from 'react'; |
| 18 | +import { MemoryRouter } from 'react-router-dom'; |
| 19 | +import { ComponentRegistry, formatDateTime, setDisplayTimeZone } from '@object-ui/core'; |
| 20 | +import { I18nProvider } from '@object-ui/i18n'; |
| 21 | +import { SchemaRenderer, SchemaRendererContext } from '@object-ui/react'; |
| 22 | +import type { DeclaredNode } from '@object-ui/types'; |
| 23 | +import '../productBlocks'; |
| 24 | + |
| 25 | +const ACTORS = { |
| 26 | + success: true, |
| 27 | + data: { |
| 28 | + actors: [ |
| 29 | + { name: 'Ada', status: 'active', lastRun: { at: '2026-11-09T08:30:00Z' } }, |
| 30 | + { name: 'Grace', status: 'paused', lastRun: null }, |
| 31 | + { name: 'Linus', status: 'retired' }, |
| 32 | + ], |
| 33 | + none: [], |
| 34 | + summary: { count: 3 }, |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +const BINDING = { provider: 'api', read: { url: '/api/v1/ai/actors' }, path: 'data' } as const; |
| 39 | + |
| 40 | +const COLUMNS = [ |
| 41 | + { path: 'name', label: { en: 'Name', 'zh-CN': '名称' } }, |
| 42 | + { path: 'status', label: 'Status', format: 'enum', enum: { active: { en: 'Active' }, paused: 'Paused' } }, |
| 43 | + { path: 'lastRun.at', label: 'Last run', format: 'datetime' }, |
| 44 | +]; |
| 45 | + |
| 46 | +function renderTable(properties: Record<string, unknown>, bound = true) { |
| 47 | + const apiFetch = vi.fn(async () => new Response(JSON.stringify(ACTORS), { status: 200 })); |
| 48 | + const schema = { type: 'row-table', ...(bound ? { dataSource: BINDING } : {}), properties } as unknown as DeclaredNode; |
| 49 | + render( |
| 50 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}> |
| 51 | + <MemoryRouter> |
| 52 | + <SchemaRendererContext.Provider value={{ dataSource: null, apiFetch }}> |
| 53 | + <SchemaRenderer schema={schema} /> |
| 54 | + </SchemaRendererContext.Provider> |
| 55 | + </MemoryRouter> |
| 56 | + </I18nProvider>, |
| 57 | + ); |
| 58 | + return apiFetch; |
| 59 | +} |
| 60 | + |
| 61 | +/** The cell texts of one body row. */ |
| 62 | +function cells(row: number): string[] { |
| 63 | + return within(screen.getByTestId(`row-table-row-${row}`)) |
| 64 | + .getAllByRole('cell') |
| 65 | + .map((cell) => cell.textContent ?? ''); |
| 66 | +} |
| 67 | + |
| 68 | +afterEach(() => { |
| 69 | + cleanup(); |
| 70 | + setDisplayTimeZone(undefined); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('row-table (#31856)', () => { |
| 74 | + it('registers with declared inputs and the API binding input', () => { |
| 75 | + const config = ComponentRegistry.getConfig('row-table'); |
| 76 | + const names = (config?.inputs ?? []).map((input) => input.name); |
| 77 | + expect(names).toEqual(['rows', 'columns', 'emptyLabel', 'dataSource']); |
| 78 | + for (const input of config?.inputs ?? []) expect(input.description, input.name).toBeTruthy(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('draws every row the binding answers, its columns in authored order', async () => { |
| 82 | + const apiFetch = renderTable({ rows: '${data.actors}', columns: COLUMNS }); |
| 83 | + await waitFor(() => expect(screen.getByTestId('row-table-row-2')).toBeInTheDocument()); |
| 84 | + expect(apiFetch).toHaveBeenCalledTimes(1); |
| 85 | + expect(screen.getAllByRole('columnheader').map((head) => head.textContent)).toEqual(['Name', 'Status', 'Last run']); |
| 86 | + expect(cells(0)[0]).toBe('Ada'); |
| 87 | + expect(cells(1)[0]).toBe('Grace'); |
| 88 | + expect(cells(2)[0]).toBe('Linus'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('draws an enum value through its label map, and a value the map does not name as written', async () => { |
| 92 | + renderTable({ rows: '${data.actors}', columns: COLUMNS }); |
| 93 | + await waitFor(() => expect(screen.getByTestId('row-table-row-2')).toBeInTheDocument()); |
| 94 | + expect(cells(0)[1]).toBe('Active'); |
| 95 | + expect(cells(1)[1]).toBe('Paused'); |
| 96 | + expect(cells(2)[1]).toBe('retired'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('draws a datetime as an instant in the display time zone', async () => { |
| 100 | + setDisplayTimeZone('Asia/Tokyo'); |
| 101 | + renderTable({ rows: '${data.actors}', columns: COLUMNS }); |
| 102 | + await waitFor(() => expect(screen.getByTestId('row-table-row-0')).toBeInTheDocument()); |
| 103 | + const shown = cells(0)[2]; |
| 104 | + // The one date-display path, in the declared zone: 08:30Z is 17:30 in Tokyo. |
| 105 | + expect(shown).toBe(formatDateTime('2026-11-09T08:30:00Z', { locale: 'en', style: 'compact' })); |
| 106 | + expect(shown).toMatch(/17:30|5:30/); |
| 107 | + expect(shown).not.toMatch(/08:30|8:30/); |
| 108 | + }); |
| 109 | + |
| 110 | + it('draws "—" for a field the row lacks or holds null', async () => { |
| 111 | + renderTable({ rows: '${data.actors}', columns: COLUMNS }); |
| 112 | + await waitFor(() => expect(screen.getByTestId('row-table-row-2')).toBeInTheDocument()); |
| 113 | + expect(cells(1)[2]).toBe('—'); |
| 114 | + expect(cells(2)[2]).toBe('—'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('draws the authored empty label for an empty list, else the bundle\'s', async () => { |
| 118 | + renderTable({ rows: '${data.none}', columns: COLUMNS, emptyLabel: { en: 'No agents yet' } }); |
| 119 | + expect((await screen.findByTestId('row-table-empty')).textContent).toBe('No agents yet'); |
| 120 | + cleanup(); |
| 121 | + renderTable({ rows: [], columns: COLUMNS }, false); |
| 122 | + expect(screen.getByTestId('row-table-empty').textContent).toBe('No rows to display'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('refuses a hole that resolves to a non-list with the error panel, never the empty state', async () => { |
| 126 | + renderTable({ rows: '${data.summary}', columns: COLUMNS, emptyLabel: 'No agents yet' }); |
| 127 | + const panel = await screen.findByTestId('row-table-datasource-error'); |
| 128 | + expect(panel).toHaveAttribute('role', 'alert'); |
| 129 | + expect(panel.textContent).toContain('rows resolved to an object'); |
| 130 | + expect(screen.queryByTestId('row-table-empty')).toBeNull(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('refuses a column format it does not draw with the error panel', () => { |
| 134 | + renderTable({ rows: [{ name: 'Ada' }], columns: [{ path: 'name', label: 'Name', format: 'currency' }] }, false); |
| 135 | + const panel = screen.getByTestId('row-table-datasource-error'); |
| 136 | + expect(panel.textContent).toContain('columns[0].format "currency"'); |
| 137 | + expect(screen.queryByTestId('row-table-block')).toBeNull(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments