1+ // Copyright The Perses Authors
2+ // Licensed under the Apache License, Version 2.0 (the "License");
3+ // you may not use this file except in compliance with the License.
4+ // You may obtain a copy of the License at
5+ //
6+ // http://www.apache.org/licenses/LICENSE-2.0
7+ //
8+ // Unless required by applicable law or agreed to in writing, software
9+ // distributed under the License is distributed on an "AS IS" BASIS,
10+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ // See the License for the specific language governing permissions and
12+ // limitations under the License.
13+
14+ import { createTimezoneAwareAxisFormatter } from './timezone-formatter' ;
15+
16+ // Mock formatWithTimeZone since it's from @perses-dev/components
17+ jest . mock ( '@perses-dev/components' , ( ) => ( {
18+ formatWithTimeZone : jest . fn ( ( date : Date , format : string , timeZone : string ) => {
19+ // Simple mock that returns format pattern with timezone
20+ return `${ format } [${ timeZone } ]` ;
21+ } ) ,
22+ } ) ) ;
23+
24+ describe ( 'createTimezoneAwareAxisFormatter' , ( ) => {
25+ const testTimestamp = 1640995200000 ; // 2022-01-01 00:00:00 UTC
26+ const timeZone = 'America/New_York' ;
27+
28+ it ( 'should format for ranges > 5 years with year format' , ( ) => {
29+ const formatter = createTimezoneAwareAxisFormatter ( 6 * 365 * 24 * 60 * 60 * 1000 , timeZone ) ;
30+ const result = formatter ( testTimestamp ) ;
31+ expect ( result ) . toBe ( 'yyyy[America/New_York]' ) ;
32+ } ) ;
33+
34+ it ( 'should format for ranges > 2 years with month-year format' , ( ) => {
35+ const formatter = createTimezoneAwareAxisFormatter ( 3 * 365 * 24 * 60 * 60 * 1000 , timeZone ) ;
36+ const result = formatter ( testTimestamp ) ;
37+ expect ( result ) . toBe ( 'MMM yyyy[America/New_York]' ) ;
38+ } ) ;
39+
40+ it ( 'should format for ranges between 10 days and 6 months with day-month format' , ( ) => {
41+ const formatter = createTimezoneAwareAxisFormatter ( 30 * 24 * 60 * 60 * 1000 , timeZone ) ; // 30 days
42+ const result = formatter ( testTimestamp ) ;
43+ expect ( result ) . toBe ( 'dd.MM[America/New_York]' ) ;
44+ } ) ;
45+
46+ it ( 'should format for ranges between 2-10 days with day-month-time format' , ( ) => {
47+ const formatter = createTimezoneAwareAxisFormatter ( 5 * 24 * 60 * 60 * 1000 , timeZone ) ; // 5 days
48+ const result = formatter ( testTimestamp ) ;
49+ expect ( result ) . toBe ( 'dd.MM HH:mm[America/New_York]' ) ;
50+ } ) ;
51+
52+ it ( 'should format for ranges <= 2 days with time format' , ( ) => {
53+ const formatter = createTimezoneAwareAxisFormatter ( 6 * 60 * 60 * 1000 , timeZone ) ; // 6 hours
54+ const result = formatter ( testTimestamp ) ;
55+ expect ( result ) . toBe ( 'HH:mm[America/New_York]' ) ;
56+ } ) ;
57+
58+ it ( 'should handle different timezones' , ( ) => {
59+ const formatter = createTimezoneAwareAxisFormatter ( 6 * 60 * 60 * 1000 , 'Europe/Prague' ) ;
60+ const result = formatter ( testTimestamp ) ;
61+ expect ( result ) . toBe ( 'HH:mm[Europe/Prague]' ) ;
62+ } ) ;
63+
64+ it ( 'should handle edge case at exactly 5 years' , ( ) => {
65+ const fiveYears = 5 * 365 * 24 * 60 * 60 * 1000 ;
66+ const formatter = createTimezoneAwareAxisFormatter ( fiveYears , timeZone ) ;
67+ const result = formatter ( testTimestamp ) ;
68+ // Should use MMM yyyy format (not > 5 years)
69+ expect ( result ) . toBe ( 'MMM yyyy[America/New_York]' ) ;
70+ } ) ;
71+
72+ it ( 'should handle edge case at exactly 2 days' , ( ) => {
73+ const twoDays = 2 * 24 * 60 * 60 * 1000 ;
74+ const formatter = createTimezoneAwareAxisFormatter ( twoDays , timeZone ) ;
75+ const result = formatter ( testTimestamp ) ;
76+ // Should use HH:mm format (not > 2 days)
77+ expect ( result ) . toBe ( 'HH:mm[America/New_York]' ) ;
78+ } ) ;
79+ } ) ;
0 commit comments