|
| 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 | + * objectui#10069 (ruling A) — a kanban lane matches a record by lane `id`, the |
| 11 | + * stored option value, ONLY. The lane `title` is presentation. |
| 12 | + * |
| 13 | + * Before the ruling `bucketCardsIntoColumns` also lowercased every lane |
| 14 | + * `title` into its lookup, so the title was an undeclared second bucketing |
| 15 | + * key. Three things moved records as a result, and each is a row here: |
| 16 | + * |
| 17 | + * 1. LOCALE. Lane titles are translated (`localizeColumn` and |
| 18 | + * `translateOptions` in `ObjectKanban.tsx`), so a record storing the |
| 19 | + * English label bucketed into its lane under one locale and into |
| 20 | + * "Uncategorized" under another. Membership must not depend on titles. |
| 21 | + * 2. THE PICKLIST DOOR. With no authored `columns`, lanes are derived from |
| 22 | + * the groupBy field's options as `{ id: opt.value, title: opt.label }`, |
| 23 | + * so relabelling an option moved records too. Driven through the real |
| 24 | + * renderer, because that door lives in `ObjectKanban`, not in the pure |
| 25 | + * bucketer. (The authored-`columns` door is the flipped row in |
| 26 | + * `objectKanbanColumnMembers-8071.test.tsx`.) |
| 27 | + * 3. LOUDNESS. A record that no longer matches is a DATA defect (it stored a |
| 28 | + * label instead of the value), so the move into "Uncategorized" is |
| 29 | + * named on the console — once per distinct raw value per bucketing pass. |
| 30 | + */ |
| 31 | + |
| 32 | +import React from 'react'; |
| 33 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 34 | +import { render, waitFor, cleanup } from '@testing-library/react'; |
| 35 | +import { SchemaRenderer, SchemaRendererProvider } from '@object-ui/react'; |
| 36 | +// Registers `object-kanban`; module scope because the import IS the |
| 37 | +// registration (AGENTS.md's test-discipline section). |
| 38 | +import { bucketCardsIntoColumns, KANBAN_UNCOLUMNED_ID } from '../index'; |
| 39 | +// Same specifier as `index.tsx`'s `React.lazy` factory, imported at module |
| 40 | +// scope so the cold transform is billed to the import phase (objectui#3010). |
| 41 | +import '../KanbanImpl'; |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + cleanup(); |
| 45 | + vi.restoreAllMocks(); |
| 46 | +}); |
| 47 | + |
| 48 | +/** `id:card+card` per lane, in lane order. */ |
| 49 | +const summarise = (lanes: Array<any>): string => |
| 50 | + lanes.map((l: any) => `${String(l.id)}:${(l.cards || []).map((c: any) => c.id).join('+')}`).join(', '); |
| 51 | + |
| 52 | +const bucket = (columns: Array<any>, data: Array<any>) => |
| 53 | + bucketCardsIntoColumns(columns, data, 'status', undefined, 'Uncategorized'); |
| 54 | + |
| 55 | +const RECORDS = [ |
| 56 | + { id: 'r1', status: 'in_progress' }, // the option VALUE |
| 57 | + { id: 'r2', status: 'In Progress' }, // the English LABEL |
| 58 | + { id: 'r3', status: 'done' }, |
| 59 | +]; |
| 60 | + |
| 61 | +describe('objectui#10069 — lane membership is decided by lane id only', () => { |
| 62 | + it('⭐ a translated title does not change membership', () => { |
| 63 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 64 | + const english = [ |
| 65 | + { id: 'in_progress', title: 'In Progress' }, |
| 66 | + { id: 'done', title: 'Done' }, |
| 67 | + ]; |
| 68 | + const german = [ |
| 69 | + { id: 'in_progress', title: 'In Bearbeitung' }, |
| 70 | + { id: 'done', title: 'Erledigt' }, |
| 71 | + ]; |
| 72 | + |
| 73 | + const underEnglish = summarise(bucket(english, RECORDS)); |
| 74 | + const underGerman = summarise(bucket(german, RECORDS)); |
| 75 | + |
| 76 | + // Same ids, different titles ⇒ the same board. Before the ruling the |
| 77 | + // English run put r2 in `in_progress` and the German run swept it. |
| 78 | + expect(underEnglish).toBe(underGerman); |
| 79 | + expect(underEnglish).toBe(`in_progress:r1, done:r3, ${KANBAN_UNCOLUMNED_ID}:r2`); |
| 80 | + }); |
| 81 | + |
| 82 | + it('CONTROL — the id comparison keeps its case folding', () => { |
| 83 | + // The ruling retires the TITLE key only; an id spelled in another case |
| 84 | + // still lands, so the row above is about titles and not about folding. |
| 85 | + expect(summarise(bucket([{ id: 'in_progress', title: 'In Progress' }], [{ id: 'r1', status: 'IN_PROGRESS' }]))).toBe( |
| 86 | + 'in_progress:r1', |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('the console warn', () => { |
| 91 | + const LANES = [ |
| 92 | + { id: 'open', title: 'Open' }, |
| 93 | + { id: 'won', title: 'Won' }, |
| 94 | + ]; |
| 95 | + |
| 96 | + it('names the raw value, the grouping field and every available lane id', () => { |
| 97 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 98 | + bucket(LANES, [{ id: 'r1', status: 'Opened' }]); |
| 99 | + |
| 100 | + expect(warn).toHaveBeenCalledTimes(1); |
| 101 | + const message = String(warn.mock.calls[0][0]); |
| 102 | + expect(message).toContain('"Opened"'); |
| 103 | + expect(message).toContain('"status"'); |
| 104 | + expect(message).toContain('["open", "won"]'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('fires ONCE per distinct raw value per pass, not once per record', () => { |
| 108 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 109 | + bucket(LANES, [ |
| 110 | + { id: 'r1', status: 'Opened' }, |
| 111 | + { id: 'r2', status: 'Opened' }, |
| 112 | + { id: 'r3', status: 'Opened' }, |
| 113 | + { id: 'r4', status: 'Closed Won' }, |
| 114 | + { id: 'r5', status: 'OPEN' }, // matched (case-folded id): no warn |
| 115 | + ]); |
| 116 | + |
| 117 | + expect(warn).toHaveBeenCalledTimes(2); |
| 118 | + const messages = warn.mock.calls.map((c) => String(c[0])); |
| 119 | + expect(messages.find((m) => m.includes('"Opened"'))).toContain('3 record(s)'); |
| 120 | + expect(messages.find((m) => m.includes('"Closed Won"'))).toContain('1 record(s)'); |
| 121 | + |
| 122 | + // A second pass (the next render or data load) warns again: deduplication |
| 123 | + // is per pass, never module-wide, so a later board is not silenced. |
| 124 | + bucket(LANES, [{ id: 'r1', status: 'Opened' }]); |
| 125 | + expect(warn).toHaveBeenCalledTimes(3); |
| 126 | + }); |
| 127 | + |
| 128 | + it('stays silent when every record matches, and for an EMPTY group value', () => { |
| 129 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 130 | + const out = bucket(LANES, [ |
| 131 | + { id: 'r1', status: 'open' }, |
| 132 | + { id: 'r2', status: '' }, |
| 133 | + { id: 'r3' }, |
| 134 | + ]); |
| 135 | + |
| 136 | + // The empty-valued records still surface (#2792) — only the warn is withheld. |
| 137 | + expect(summarise(out)).toBe(`open:r1, won:, ${KANBAN_UNCOLUMNED_ID}:r2+r3`); |
| 138 | + expect(warn).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +/* -------------------------------------------------------------------------- */ |
| 144 | +/* The picklist door — lanes derived from `field.options`, no `columns` */ |
| 145 | +/* -------------------------------------------------------------------------- */ |
| 146 | + |
| 147 | +const OBJECT = 'deal'; |
| 148 | + |
| 149 | +const DEAL_SCHEMA = { |
| 150 | + name: OBJECT, |
| 151 | + label: 'Deal', |
| 152 | + fields: { |
| 153 | + name: { type: 'text', label: 'Name' }, |
| 154 | + status: { |
| 155 | + type: 'select', |
| 156 | + label: 'Status', |
| 157 | + options: [ |
| 158 | + { value: 'in_progress', label: 'In Progress' }, |
| 159 | + { value: 'done', label: 'Done' }, |
| 160 | + ], |
| 161 | + }, |
| 162 | + }, |
| 163 | +}; |
| 164 | + |
| 165 | +function makeAdapter(rows: Array<Record<string, unknown>>): Record<string, any> { |
| 166 | + return { |
| 167 | + find: vi.fn(async () => ({ data: rows })), |
| 168 | + findOne: vi.fn(), |
| 169 | + create: vi.fn(), |
| 170 | + update: vi.fn(), |
| 171 | + delete: vi.fn(), |
| 172 | + getObjectSchema: vi.fn(async () => DEAL_SCHEMA), |
| 173 | + }; |
| 174 | +} |
| 175 | + |
| 176 | +const cardsInList = (container: HTMLElement, label: string): string[] => |
| 177 | + [...(container.querySelector(`[role="list"][aria-label="${label}"]`)?.children ?? [])] |
| 178 | + .map((node) => node.getAttribute('aria-label') ?? '') |
| 179 | + .filter((name) => name !== ''); |
| 180 | + |
| 181 | +const laneNames = (container: HTMLElement): string[] => |
| 182 | + [...container.querySelectorAll('[role="group"]')].map((n) => n.getAttribute('aria-label') ?? ''); |
| 183 | + |
| 184 | +describe('objectui#10069 — the picklist door (`title: opt.label`) no longer buckets by label', () => { |
| 185 | + it('⭐ a record storing an option LABEL is swept; the one storing its VALUE lands', async () => { |
| 186 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 187 | + const adapter = makeAdapter([ |
| 188 | + { id: 'a', name: 'By value', status: 'in_progress' }, |
| 189 | + { id: 'b', name: 'By label', status: 'In Progress' }, |
| 190 | + ]); |
| 191 | + const { container } = render( |
| 192 | + <SchemaRendererProvider dataSource={adapter as any}> |
| 193 | + <SchemaRenderer schema={{ type: 'object-kanban', objectName: OBJECT, groupBy: 'status' } as never} /> |
| 194 | + </SchemaRendererProvider>, |
| 195 | + ); |
| 196 | + |
| 197 | + // Wait on the CARD, not the lane: lanes commit before the query settles. |
| 198 | + await waitFor(() => expect(cardsInList(container, 'In Progress cards')).toEqual(['By value'])); |
| 199 | + // The lanes really were derived from the picklist (titles are the labels), |
| 200 | + // plus the trailing sweep holding the label-valued record. |
| 201 | + expect(laneNames(container).slice(0, 2)).toEqual(['In Progress', 'Done']); |
| 202 | + expect(laneNames(container)).toHaveLength(3); |
| 203 | + expect(cardsInList(container, `${laneNames(container)[2]} cards`)).toEqual(['By label']); |
| 204 | + expect( |
| 205 | + warn.mock.calls.some((c) => String(c[0]).includes('"In Progress"') && String(c[0]).includes('"in_progress", "done"')), |
| 206 | + ).toBe(true); |
| 207 | + }); |
| 208 | +}); |
0 commit comments