EnxDev commented on code in PR #38017:
URL: https://github.com/apache/superset/pull/38017#discussion_r2832230766
##########
superset-frontend/plugins/plugin-chart-echarts/test/utils/formatters.test.ts:
##########
@@ -35,3 +38,135 @@ describe('getPercentFormatter', () => {
).toEqual('60.00%');
});
});
+
+describe('getXAxisFormatter', () => {
+ test('should return smart date formatter for SMART_DATE_ID format', () => {
+ const formatter = getXAxisFormatter(SMART_DATE_ID);
+ expect(formatter).toBeDefined();
+ expect(formatter).toBeInstanceOf(TimeFormatter);
+ expect((formatter as TimeFormatter).id).toBe(SMART_DATE_ID);
+ });
+
+ test('should return smart date formatter for undefined format', () => {
+ const formatter = getXAxisFormatter();
+ expect(formatter).toBeDefined();
+ expect(formatter).toBeInstanceOf(TimeFormatter);
+ expect((formatter as TimeFormatter).id).toBe(SMART_DATE_ID);
+ });
+
+ test('should return custom time formatter for custom format', () => {
+ const customFormat = '%Y-%m-%d';
+ const formatter = getXAxisFormatter(customFormat);
+ expect(formatter).toBeDefined();
+ expect(formatter).toBeInstanceOf(TimeFormatter);
+ expect((formatter as TimeFormatter).id).toBe(customFormat);
+ });
+
+ test('smart date formatter should be returned and not undefined', () => {
+ const formatter = getXAxisFormatter(SMART_DATE_ID);
+ expect(formatter).toBeDefined();
+ expect(formatter).toBeInstanceOf(TimeFormatter);
+ expect((formatter as TimeFormatter).id).toBe(SMART_DATE_ID);
+
+ const undefinedFormatter = getXAxisFormatter(undefined);
+ expect(undefinedFormatter).toBeDefined();
+ expect(undefinedFormatter).toBeInstanceOf(TimeFormatter);
+ expect((undefinedFormatter as TimeFormatter).id).toBe(SMART_DATE_ID);
+
+ const emptyFormatter = getXAxisFormatter();
+ expect(emptyFormatter).toBeDefined();
+ expect(emptyFormatter).toBeInstanceOf(TimeFormatter);
+ expect((emptyFormatter as TimeFormatter).id).toBe(SMART_DATE_ID);
+ });
+
+ test('time grain aware formatter should prevent millisecond and timestamp
formats', () => {
+ const formatter = getXAxisFormatter(SMART_DATE_ID, 'P1M');
+
+ // Test that dates with milliseconds don't show millisecond format
+ const dateWithMs = new Date('2025-03-15T21:13:32.389Z');
+ const result = (formatter as TimeFormatter).format(dateWithMs);
+ expect(result).not.toContain('.389ms');
+ expect(result).not.toMatch(/\.\d+ms/);
+ expect(result).not.toContain('PM');
+ expect(result).not.toContain('AM');
+ expect(result).not.toMatch(/\d{1,2}:\d{2}/); // No time format
+ });
+
+ test('time grain aware formatting should prevent problematic formats', () =>
{
+ // Test that time grain aware formatter prevents the specific issues we
solved
+ const monthFormatter = getXAxisFormatter(SMART_DATE_ID, 'P1M');
+ const yearFormatter = getXAxisFormatter(SMART_DATE_ID, 'P1Y');
+ const dayFormatter = getXAxisFormatter(SMART_DATE_ID, 'P1D');
+
+ // Test dates that previously caused issues
+ const problematicDates = [
+ new Date('2025-03-15T21:13:32.389Z'), // Had .389ms issue
+ new Date('2025-04-01T02:30:00.000Z'), // Timezone edge case
+ new Date('2025-07-01T00:00:00.000Z'), // Month boundary
+ ];
+
+ problematicDates.forEach(date => {
+ // Month formatter should not show milliseconds or PM/AM
+ const monthResult = (monthFormatter as TimeFormatter).format(date);
+ expect(monthResult).not.toMatch(/\.\d+ms/);
+ expect(monthResult).not.toMatch(/PM|AM/);
+ expect(monthResult).not.toMatch(/\d{1,2}:\d{2}:\d{2}/);
+
+ // Year formatter should not show milliseconds or PM/AM
+ const yearResult = (yearFormatter as TimeFormatter).format(date);
+ expect(yearResult).not.toMatch(/\.\d+ms/);
+ expect(yearResult).not.toMatch(/PM|AM/);
+ expect(yearResult).not.toMatch(/\d{1,2}:\d{2}:\d{2}/);
+
+ // Day formatter should not show milliseconds or seconds
+ const dayResult = (dayFormatter as TimeFormatter).format(date);
+ expect(dayResult).not.toMatch(/\.\d+ms/);
+ expect(dayResult).not.toMatch(/:\d{2}:\d{2}/); // No seconds
+ });
+ });
+
+ test('time grain parameter should be passed correctly', () => {
+ // Test that formatter with time grain is different from formatter without
+ const formatterWithGrain = getXAxisFormatter(SMART_DATE_ID, 'P1M');
Review Comment:
Would it be better to use the existing constant here rather than 'P1M'?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]