Merge branch 'dev' into jajaja

# Conflicts:
#	package.json
#	yarn.lock
This commit is contained in:
Mike Cao 2025-03-31 23:31:09 -05:00
commit c71e9b5707
11 changed files with 126 additions and 30 deletions

View file

@ -0,0 +1,81 @@
import { renderNumberLabels, renderDateLabels } from '../charts';
import { formatDate } from '../date';
// test for renderNumberLabels
describe('renderNumberLabels', () => {
test.each([
['1000000', '1.0m'],
['2500000', '2.5m'],
])("formats numbers ≥ 1 million as 'Xm' (%s → %s)", (input, expected) => {
expect(renderNumberLabels(input)).toBe(expected);
});
test.each([['150000', '150k']])("formats numbers ≥ 100K as 'Xk' (%s → %s)", (input, expected) => {
expect(renderNumberLabels(input)).toBe(expected);
});
test.each([['12500', '12.5k']])(
"formats numbers ≥ 10K as 'X.Xk' (%s → %s)",
(input, expected) => {
expect(renderNumberLabels(input)).toBe(expected);
},
);
test.each([['1500', '1.50k']])("formats numbers ≥ 1K as 'X.XXk' (%s → %s)", (input, expected) => {
expect(renderNumberLabels(input)).toBe(expected);
});
test.each([['999', '999']])(
'calls formatNumber for values < 1000 (%s → %s)',
(input, expected) => {
expect(renderNumberLabels(input)).toBe(expected);
},
);
test.each([
['0', '0'],
['-5000', '-5000'],
])('handles edge cases correctly (%s → %s)', (input, expected) => {
expect(renderNumberLabels(input)).toBe(expected);
});
});
describe('renderDateLabels', () => {
const mockValues = [{ value: '2024-03-23T10:00:00Z' }, { value: '2024-03-24T15:30:00Z' }];
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
jest.spyOn(require('@/lib/date'), 'formatDate');
});
afterEach(() => {
jest.restoreAllMocks(); // Reset spy to prevent interference
});
test.each([
['minute', 'h:mm', 'en-US'],
['hour', 'p', 'en-US'],
['day', 'MMM d', 'en-US'],
['month', 'MMM', 'en-US'],
['year', 'yyyy', 'en-US'],
])('formats date correctly for unit: %s', (unit, expectedFormat, locale) => {
const formatLabel = renderDateLabels(unit, locale);
const formatted = formatLabel('label', 0, mockValues);
expect(formatDate).toHaveBeenCalledWith(new Date(mockValues[0].value), expectedFormat, locale);
expect(formatted).toBe(formatDate(new Date(mockValues[0].value), expectedFormat, locale));
});
test('returns label for unknown unit', () => {
const formatLabel = renderDateLabels('unknown', 'en-US');
expect(formatLabel('original-label', 0, mockValues)).toBe('original-label');
});
test('throws error for invalid date input', () => {
const invalidValues = [{ value: 'invalid-date' }];
const formatLabel = renderDateLabels('day', 'en-US');
expect(() => formatLabel('label', 0, invalidValues)).toThrow();
});
});

View file

@ -11,15 +11,15 @@ export function renderDateLabels(unit: string, locale: string) {
switch (unit) {
case 'minute':
return formatDate(d, 'h:mm', locale);
return formatDate(d, 'p', locale).split(' ')[0];
case 'hour':
return formatDate(d, 'p', locale);
case 'day':
return formatDate(d, 'MMM d', locale);
return formatDate(d, 'PP', locale).replace(/\W*20\d{2}\W*/, ''); // Remove year
case 'month':
return formatDate(d, 'MMM', locale);
case 'year':
return formatDate(d, 'YYY', locale);
return formatDate(d, 'yyyy', locale);
default:
return label;
}