|
| 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 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { normalizeListViewSchema } from '../normalize-list-view.js'; |
| 11 | + |
| 12 | +describe('normalizeListViewSchema (#2890)', () => { |
| 13 | + describe('fields → columns', () => { |
| 14 | + it('folds the legacy `fields` into the spec-canonical `columns`', () => { |
| 15 | + const out = normalizeListViewSchema({ type: 'list-view', viewType: 'grid', fields: ['name', 'stage'] }); |
| 16 | + expect(out).toEqual({ type: 'list-view', viewType: 'grid', columns: ['name', 'stage'] }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('drops the legacy key so a missed read-site fails loudly instead of taking the legacy path', () => { |
| 20 | + const out = normalizeListViewSchema({ viewType: 'grid', fields: ['name'] }) as Record<string, unknown>; |
| 21 | + expect('fields' in out).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + it('lets the canonical key win when a payload carries both', () => { |
| 25 | + const out = normalizeListViewSchema({ |
| 26 | + viewType: 'grid', |
| 27 | + columns: ['canonical'], |
| 28 | + fields: ['legacy'], |
| 29 | + }) as Record<string, unknown>; |
| 30 | + expect(out.columns).toEqual(['canonical']); |
| 31 | + expect('fields' in out).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it('preserves ListColumn object entries, not just string columns', () => { |
| 35 | + const columns = [{ field: 'name', label: 'Name', width: 200 }]; |
| 36 | + const out = normalizeListViewSchema({ viewType: 'grid', columns }) as Record<string, unknown>; |
| 37 | + expect(out.columns).toBe(columns); |
| 38 | + }); |
| 39 | + |
| 40 | + it('folds an empty `fields` array (an explicitly empty column set is not "absent")', () => { |
| 41 | + const out = normalizeListViewSchema({ viewType: 'grid', fields: [] }) as Record<string, unknown>; |
| 42 | + expect(out.columns).toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('ignores a non-array `fields` — malformed metadata must not become a column set', () => { |
| 46 | + const out = normalizeListViewSchema({ viewType: 'grid', fields: 'name' }) as Record<string, unknown>; |
| 47 | + expect(out.columns).toBeUndefined(); |
| 48 | + expect(out.fields).toBe('name'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('is idempotent', () => { |
| 52 | + const once = normalizeListViewSchema({ viewType: 'grid', fields: ['name'] }); |
| 53 | + const twice = normalizeListViewSchema(once); |
| 54 | + expect(twice).toEqual(once); |
| 55 | + expect(twice).toBe(once); // nothing left to fold → same reference |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('viewType defaulting', () => { |
| 60 | + it('defaults a missing view kind to the renderable `grid`', () => { |
| 61 | + expect(normalizeListViewSchema({ type: 'list-view' })).toEqual({ type: 'list-view', viewType: 'grid' }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('maps the view CATEGORY `list` to `grid` (AI-authored metadata stores `list`)', () => { |
| 65 | + const out = normalizeListViewSchema({ viewType: 'list' }) as Record<string, unknown>; |
| 66 | + expect(out.viewType).toBe('grid'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('leaves an explicit renderable kind alone', () => { |
| 70 | + const out = normalizeListViewSchema({ viewType: 'kanban' }) as Record<string, unknown>; |
| 71 | + expect(out.viewType).toBe('kanban'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('reference stability', () => { |
| 76 | + it('returns the input by reference when there is nothing to fold', () => { |
| 77 | + // Load-bearing: ListView memoizes on this identity, so allocating a fresh |
| 78 | + // object on every render would re-run every downstream useMemo. |
| 79 | + const schema = { type: 'list-view', viewType: 'grid', columns: ['name'] }; |
| 80 | + expect(normalizeListViewSchema(schema)).toBe(schema); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not mutate the input when it does fold', () => { |
| 84 | + const schema = { viewType: 'grid', fields: ['name'] }; |
| 85 | + normalizeListViewSchema(schema); |
| 86 | + expect(schema).toEqual({ viewType: 'grid', fields: ['name'] }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('tolerates non-object input', () => { |
| 90 | + expect(normalizeListViewSchema(null)).toBeNull(); |
| 91 | + expect(normalizeListViewSchema(undefined)).toBeUndefined(); |
| 92 | + expect(normalizeListViewSchema('list-view')).toBe('list-view'); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments