|
| 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 type { ListViewSchema } from '@object-ui/types'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests for Gallery/Timeline spec config propagation through ListView's |
| 14 | + * buildViewSchema. We test the internal logic by checking that the |
| 15 | + * ListViewSchema types accept spec config and that the config values are correct. |
| 16 | + */ |
| 17 | + |
| 18 | +describe('Gallery/Timeline Spec Config Types', () => { |
| 19 | + describe('GalleryConfig on ListViewSchema', () => { |
| 20 | + it('accepts spec gallery config with coverField', () => { |
| 21 | + const schema: ListViewSchema = { |
| 22 | + type: 'list-view', |
| 23 | + objectName: 'products', |
| 24 | + viewType: 'gallery', |
| 25 | + fields: ['name', 'photo'], |
| 26 | + gallery: { |
| 27 | + coverField: 'photo', |
| 28 | + coverFit: 'contain', |
| 29 | + cardSize: 'large', |
| 30 | + titleField: 'name', |
| 31 | + visibleFields: ['status', 'price'], |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + expect(schema.gallery?.coverField).toBe('photo'); |
| 36 | + expect(schema.gallery?.coverFit).toBe('contain'); |
| 37 | + expect(schema.gallery?.cardSize).toBe('large'); |
| 38 | + expect(schema.gallery?.titleField).toBe('name'); |
| 39 | + expect(schema.gallery?.visibleFields).toEqual(['status', 'price']); |
| 40 | + }); |
| 41 | + |
| 42 | + it('accepts all cardSize values', () => { |
| 43 | + const sizes = ['small', 'medium', 'large'] as const; |
| 44 | + sizes.forEach((cardSize) => { |
| 45 | + const schema: ListViewSchema = { |
| 46 | + type: 'list-view', |
| 47 | + objectName: 'products', |
| 48 | + viewType: 'gallery', |
| 49 | + fields: ['name'], |
| 50 | + gallery: { cardSize }, |
| 51 | + }; |
| 52 | + expect(schema.gallery?.cardSize).toBe(cardSize); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('accepts all coverFit values', () => { |
| 57 | + const fits = ['cover', 'contain', 'fill'] as const; |
| 58 | + fits.forEach((coverFit) => { |
| 59 | + const schema: ListViewSchema = { |
| 60 | + type: 'list-view', |
| 61 | + objectName: 'products', |
| 62 | + viewType: 'gallery', |
| 63 | + fields: ['name'], |
| 64 | + gallery: { coverFit }, |
| 65 | + }; |
| 66 | + expect(schema.gallery?.coverFit).toBe(coverFit); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('accepts legacy imageField and subtitleField alongside spec fields', () => { |
| 71 | + const schema: ListViewSchema = { |
| 72 | + type: 'list-view', |
| 73 | + objectName: 'products', |
| 74 | + viewType: 'gallery', |
| 75 | + fields: ['name'], |
| 76 | + gallery: { |
| 77 | + coverField: 'photo', |
| 78 | + imageField: 'legacyImg', |
| 79 | + subtitleField: 'description', |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + expect(schema.gallery?.coverField).toBe('photo'); |
| 84 | + expect(schema.gallery?.imageField).toBe('legacyImg'); |
| 85 | + expect(schema.gallery?.subtitleField).toBe('description'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('accepts gallery config from legacy options as fallback', () => { |
| 89 | + const schema: ListViewSchema = { |
| 90 | + type: 'list-view', |
| 91 | + objectName: 'products', |
| 92 | + viewType: 'gallery', |
| 93 | + fields: ['name'], |
| 94 | + options: { |
| 95 | + gallery: { imageField: 'oldImg', titleField: 'label' }, |
| 96 | + }, |
| 97 | + }; |
| 98 | + |
| 99 | + expect(schema.options?.gallery?.imageField).toBe('oldImg'); |
| 100 | + expect(schema.options?.gallery?.titleField).toBe('label'); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('TimelineConfig on ListViewSchema', () => { |
| 105 | + it('accepts spec timeline config with all fields', () => { |
| 106 | + const schema: ListViewSchema = { |
| 107 | + type: 'list-view', |
| 108 | + objectName: 'events', |
| 109 | + viewType: 'timeline', |
| 110 | + fields: ['name', 'date'], |
| 111 | + timeline: { |
| 112 | + startDateField: 'start_date', |
| 113 | + endDateField: 'end_date', |
| 114 | + titleField: 'event_name', |
| 115 | + groupByField: 'category', |
| 116 | + colorField: 'priority_color', |
| 117 | + scale: 'month', |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + expect(schema.timeline?.startDateField).toBe('start_date'); |
| 122 | + expect(schema.timeline?.endDateField).toBe('end_date'); |
| 123 | + expect(schema.timeline?.titleField).toBe('event_name'); |
| 124 | + expect(schema.timeline?.groupByField).toBe('category'); |
| 125 | + expect(schema.timeline?.colorField).toBe('priority_color'); |
| 126 | + expect(schema.timeline?.scale).toBe('month'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('accepts legacy dateField for backward compatibility', () => { |
| 130 | + const schema: ListViewSchema = { |
| 131 | + type: 'list-view', |
| 132 | + objectName: 'events', |
| 133 | + viewType: 'timeline', |
| 134 | + fields: ['name'], |
| 135 | + timeline: { |
| 136 | + startDateField: 'created_at', |
| 137 | + titleField: 'name', |
| 138 | + dateField: 'legacy_date', |
| 139 | + }, |
| 140 | + }; |
| 141 | + |
| 142 | + expect(schema.timeline?.startDateField).toBe('created_at'); |
| 143 | + expect(schema.timeline?.dateField).toBe('legacy_date'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('supports all scale values', () => { |
| 147 | + const scales = ['hour', 'day', 'week', 'month', 'quarter', 'year'] as const; |
| 148 | + scales.forEach((scale) => { |
| 149 | + const schema: ListViewSchema = { |
| 150 | + type: 'list-view', |
| 151 | + objectName: 'events', |
| 152 | + viewType: 'timeline', |
| 153 | + fields: ['name'], |
| 154 | + timeline: { startDateField: 'date', titleField: 'name', scale }, |
| 155 | + }; |
| 156 | + expect(schema.timeline?.scale).toBe(scale); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it('accepts timeline config from legacy options as fallback', () => { |
| 161 | + const schema: ListViewSchema = { |
| 162 | + type: 'list-view', |
| 163 | + objectName: 'events', |
| 164 | + viewType: 'timeline', |
| 165 | + fields: ['name'], |
| 166 | + options: { |
| 167 | + timeline: { dateField: 'created_at', titleField: 'name' }, |
| 168 | + }, |
| 169 | + }; |
| 170 | + |
| 171 | + expect(schema.options?.timeline?.dateField).toBe('created_at'); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + describe('spec config co-existence', () => { |
| 176 | + it('gallery and timeline configs can coexist on the same ListViewSchema', () => { |
| 177 | + const schema: ListViewSchema = { |
| 178 | + type: 'list-view', |
| 179 | + objectName: 'projects', |
| 180 | + viewType: 'grid', |
| 181 | + fields: ['name', 'date', 'photo'], |
| 182 | + gallery: { |
| 183 | + coverField: 'photo', |
| 184 | + cardSize: 'medium', |
| 185 | + titleField: 'name', |
| 186 | + visibleFields: ['status'], |
| 187 | + }, |
| 188 | + timeline: { |
| 189 | + startDateField: 'start_date', |
| 190 | + titleField: 'name', |
| 191 | + scale: 'quarter', |
| 192 | + groupByField: 'team', |
| 193 | + }, |
| 194 | + }; |
| 195 | + |
| 196 | + expect(schema.gallery?.coverField).toBe('photo'); |
| 197 | + expect(schema.gallery?.cardSize).toBe('medium'); |
| 198 | + expect(schema.timeline?.startDateField).toBe('start_date'); |
| 199 | + expect(schema.timeline?.scale).toBe('quarter'); |
| 200 | + expect(schema.timeline?.groupByField).toBe('team'); |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments