refactoring
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
export const sortEntriesByLabel = (entries = []) => {
|
||||
return [...entries].sort((a, b) =>
|
||||
(a?.label || '').localeCompare(b?.label || '', 'de', { sensitivity: 'base' })
|
||||
);
|
||||
return [...entries].sort((a, b) => {
|
||||
const labelA = (a?.label || '').trim();
|
||||
const labelB = (b?.label || '').trim();
|
||||
if (!labelA && !labelB) {
|
||||
return 0;
|
||||
}
|
||||
if (!labelA) {
|
||||
return 1;
|
||||
}
|
||||
if (!labelB) {
|
||||
return -1;
|
||||
}
|
||||
return labelA.localeCompare(labelB, 'de', { sensitivity: 'base' });
|
||||
});
|
||||
};
|
||||
|
||||
21
src/utils/configUtils.test.js
Normal file
21
src/utils/configUtils.test.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { sortEntriesByLabel } from './configUtils';
|
||||
|
||||
describe('configUtils', () => {
|
||||
test('sortEntriesByLabel orders entries alphabetically', () => {
|
||||
const entries = [
|
||||
{ label: 'Zeta Markt' },
|
||||
{ label: 'alpha Shop' },
|
||||
{ label: 'Beta Store' }
|
||||
];
|
||||
const sorted = sortEntriesByLabel(entries);
|
||||
expect(sorted.map((item) => item.label)).toEqual(['alpha Shop', 'Beta Store', 'Zeta Markt']);
|
||||
});
|
||||
|
||||
test('sortEntriesByLabel handles missing labels', () => {
|
||||
const entries = [{}, { label: 'B' }, { label: 'A' }];
|
||||
const sorted = sortEntriesByLabel(entries);
|
||||
expect(sorted[0].label).toBe('A');
|
||||
expect(sorted[1].label).toBe('B');
|
||||
expect(sorted[2].label).toBeUndefined();
|
||||
});
|
||||
});
|
||||
27
src/utils/dateUtils.test.js
Normal file
27
src/utils/dateUtils.test.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { buildSelectionRange, formatDateValue, formatRangeLabel, parseDateValue } from './dateUtils';
|
||||
|
||||
describe('dateUtils', () => {
|
||||
test('parseDateValue returns null for invalid input', () => {
|
||||
expect(parseDateValue('invalid')).toBeNull();
|
||||
expect(parseDateValue('')).toBeNull();
|
||||
});
|
||||
|
||||
test('formatDateValue formats valid dates', () => {
|
||||
const date = new Date('2024-02-10T00:00:00.000Z');
|
||||
expect(formatDateValue(date)).toBe('2024-02-10');
|
||||
});
|
||||
|
||||
test('formatRangeLabel handles ranges and single dates', () => {
|
||||
expect(formatRangeLabel('2024-02-10', '2024-02-10')).toBe('10.02.2024');
|
||||
expect(formatRangeLabel('2024-02-10', '2024-02-12')).toBe('10.02.2024 – 12.02.2024');
|
||||
expect(formatRangeLabel(null, null)).toBe('Zeitraum auswählen');
|
||||
});
|
||||
|
||||
test('buildSelectionRange enforces minimum date', () => {
|
||||
const minDate = new Date('2024-05-01T00:00:00.000Z');
|
||||
const selection = buildSelectionRange('2024-04-01', '2024-04-05', minDate);
|
||||
expect(selection.startDate).toEqual(minDate);
|
||||
expect(selection.endDate).toEqual(minDate);
|
||||
expect(selection.key).toBe('selection');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user