|
| 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 | + * "Add group field" adds no field by itself (objectstack#36204). |
| 11 | + * |
| 12 | + * It used to add the first field no level used, at once: in a list the first |
| 13 | + * column, a name as a rule, so the first click grouped the records one per |
| 14 | + * group. With `askFieldOnAdd` the click writes nothing and opens a row whose |
| 15 | + * field dropdown is already open; the level is written only once a field is |
| 16 | + * picked there, and closing the dropdown without a pick writes nothing. |
| 17 | + * With no field to offer the button stays, disabled, and `labels.noFields` |
| 18 | + * says why; while `fieldOptionsLoading` it is disabled without a reason. |
| 19 | + * |
| 20 | + * DIRECTION: every pin but the CONTROLs fails on the old editor (the button |
| 21 | + * wrote the first field at once, no pending row existed, and with no option |
| 22 | + * the button was simply not drawn). The CONTROLs are the old behaviour, kept |
| 23 | + * for a host that passes none of the new props. |
| 24 | + */ |
| 25 | +import { describe, it, expect, vi } from 'vitest'; |
| 26 | +import React from 'react'; |
| 27 | +import { render, screen, fireEvent, waitFor, within } from '@testing-library/react'; |
| 28 | +import '@testing-library/jest-dom'; |
| 29 | +import { GroupingEditor, type GroupingConfigValue, type GroupingEditorProps } from '../custom/grouping-editor'; |
| 30 | + |
| 31 | +const FIELD_OPTIONS = [ |
| 32 | + { value: 'status', label: 'Status' }, |
| 33 | + { value: 'owner', label: 'Owner' }, |
| 34 | + { value: 'vip', label: 'VIP' }, |
| 35 | +]; |
| 36 | + |
| 37 | +function renderEditor(value: GroupingConfigValue | undefined, extra: Partial<GroupingEditorProps> = {}) { |
| 38 | + const onChange = vi.fn(); |
| 39 | + const props: GroupingEditorProps = { value, onChange, fieldOptions: FIELD_OPTIONS, askFieldOnAdd: true, ...extra }; |
| 40 | + const utils = render(<GroupingEditor {...props} />); |
| 41 | + return { ...utils, onChange, props }; |
| 42 | +} |
| 43 | + |
| 44 | +const labels = (options: HTMLElement[]) => options.map((o) => o.textContent ?? ''); |
| 45 | + |
| 46 | +describe('"Add group field" opens the field dropdown and writes nothing until a pick', () => { |
| 47 | + it('opens a row with its dropdown open, writes nothing, and hides the add button', async () => { |
| 48 | + const { onChange } = renderEditor(undefined); |
| 49 | + fireEvent.click(screen.getByTestId('grouping-add')); |
| 50 | + expect(onChange).not.toHaveBeenCalled(); |
| 51 | + expect(screen.getByTestId('grouping-field-pending')).toBeInTheDocument(); |
| 52 | + expect(screen.queryByTestId('grouping-add')).toBeNull(); |
| 53 | + // No level yet: the list is not grouped. |
| 54 | + expect(screen.queryByTestId('grouping-field-0')).toBeNull(); |
| 55 | + const listbox = await screen.findByRole('listbox'); |
| 56 | + expect(labels(within(listbox).getAllByRole('option'))).toEqual(['Status', 'Owner', 'VIP']); |
| 57 | + }); |
| 58 | + |
| 59 | + it('a pick writes the level and the row becomes that level', async () => { |
| 60 | + const { onChange, rerender, props } = renderEditor(undefined); |
| 61 | + fireEvent.click(screen.getByTestId('grouping-add')); |
| 62 | + const listbox = await screen.findByRole('listbox'); |
| 63 | + fireEvent.click(within(listbox).getAllByRole('option').find((o) => o.textContent === 'Owner')!); |
| 64 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 65 | + const written = onChange.mock.calls[0][0]; |
| 66 | + expect(written).toStrictEqual({ fields: [{ field: 'owner', order: 'asc', collapsed: false }] }); |
| 67 | + rerender(<GroupingEditor {...props} value={written} />); |
| 68 | + await waitFor(() => expect(screen.queryByTestId('grouping-field-pending')).toBeNull()); |
| 69 | + expect(screen.getByTestId('grouping-field-0')).toHaveTextContent('Owner'); |
| 70 | + expect(screen.getByTestId('grouping-add')).toBeInTheDocument(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('a second level offers only the fields no level uses and appends', async () => { |
| 74 | + const { onChange } = renderEditor({ fields: [{ field: 'status', order: 'desc', collapsed: true }] }); |
| 75 | + fireEvent.click(screen.getByTestId('grouping-add')); |
| 76 | + const listbox = await screen.findByRole('listbox'); |
| 77 | + expect(labels(within(listbox).getAllByRole('option'))).toEqual(['Owner', 'VIP']); |
| 78 | + fireEvent.click(within(listbox).getAllByRole('option').find((o) => o.textContent === 'VIP')!); |
| 79 | + expect(onChange.mock.calls[0][0]).toStrictEqual({ |
| 80 | + fields: [ |
| 81 | + { field: 'status', order: 'desc', collapsed: true }, |
| 82 | + { field: 'vip', order: 'asc', collapsed: false }, |
| 83 | + ], |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('closing the dropdown without a pick drops the row and writes nothing', async () => { |
| 88 | + const { onChange } = renderEditor(undefined); |
| 89 | + fireEvent.click(screen.getByTestId('grouping-add')); |
| 90 | + const listbox = await screen.findByRole('listbox'); |
| 91 | + fireEvent.keyDown(listbox, { key: 'Escape' }); |
| 92 | + await waitFor(() => expect(screen.queryByTestId('grouping-field-pending')).toBeNull()); |
| 93 | + expect(onChange).not.toHaveBeenCalled(); |
| 94 | + expect(screen.getByTestId('grouping-add')).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('CONTROL: without `askFieldOnAdd` the first unused field is added at once, as before', () => { |
| 98 | + const { onChange } = renderEditor(undefined, { askFieldOnAdd: undefined }); |
| 99 | + fireEvent.click(screen.getByTestId('grouping-add')); |
| 100 | + expect(onChange.mock.calls[0][0]).toStrictEqual({ |
| 101 | + fields: [{ field: 'status', order: 'asc', collapsed: false }], |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +describe('no field to offer: the button stays, disabled, and says why', () => { |
| 107 | + it('with `labels.noFields` the button is disabled and described by the reason', () => { |
| 108 | + const { onChange } = renderEditor(undefined, { fieldOptions: [], labels: { noFields: 'Nothing can group this' } }); |
| 109 | + const button = screen.getByTestId('grouping-add'); |
| 110 | + expect(button).toBeDisabled(); |
| 111 | + const reason = screen.getByTestId('grouping-no-fields'); |
| 112 | + expect(reason).toHaveTextContent('Nothing can group this'); |
| 113 | + expect(button).toHaveAttribute('aria-describedby', reason.id); |
| 114 | + fireEvent.click(button); |
| 115 | + expect(onChange).not.toHaveBeenCalled(); |
| 116 | + expect(screen.queryByTestId('grouping-field-pending')).toBeNull(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('while the options load the button is disabled without a reason', () => { |
| 120 | + renderEditor(undefined, { fieldOptions: [], fieldOptionsLoading: true, labels: { noFields: 'Nothing can group this' } }); |
| 121 | + const button = screen.getByTestId('grouping-add'); |
| 122 | + expect(button).toBeDisabled(); |
| 123 | + expect(button).not.toHaveAttribute('aria-describedby'); |
| 124 | + expect(screen.queryByTestId('grouping-no-fields')).toBeNull(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('once the options arrive the button is enabled', async () => { |
| 128 | + const { rerender, props } = renderEditor(undefined, { fieldOptions: [], fieldOptionsLoading: true }); |
| 129 | + expect(screen.getByTestId('grouping-add')).toBeDisabled(); |
| 130 | + rerender(<GroupingEditor {...props} fieldOptions={FIELD_OPTIONS} fieldOptionsLoading={false} />); |
| 131 | + expect(screen.getByTestId('grouping-add')).toBeEnabled(); |
| 132 | + fireEvent.click(screen.getByTestId('grouping-add')); |
| 133 | + expect(await screen.findByRole('listbox')).toBeInTheDocument(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('CONTROL: with no option and no `noFields`, no add button is drawn, as before', () => { |
| 137 | + renderEditor(undefined, { fieldOptions: [], askFieldOnAdd: undefined }); |
| 138 | + expect(screen.queryByTestId('grouping-add')).toBeNull(); |
| 139 | + expect(screen.queryByTestId('grouping-no-fields')).toBeNull(); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('a level grouped by a field the host does not offer', () => { |
| 144 | + it('shows the label `fieldLabel` gives', () => { |
| 145 | + renderEditor({ fields: [{ field: 'customer_name', order: 'asc', collapsed: false }] }, { |
| 146 | + fieldLabel: (f) => (f === 'customer_name' ? 'Customer name' : undefined), |
| 147 | + }); |
| 148 | + expect(screen.getByTestId('grouping-field-0')).toHaveTextContent('Customer name'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('CONTROL: without `fieldLabel` it shows the field name', () => { |
| 152 | + renderEditor({ fields: [{ field: 'customer_name', order: 'asc', collapsed: false }] }); |
| 153 | + expect(screen.getByTestId('grouping-field-0')).toHaveTextContent('customer_name'); |
| 154 | + }); |
| 155 | +}); |
0 commit comments