import { describe, it, expect } from 'vitest' import { resolveConfig, mergeWidgetConfig } from './utils' describe('resolveConfig', () => { interface TestConfig { max: number items: number label?: string } type TestData = { value: number }[] const baseConfig: TestConfig = { max: 100, items: 10, label: 'default', } const testData: TestData = [{ value: 50 }, { value: 75 }] it('should return undefined when config is undefined', () => { const result = resolveConfig(undefined, baseConfig, testData) expect(result).toBeUndefined() }) it('should return the object config unchanged when config is an object', () => { const objectConfig = { max: 200, items: 20 } const result = resolveConfig(objectConfig, baseConfig, testData) expect(result).toEqual(objectConfig) }) it('should call function config with baseConfig and data when config is a function', () => { const fnConfig = (base: TestConfig, data: TestData) => ({ max: base.max * 2, items: data.length, }) const result = resolveConfig(fnConfig, baseConfig, testData) expect(result).toEqual({ max: 200, items: 2 }) }) it('should allow function config to access baseConfig properties', () => { const fnConfig = (base: TestConfig) => ({ label: `Modified: ${base.label}`, }) const result = resolveConfig(fnConfig, baseConfig, testData) expect(result).toEqual({ label: 'Modified: default' }) }) it('should allow function config to compute values from data', () => { const fnConfig = (_base: TestConfig, data: TestData) => ({ max: Math.max(...data.map((d) => d.value)), }) const result = resolveConfig(fnConfig, baseConfig, testData) expect(result).toEqual({ max: 75 }) }) it('should return partial config from function', () => { const fnConfig = () => ({ items: 5 }) const result = resolveConfig(fnConfig, baseConfig, testData) expect(result).toEqual({ items: 5 }) // Ensure it's partial - doesn't include all base properties expect(result).not.toHaveProperty('max') expect(result).not.toHaveProperty('label') }) }) describe('mergeWidgetConfig', () => { interface TestConfig { max: number items: number label?: string series?: { name: string }[] } it('should merge two partial configs', () => { const base: Partial = { max: 100, items: 10 } const override: Partial = { max: 200 } const result = mergeWidgetConfig(base, override) expect(result).toEqual({ max: 200, items: 10 }) }) it('should handle undefined base config', () => { const override: Partial = { max: 200 } const result = mergeWidgetConfig(undefined, override) expect(result).toEqual({ max: 200 }) }) it('should handle undefined override config', () => { const base: Partial = { max: 100 } const result = mergeWidgetConfig(base, undefined) expect(result).toEqual({ max: 100 }) }) it('should replace arrays instead of merging them', () => { const base: Partial = { series: [{ name: 'Series 1' }, { name: 'Series 2' }], } const override: Partial = { series: [{ name: 'New Series' }], } const result = mergeWidgetConfig(base, override) expect(result.series).toEqual([{ name: 'New Series' }]) }) })