|
| 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 | +import { describe, it, expect, vi } from 'vitest'; |
| 10 | +import { render, screen } from '@testing-library/react'; |
| 11 | +import { ViewSwitcher } from '../ViewSwitcher'; |
| 12 | +import type { ViewSwitcherSchema, ViewType } from '@object-ui/types'; |
| 13 | + |
| 14 | +// Mock @object-ui/react to avoid circular dependency issues; mirrors |
| 15 | +// ObjectView.test.tsx, including the data-invalidation bus that |
| 16 | +// @object-ui/components imports at module-eval time. |
| 17 | +vi.mock('@object-ui/react', async () => { |
| 18 | + const React = await import('react'); |
| 19 | + return { |
| 20 | + SchemaRenderer: ({ schema }: any) => ( |
| 21 | + <div data-testid="schema-renderer" data-schema-type={schema?.type}> |
| 22 | + {schema?.type} |
| 23 | + </div> |
| 24 | + ), |
| 25 | + SchemaRendererContext: React.createContext(null), |
| 26 | + subscribeDataChanges: () => () => {}, |
| 27 | + notifyDataChanged: () => {}, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +// Every member of `ViewType`. Declared as `ViewType[]` rather than inferred, so |
| 32 | +// adding a member to the union without extending this list is a compile error |
| 33 | +// here too — the same guard `DEFAULT_VIEW_LABELS` / `DEFAULT_VIEW_ICONS` get |
| 34 | +// from being `Record<ViewType, ...>` (#2916). |
| 35 | +const ALL_VIEW_TYPES: ViewType[] = [ |
| 36 | + 'list', |
| 37 | + 'detail', |
| 38 | + 'grid', |
| 39 | + 'kanban', |
| 40 | + 'calendar', |
| 41 | + 'timeline', |
| 42 | + 'map', |
| 43 | + 'gallery', |
| 44 | + 'gantt', |
| 45 | + 'chart', |
| 46 | + 'tree', |
| 47 | +]; |
| 48 | + |
| 49 | +function schemaFor(types: ViewType[]): ViewSwitcherSchema { |
| 50 | + return { |
| 51 | + type: 'view-switcher', |
| 52 | + variant: 'buttons', |
| 53 | + views: types.map(type => ({ type })), |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +describe('ViewSwitcher default view labels and icons', () => { |
| 58 | + it('renders a non-empty label and an icon for every ViewType', () => { |
| 59 | + render(<ViewSwitcher schema={schemaFor(ALL_VIEW_TYPES)} />); |
| 60 | + |
| 61 | + for (const type of ALL_VIEW_TYPES) { |
| 62 | + // A missing entry falls back to the raw type key for the label and to no |
| 63 | + // icon at all, which is what a hole in either Record<ViewType, ...> map |
| 64 | + // looked like on screen. |
| 65 | + const button = screen |
| 66 | + .getAllByRole('button') |
| 67 | + .find(b => b.textContent?.trim().toLowerCase() === type || b.textContent?.trim() === type); |
| 68 | + expect(button, `no button rendered for view type "${type}"`).toBeDefined(); |
| 69 | + expect(button!.querySelector('svg'), `view type "${type}" rendered without an icon`).not.toBeNull(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it('labels the chart view "Chart" rather than falling back to the type key', () => { |
| 74 | + render(<ViewSwitcher schema={schemaFor(['chart'])} />); |
| 75 | + |
| 76 | + expect(screen.getByText('Chart')).toBeInTheDocument(); |
| 77 | + expect(screen.queryByText('chart')).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('still lets an explicit label and icon override the defaults', () => { |
| 81 | + render( |
| 82 | + <ViewSwitcher |
| 83 | + schema={{ |
| 84 | + type: 'view-switcher', |
| 85 | + variant: 'buttons', |
| 86 | + views: [{ type: 'chart', label: 'Revenue', icon: 'pie-chart' }], |
| 87 | + }} |
| 88 | + /> |
| 89 | + ); |
| 90 | + |
| 91 | + expect(screen.getByText('Revenue')).toBeInTheDocument(); |
| 92 | + expect(screen.queryByText('Chart')).toBeNull(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments