@@ -25,6 +25,12 @@ import {
2525 within ,
2626} from '@superset-ui/core/spec' ;
2727import { cloneDeep } from 'lodash' ;
28+ import {
29+ QueryMode ,
30+ TimeGranularity ,
31+ SMART_DATE_ID ,
32+ getTimeFormatterForGranularity ,
33+ } from '@superset-ui/core' ;
2834import TableChart , { sanitizeHeaderId } from '../src/TableChart' ;
2935import { GenericDataType } from '@apache-superset/core/api/core' ;
3036import transformProps from '../src/transformProps' ;
@@ -376,6 +382,52 @@ describe('plugin-chart-table', () => {
376382 expect ( percentMetric2 ?. originalLabel ) . toBe ( 'metric_2' ) ;
377383 } ) ;
378384
385+ it ( 'should not apply time grain formatting in Raw Records mode' , ( ) => {
386+ const rawRecordsProps = {
387+ ...testData . basic ,
388+ rawFormData : {
389+ ...testData . basic . rawFormData ,
390+ query_mode : QueryMode . Raw ,
391+ time_grain_sqla : TimeGranularity . MONTH ,
392+ table_timestamp_format : SMART_DATE_ID ,
393+ } ,
394+ } ;
395+
396+ const transformedProps = transformProps ( rawRecordsProps ) ;
397+ const timestampColumn = transformedProps . columns . find (
398+ col => col . key === '__timestamp' ,
399+ ) ;
400+
401+ expect ( timestampColumn ) . toBeDefined ( ) ;
402+ const testValue = new Date ( '2023-01-15T10:30:45' ) ;
403+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
404+ const formatted = ( timestampColumn ?. formatter as any ) ?.( testValue ) ;
405+ const granularityFormatted = getTimeFormatterForGranularity (
406+ TimeGranularity . MONTH ,
407+ ) ( testValue as number | Date | null ) ;
408+ expect ( formatted ) . not . toBe ( granularityFormatted ) ;
409+ expect ( typeof formatted ) . toBe ( 'string' ) ;
410+ expect ( formatted ) . toContain ( '2023' ) ;
411+ } ) ;
412+
413+ it ( 'should handle null/undefined timestamp values correctly' , ( ) => {
414+ const rawRecordsProps = {
415+ ...testData . basic ,
416+ rawFormData : {
417+ ...testData . basic . rawFormData ,
418+ query_mode : QueryMode . Raw ,
419+ } ,
420+ } ;
421+
422+ const transformedProps = transformProps ( rawRecordsProps ) ;
423+ expect ( transformedProps . isRawRecords ) . toBe ( true ) ;
424+
425+ const timestampColumn = transformedProps . columns . find (
426+ col => col . key === '__timestamp' ,
427+ ) ;
428+ expect ( timestampColumn ) . toBeDefined ( ) ;
429+ } ) ;
430+
379431 describe ( 'TableChart' , ( ) => {
380432 test ( 'render basic data' , ( ) => {
381433 render (
@@ -385,7 +437,8 @@ describe('plugin-chart-table', () => {
385437 const firstDataRow = screen . getAllByRole ( 'rowgroup' ) [ 1 ] ;
386438 const cells = firstDataRow . querySelectorAll ( 'td' ) ;
387439 expect ( cells ) . toHaveLength ( 12 ) ;
388- expect ( cells [ 0 ] ) . toHaveTextContent ( '2020-01-01 12:34:56' ) ;
440+ // Date is rendered as ISO string format
441+ expect ( cells [ 0 ] ) . toHaveTextContent ( '2020-01-01T12:34:56' ) ;
389442 expect ( cells [ 1 ] ) . toHaveTextContent ( 'Michael' ) ;
390443 // number is not in `metrics` list, so it should output raw value
391444 // (in real world Superset, this would mean the column is used in GROUP BY)
0 commit comments