fix(categories): fall back to gray when category color is unknown
getCategoryColorClasses returned colorMap[color] directly, so a category whose color is not one of the known CATEGORY_COLORS (e.g. a legacy or out-of-enum value) yielded undefined. Callers such as the transaction categorizer command palette then read .bg off undefined, throwing 'TypeError: Cannot read properties of undefined (reading "bg")' and crashing the page (Sentry PHP-LARAVEL-4H). Mirror the existing guard in getCategoryChartColor by falling back to the gray classes when the color is not in the map.
This commit is contained in:
parent
91ffeb917d
commit
66c9d9119a
|
|
@ -1,6 +1,10 @@
|
|||
import { setTranslations } from '@/utils/i18n';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { getCategoryTypeLabel } from './category';
|
||||
import {
|
||||
type CategoryColor,
|
||||
getCategoryColorClasses,
|
||||
getCategoryTypeLabel,
|
||||
} from './category';
|
||||
|
||||
describe('getCategoryTypeLabel', () => {
|
||||
afterEach(() => {
|
||||
|
|
@ -25,3 +29,19 @@ describe('getCategoryTypeLabel', () => {
|
|||
expect(getCategoryTypeLabel('transfer')).toBe('Transfer');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCategoryColorClasses', () => {
|
||||
it('returns the classes for a known color', () => {
|
||||
expect(getCategoryColorClasses('blue')).toEqual({
|
||||
bg: 'bg-blue-100 dark:bg-blue-700',
|
||||
text: 'text-blue-700 dark:text-blue-100',
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to gray for an unknown color instead of returning undefined', () => {
|
||||
const classes = getCategoryColorClasses('chartreuse' as CategoryColor);
|
||||
|
||||
expect(classes).toEqual(getCategoryColorClasses('gray'));
|
||||
expect(classes.bg).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -235,7 +235,7 @@ export function getCategoryColorClasses(color: CategoryColor): {
|
|||
},
|
||||
};
|
||||
|
||||
return colorMap[color];
|
||||
return colorMap[color] ?? colorMap.gray;
|
||||
}
|
||||
|
||||
export function getCategoryChartColor(color: CategoryColor): string {
|
||||
|
|
|
|||
Loading…
Reference in New Issue