|
| 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#35688 (S2), measured through the real `ObjectGrid` → `data-table` |
| 11 | + * path with the real `@object-ui/fields` editors: a `select` declared |
| 12 | + * `multiple` draws the check list in the cell editor, and each check only |
| 13 | + * stages. The edit is saved when the user moves on, as for `multiselect`. |
| 14 | + * A single `select` still saves on the pick (CONTROL). |
| 15 | + */ |
| 16 | +import { describe, it, expect, vi, beforeAll, beforeEach, afterEach } from 'vitest'; |
| 17 | +import { render, screen, waitFor, fireEvent, cleanup } from '@testing-library/react'; |
| 18 | +import '@testing-library/jest-dom'; |
| 19 | +import React from 'react'; |
| 20 | + |
| 21 | +import { ObjectGrid } from '../ObjectGrid'; |
| 22 | +import { __clearRecordCrudVerdictCache } from '../hooks/useRecordCrudVerdicts'; |
| 23 | +import { installExplainDouble } from './explainDouble'; |
| 24 | +import { registerAllFields } from '@object-ui/fields'; |
| 25 | +import { ActionProvider, SchemaRendererProvider } from '@object-ui/react'; |
| 26 | + |
| 27 | +registerAllFields(); |
| 28 | + |
| 29 | +beforeAll(() => { |
| 30 | + if (!Element.prototype.scrollIntoView) { |
| 31 | + Element.prototype.scrollIntoView = vi.fn() as any; |
| 32 | + } |
| 33 | + if (!(Element.prototype as any).hasPointerCapture) { |
| 34 | + (Element.prototype as any).hasPointerCapture = () => false; |
| 35 | + } |
| 36 | + if (!(Element.prototype as any).setPointerCapture) { |
| 37 | + (Element.prototype as any).setPointerCapture = () => {}; |
| 38 | + } |
| 39 | + if (!(Element.prototype as any).releasePointerCapture) { |
| 40 | + (Element.prototype as any).releasePointerCapture = () => {}; |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +beforeEach(() => { |
| 45 | + __clearRecordCrudVerdictCache(); |
| 46 | + installExplainDouble(); |
| 47 | +}); |
| 48 | +afterEach(() => { |
| 49 | + cleanup(); |
| 50 | + vi.unstubAllGlobals(); |
| 51 | +}); |
| 52 | + |
| 53 | +const OBJECT = 'task_35688_s2'; |
| 54 | +const COLORS = [ |
| 55 | + { label: 'Red', value: 'red' }, |
| 56 | + { label: 'Green', value: 'green' }, |
| 57 | + { label: 'Blue', value: 'blue' }, |
| 58 | +]; |
| 59 | + |
| 60 | +function makeDataSource() { |
| 61 | + const store: Record<string, any> = { |
| 62 | + r1: { id: 'r1', title: 'Design system', priority: 'high', colors: ['blue'] }, |
| 63 | + }; |
| 64 | + return { |
| 65 | + find: vi.fn(async () => { |
| 66 | + const data = Object.values(store).map((r) => ({ ...r })); |
| 67 | + return { data, total: data.length, hasMore: false, pageSize: 50 }; |
| 68 | + }), |
| 69 | + findOne: vi.fn(async (_o: string, id: string) => ({ ...store[id] })), |
| 70 | + update: vi.fn(async (_o: string, id: string, changes: Record<string, any>) => { |
| 71 | + store[id] = { ...store[id], ...changes }; |
| 72 | + return { ...store[id] }; |
| 73 | + }), |
| 74 | + getObjectSchema: async (name: string) => ({ |
| 75 | + name, |
| 76 | + fields: { |
| 77 | + id: { type: 'text' }, |
| 78 | + title: { type: 'text', label: 'Title' }, |
| 79 | + priority: { |
| 80 | + type: 'select', |
| 81 | + label: 'Priority', |
| 82 | + options: [ |
| 83 | + { label: 'Low', value: 'low' }, |
| 84 | + { label: 'High', value: 'high' }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + colors: { type: 'select', multiple: true, label: 'Colors', options: COLORS }, |
| 88 | + }, |
| 89 | + }), |
| 90 | + } as any; |
| 91 | +} |
| 92 | + |
| 93 | +function renderGrid() { |
| 94 | + const ds = makeDataSource(); |
| 95 | + const schema: any = { |
| 96 | + type: 'object-grid', |
| 97 | + objectName: OBJECT, |
| 98 | + editable: true, |
| 99 | + singleClickEdit: true, |
| 100 | + columns: [ |
| 101 | + { field: 'title', label: 'Title', type: 'text' }, |
| 102 | + { field: 'priority', label: 'Priority', type: 'select' }, |
| 103 | + { field: 'colors', label: 'Colors', type: 'select' }, |
| 104 | + ], |
| 105 | + pagination: { pageSize: 50 }, |
| 106 | + }; |
| 107 | + const utils = render( |
| 108 | + <ActionProvider> |
| 109 | + <SchemaRendererProvider dataSource={ds}> |
| 110 | + <div> |
| 111 | + <button data-testid="outside">outside</button> |
| 112 | + <ObjectGrid schema={schema} dataSource={ds} /> |
| 113 | + </div> |
| 114 | + </SchemaRendererProvider> |
| 115 | + </ActionProvider>, |
| 116 | + ); |
| 117 | + return { ...utils, ds }; |
| 118 | +} |
| 119 | + |
| 120 | +/** The body cell under the header named `label`. */ |
| 121 | +function cellUnder(container: HTMLElement, label: string): HTMLElement { |
| 122 | + const headers = Array.from(container.querySelectorAll('thead th')); |
| 123 | + const at = headers.findIndex((th) => th.textContent?.trim().startsWith(label)); |
| 124 | + expect(at).toBeGreaterThanOrEqual(0); |
| 125 | + return container.querySelectorAll('tbody tr')[0].querySelectorAll('td')[at] as HTMLElement; |
| 126 | +} |
| 127 | + |
| 128 | +// The grid batches edits (no `cellSelection`, so no per-cell autosave): what a |
| 129 | +// pick wrote shows as the row's pending change, and nothing reaches `update`. |
| 130 | +describe('ObjectGrid cell editor: a select declared multiple (objectstack#35688 S2)', () => { |
| 131 | + it('each check stages and the editor stays open; moving on closes it with every check', async () => { |
| 132 | + const { container } = renderGrid(); |
| 133 | + await waitFor(() => expect(screen.getByText('Design system')).toBeInTheDocument()); |
| 134 | + |
| 135 | + fireEvent.click(cellUnder(container, 'Colors')); |
| 136 | + await waitFor(() => expect(screen.getByTestId('in-cell-option-red')).toBeInTheDocument()); |
| 137 | + |
| 138 | + fireEvent.click(screen.getByTestId('in-cell-option-red')); |
| 139 | + // Still open after the first check, with the check on. |
| 140 | + expect(screen.getByTestId('in-cell-option-green')).toBeInTheDocument(); |
| 141 | + expect(screen.getByTestId('in-cell-option-red')).toHaveAttribute('aria-selected', 'true'); |
| 142 | + fireEvent.click(screen.getByTestId('in-cell-option-green')); |
| 143 | + expect(screen.getByTestId('in-cell-option-green')).toHaveAttribute('aria-selected', 'true'); |
| 144 | + |
| 145 | + fireEvent.pointerDown(screen.getByTestId('outside')); |
| 146 | + await waitFor(() => expect(screen.queryByTestId('in-cell-option-red')).toBeNull()); |
| 147 | + // This grid batches its edits: the row carries the three colours, pending. |
| 148 | + await waitFor(() => expect(screen.getByText(/1 row modified/)).toBeInTheDocument()); |
| 149 | + const cell = cellUnder(container, 'Colors'); |
| 150 | + for (const label of ['Blue', 'Red', 'Green']) expect(cell.textContent).toContain(label); |
| 151 | + expect(screen.queryByTestId('in-cell-option-red')).toBeNull(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('CONTROL: a single select saves on the pick and closes', async () => { |
| 155 | + const { container } = renderGrid(); |
| 156 | + await waitFor(() => expect(screen.getByText('Design system')).toBeInTheDocument()); |
| 157 | + |
| 158 | + fireEvent.click(cellUnder(container, 'Priority')); |
| 159 | + await waitFor(() => expect(screen.getByTestId('in-cell-option-low')).toBeInTheDocument()); |
| 160 | + fireEvent.click(screen.getByTestId('in-cell-option-low')); |
| 161 | + |
| 162 | + // The pick closes the editor, and the click that made it does not open it again. |
| 163 | + await waitFor(() => expect(screen.queryByTestId('in-cell-option-low')).toBeNull()); |
| 164 | + await waitFor(() => expect(screen.getByText(/1 row modified/)).toBeInTheDocument()); |
| 165 | + expect(cellUnder(container, 'Priority').textContent).toContain('Low'); |
| 166 | + }); |
| 167 | +}); |
0 commit comments