|
| 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#28211 — a list view's `kanban.lanes`, handed to the board as its |
| 11 | + * `columns` by `kanbanDeclaredLanes` (`@object-ui/core`), the mapping both |
| 12 | + * producers of the `object-kanban` node make. |
| 13 | + * |
| 14 | + * Rows: |
| 15 | + * (a) the board draws only the listed lanes, in the listed order, over the |
| 16 | + * options' own order and over the lifecycle order it derives itself; |
| 17 | + * (b) a record whose value is not listed lands in `Uncategorized`, never |
| 18 | + * hidden; |
| 19 | + * (c) a listed lost lane still starts collapsed. |
| 20 | + */ |
| 21 | + |
| 22 | +import React from 'react'; |
| 23 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 24 | +import { render, waitFor, cleanup, within } from '@testing-library/react'; |
| 25 | +import '@testing-library/jest-dom'; |
| 26 | +import { SchemaRenderer, SchemaRendererProvider } from '@object-ui/react'; |
| 27 | +import { kanbanDeclaredLanes } from '@object-ui/core'; |
| 28 | +// Registers `object-kanban`. |
| 29 | +import './index'; |
| 30 | +// The board renders inside `KanbanRenderer`'s `React.lazy` boundary; importing |
| 31 | +// the chunk at module scope bills the cold transform to the import phase |
| 32 | +// (objectui#3010), same specifier as `index.tsx`'s factory. |
| 33 | +import './KanbanImpl'; |
| 34 | + |
| 35 | +const STAGE_OPTIONS = [ |
| 36 | + { value: 'new', label: 'New', kind: 'open' }, |
| 37 | + { value: 'working', label: 'Working', kind: 'open' }, |
| 38 | + { value: 'won', label: 'Won', kind: 'won' }, |
| 39 | + { value: 'lost', label: 'Lost', kind: 'lost' }, |
| 40 | +]; |
| 41 | + |
| 42 | +const OBJECT_SCHEMA = { |
| 43 | + name: 'lanes_deal', |
| 44 | + fields: { |
| 45 | + id: { type: 'text' }, |
| 46 | + name: { type: 'text', label: 'Name' }, |
| 47 | + stage: { name: 'stage', type: 'select', label: 'Stage', options: STAGE_OPTIONS }, |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +const ROWS = [ |
| 52 | + { id: 'd1', name: 'Deal in new', stage: 'new' }, |
| 53 | + { id: 'd2', name: 'Deal in working', stage: 'working' }, |
| 54 | + { id: 'd3', name: 'Deal won', stage: 'won' }, |
| 55 | + { id: 'd4', name: 'Deal lost', stage: 'lost' }, |
| 56 | +]; |
| 57 | + |
| 58 | +function makeDataSource() { |
| 59 | + return { |
| 60 | + find: vi.fn().mockResolvedValue({ data: ROWS, total: ROWS.length }), |
| 61 | + findOne: vi.fn(), |
| 62 | + create: vi.fn(), |
| 63 | + update: vi.fn(), |
| 64 | + delete: vi.fn(), |
| 65 | + getObjectSchema: vi.fn().mockResolvedValue(OBJECT_SCHEMA), |
| 66 | + } as any; |
| 67 | +} |
| 68 | + |
| 69 | +async function renderBoard(lanes: string[], waitForText: string) { |
| 70 | + const result = render( |
| 71 | + <SchemaRendererProvider dataSource={makeDataSource()}> |
| 72 | + <SchemaRenderer |
| 73 | + schema={{ |
| 74 | + type: 'object-kanban', |
| 75 | + objectName: 'lanes_deal', |
| 76 | + groupBy: 'stage', |
| 77 | + columns: kanbanDeclaredLanes(lanes, STAGE_OPTIONS), |
| 78 | + } as any} |
| 79 | + /> |
| 80 | + </SchemaRendererProvider>, |
| 81 | + ); |
| 82 | + await waitFor(() => expect(result.container.textContent).toContain(waitForText)); |
| 83 | + return result; |
| 84 | +} |
| 85 | + |
| 86 | +/** Each lane's `role="group"` element, in DOM order. */ |
| 87 | +const lanesOf = (container: HTMLElement): HTMLElement[] => |
| 88 | + Array.from(container.querySelectorAll<HTMLElement>('[role="group"][aria-label]')); |
| 89 | +const laneNamed = (container: HTMLElement, name: string) => |
| 90 | + lanesOf(container).find((l) => l.getAttribute('aria-label') === name); |
| 91 | + |
| 92 | +afterEach(cleanup); |
| 93 | + |
| 94 | +describe('the board draws the declared lanes (objectstack#28211)', () => { |
| 95 | + it('(a) draws only the listed lanes, in the listed order', async () => { |
| 96 | + const { container } = await renderBoard(['won', 'new'], 'Deal won'); |
| 97 | + const names = lanesOf(container).map((l) => l.getAttribute('aria-label')); |
| 98 | + expect(names.filter((n) => n !== 'Uncategorized')).toEqual(['Won', 'New']); |
| 99 | + expect(names).not.toContain('Working'); |
| 100 | + expect(names).not.toContain('Lost'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('(b) lands a record whose value is not listed in Uncategorized, never hidden', async () => { |
| 104 | + const { container } = await renderBoard(['won', 'new'], 'Deal won'); |
| 105 | + const uncategorized = laneNamed(container, 'Uncategorized'); |
| 106 | + expect(uncategorized).toBeDefined(); |
| 107 | + expect(uncategorized!.textContent).toContain('Deal in working'); |
| 108 | + expect(uncategorized!.textContent).toContain('Deal lost'); |
| 109 | + expect(laneNamed(container, 'Won')!.textContent).toContain('Deal won'); |
| 110 | + expect(laneNamed(container, 'New')!.textContent).toContain('Deal in new'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('(c) starts a listed lost lane collapsed, and leaves the others open', async () => { |
| 114 | + const { container } = await renderBoard(['lost', 'new'], 'Deal in new'); |
| 115 | + const lost = laneNamed(container, 'Lost')!; |
| 116 | + expect(within(lost).getByRole('button', { expanded: false })).toBeInTheDocument(); |
| 117 | + expect(lost.textContent).not.toContain('Deal lost'); |
| 118 | + expect(within(laneNamed(container, 'New')!).queryByRole('button', { expanded: false })).toBeNull(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments