|
| 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#36430 — a board grouped by a reference field (lookup, user, |
| 11 | + * master-detail, tree). |
| 12 | + * |
| 13 | + * The board fetches with `$expand`, so the lane field's value is the |
| 14 | + * referenced record. Before the fix the lanes were built from |
| 15 | + * `String(value)`: one lane per card, every one titled "[object Object]", and |
| 16 | + * each holding every card (bucketing keyed on the same string). |
| 17 | + * |
| 18 | + * Rows: |
| 19 | + * (a) one lane per referenced record, titled by its name, holding that |
| 20 | + * record's cards; a card with no value sits in "Uncategorized"; |
| 21 | + * (b) the referenced object's declared name field titles the lane; |
| 22 | + * (c) a drag to another lane writes the foreign key, and the card stays in |
| 23 | + * the lane it was dropped on, still naming its new owner; |
| 24 | + * (d) declared lanes keyed by foreign key bucket expanded values. |
| 25 | + * |
| 26 | + * The drop is driven through `DndContext`'s real `onDragEnd`, captured as in |
| 27 | + * `ObjectKanban.rejectedMoveRollback.test.tsx`: jsdom has no pointer layout. |
| 28 | + */ |
| 29 | + |
| 30 | +import React from 'react'; |
| 31 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 32 | +import { render, screen, within, act, cleanup, waitFor } from '@testing-library/react'; |
| 33 | +import '@testing-library/jest-dom'; |
| 34 | +import { registerAllFields } from '@object-ui/fields'; |
| 35 | +import type { DataSource } from '@object-ui/types'; |
| 36 | +import { ObjectKanban } from './ObjectKanban'; |
| 37 | +import './KanbanImpl'; |
| 38 | + |
| 39 | +const dnd = vi.hoisted(() => ({ |
| 40 | + onDragEnd: undefined as undefined | ((event: unknown) => void), |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock('@dnd-kit/core', async (importOriginal) => { |
| 44 | + const actual = await importOriginal<typeof import('@dnd-kit/core')>(); |
| 45 | + const ReactMod = await import('react'); |
| 46 | + const CapturingDndContext = (props: Record<string, unknown>) => { |
| 47 | + dnd.onDragEnd = props.onDragEnd as (event: unknown) => void; |
| 48 | + return ReactMod.createElement(actual.DndContext, props as never); |
| 49 | + }; |
| 50 | + return { ...actual, DndContext: CapturingDndContext }; |
| 51 | +}); |
| 52 | + |
| 53 | +registerAllFields(); |
| 54 | + |
| 55 | +const DEAL = { |
| 56 | + name: 'deal', |
| 57 | + fields: { |
| 58 | + title: { type: 'text', label: 'Title' }, |
| 59 | + owner_id: { type: 'user', label: 'Owner', reference: 'sys_user' }, |
| 60 | + account_id: { type: 'lookup', label: 'Account', reference: 'account' }, |
| 61 | + }, |
| 62 | +}; |
| 63 | + |
| 64 | +/** The account object names its records by `company`, not by a name-ish key. */ |
| 65 | +const ACCOUNT = { |
| 66 | + name: 'account', |
| 67 | + nameField: 'company', |
| 68 | + fields: { company: { type: 'text', label: 'Company' }, code: { type: 'text', label: 'Code' } }, |
| 69 | +}; |
| 70 | + |
| 71 | +const ALICE = { id: 'u1', name: 'Alice' }; |
| 72 | +const BOB = { id: 'u2', name: 'Bob' }; |
| 73 | +const ACME = { id: 'a1', company: 'Acme', code: 'ZZ-1' }; |
| 74 | +const GLOBEX = { id: 'a2', company: 'Globex', code: 'ZZ-2' }; |
| 75 | + |
| 76 | +const rows = () => [ |
| 77 | + { id: 'd1', title: 'Deal one', owner_id: ALICE, account_id: ACME }, |
| 78 | + { id: 'd2', title: 'Deal two', owner_id: BOB, account_id: GLOBEX }, |
| 79 | + { id: 'd3', title: 'Deal three', owner_id: ALICE, account_id: ACME }, |
| 80 | + { id: 'd4', title: 'Deal four', owner_id: null, account_id: null }, |
| 81 | +]; |
| 82 | + |
| 83 | +function makeDataSource(update = vi.fn(async () => ({}))): DataSource { |
| 84 | + return { |
| 85 | + getObjectSchema: vi.fn(async (name: string) => (name === 'account' ? ACCOUNT : name === 'deal' ? DEAL : { name, fields: {} })), |
| 86 | + find: vi.fn(async () => ({ value: rows() })), |
| 87 | + update, |
| 88 | + } as unknown as DataSource; |
| 89 | +} |
| 90 | + |
| 91 | +/** The cards in a lane, by the lane's visible title. */ |
| 92 | +function cardsIn(laneTitle: string): string[] { |
| 93 | + const list = screen.getByRole('list', { name: `${laneTitle} cards` }); |
| 94 | + return within(list) |
| 95 | + .queryAllByRole('listitem') |
| 96 | + .map((el) => el.getAttribute('aria-label') ?? ''); |
| 97 | +} |
| 98 | + |
| 99 | +function laneTitles(container: HTMLElement): string[] { |
| 100 | + return Array.from(container.querySelectorAll<HTMLElement>('[role="group"][aria-label]')) |
| 101 | + .map((lane) => lane.getAttribute('aria-label') ?? ''); |
| 102 | +} |
| 103 | + |
| 104 | +async function mountBoard(groupBy: string, extra: Record<string, unknown> = {}, dataSource = makeDataSource()) { |
| 105 | + const result = render( |
| 106 | + <ObjectKanban |
| 107 | + schema={{ type: 'object-kanban', objectName: 'deal', groupBy, cardTitle: 'title', ...extra } as any} |
| 108 | + dataSource={dataSource} |
| 109 | + />, |
| 110 | + ); |
| 111 | + expect(await screen.findByText('Deal four')).toBeInTheDocument(); |
| 112 | + await act(async () => { |
| 113 | + await Promise.resolve(); |
| 114 | + await Promise.resolve(); |
| 115 | + }); |
| 116 | + return result; |
| 117 | +} |
| 118 | + |
| 119 | +beforeEach(() => { |
| 120 | + dnd.onDragEnd = undefined; |
| 121 | +}); |
| 122 | + |
| 123 | +afterEach(() => { |
| 124 | + cleanup(); |
| 125 | + vi.restoreAllMocks(); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('a board grouped by a reference field (objectstack#36430)', () => { |
| 129 | + it('(a) draws one lane per owner, titled by name, holding that owner\'s cards', async () => { |
| 130 | + const { container } = await mountBoard('owner_id'); |
| 131 | + await waitFor(() => expect(cardsIn('Alice')).toEqual(['Deal one', 'Deal three'])); |
| 132 | + expect(cardsIn('Bob')).toEqual(['Deal two']); |
| 133 | + expect(cardsIn('Uncategorized')).toEqual(['Deal four']); |
| 134 | + expect(container.textContent).not.toContain('[object Object]'); |
| 135 | + expect(laneTitles(container).filter((t) => t === 'Alice')).toHaveLength(1); |
| 136 | + }); |
| 137 | + |
| 138 | + it('(b) titles a lookup lane by the target object\'s declared name field', async () => { |
| 139 | + const { container } = await mountBoard('account_id'); |
| 140 | + await waitFor(() => expect(cardsIn('Acme')).toEqual(['Deal one', 'Deal three'])); |
| 141 | + expect(cardsIn('Globex')).toEqual(['Deal two']); |
| 142 | + expect(laneTitles(container)).not.toContain('ZZ-1'); |
| 143 | + expect(container.textContent).not.toContain('[object Object]'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('(c) a drag to another owner\'s lane writes the foreign key and keeps the card there', async () => { |
| 147 | + const update = vi.fn(async () => ({})); |
| 148 | + await mountBoard('owner_id', {}, makeDataSource(update)); |
| 149 | + await waitFor(() => expect(cardsIn('Bob')).toEqual(['Deal two'])); |
| 150 | + |
| 151 | + expect(dnd.onDragEnd).toBeTypeOf('function'); |
| 152 | + await act(async () => { |
| 153 | + dnd.onDragEnd!({ active: { id: 'd1' }, over: { id: 'u2' } }); |
| 154 | + }); |
| 155 | + |
| 156 | + expect(update).toHaveBeenCalledTimes(1); |
| 157 | + expect(update).toHaveBeenCalledWith('deal', 'd1', { owner_id: 'u2' }); |
| 158 | + // Lanes hold their cards in fetch order, so d1 sorts ahead of d2. |
| 159 | + await waitFor(() => expect(cardsIn('Bob')).toEqual(['Deal one', 'Deal two'])); |
| 160 | + expect(cardsIn('Alice')).toEqual(['Deal three']); |
| 161 | + }); |
| 162 | + |
| 163 | + it('(d) declared lanes keyed by foreign key take the expanded values', async () => { |
| 164 | + await mountBoard('owner_id', { |
| 165 | + columns: [{ id: 'u1', title: 'Team Alice' }, { id: 'u2', title: 'Team Bob' }], |
| 166 | + }); |
| 167 | + await waitFor(() => expect(cardsIn('Team Alice')).toEqual(['Deal one', 'Deal three'])); |
| 168 | + expect(cardsIn('Team Bob')).toEqual(['Deal two']); |
| 169 | + expect(cardsIn('Uncategorized')).toEqual(['Deal four']); |
| 170 | + }); |
| 171 | +}); |
0 commit comments