@@ -2,9 +2,11 @@ import { describe, it, expect, beforeEach } from 'vitest';
22import {
33 getNumberFormat ,
44 getDateTimeFormat ,
5+ getRelativeTimeFormat ,
56 clearIntlCache ,
67 getNumberFormatCacheSize ,
78 getDateTimeFormatCacheSize ,
9+ getRelativeTimeFormatCacheSize ,
810} from '../intlCache' ;
911
1012describe ( 'intlCache' , ( ) => {
@@ -58,6 +60,26 @@ describe('intlCache', () => {
5860 } ) ;
5961 } ) ;
6062
63+ describe ( 'getRelativeTimeFormat' , ( ) => {
64+ it ( 'returns a valid Intl.RelativeTimeFormat instance' , ( ) => {
65+ const formatter = getRelativeTimeFormat ( 'en-US' , { numeric : 'auto' } ) ;
66+ expect ( formatter ) . toBeInstanceOf ( Intl . RelativeTimeFormat ) ;
67+ expect ( formatter . format ( - 1 , 'day' ) ) . toBe ( 'yesterday' ) ;
68+ } ) ;
69+
70+ it ( 'caches formatters by locale and options' , ( ) => {
71+ const formatter1 = getRelativeTimeFormat ( 'en-US' , { numeric : 'auto' } ) ;
72+ const formatter2 = getRelativeTimeFormat ( 'en-US' , { numeric : 'auto' } ) ;
73+ expect ( formatter1 ) . toBe ( formatter2 ) ;
74+ } ) ;
75+
76+ it ( 'creates separate formatters for different locales' , ( ) => {
77+ const formatterEN = getRelativeTimeFormat ( 'en-US' , { numeric : 'auto' } ) ;
78+ const formatterFR = getRelativeTimeFormat ( 'fr-FR' , { numeric : 'auto' } ) ;
79+ expect ( formatterEN ) . not . toBe ( formatterFR ) ;
80+ } ) ;
81+ } ) ;
82+
6183 describe ( 'cache sizes' , ( ) => {
6284 it ( 'returns correct number format cache size' , ( ) => {
6385 expect ( getNumberFormatCacheSize ( ) ) . toBe ( 0 ) ;
@@ -72,14 +94,23 @@ describe('intlCache', () => {
7294 expect ( getDateTimeFormatCacheSize ( ) ) . toBe ( 1 ) ;
7395 } ) ;
7496
75- it ( 'clearIntlCache clears both caches' , ( ) => {
97+ it ( 'returns correct relative time format cache size' , ( ) => {
98+ expect ( getRelativeTimeFormatCacheSize ( ) ) . toBe ( 0 ) ;
99+ getRelativeTimeFormat ( 'en-US' ) ;
100+ expect ( getRelativeTimeFormatCacheSize ( ) ) . toBe ( 1 ) ;
101+ } ) ;
102+
103+ it ( 'clearIntlCache clears all caches' , ( ) => {
76104 getNumberFormat ( 'en-US' ) ;
77105 getDateTimeFormat ( 'en-US' ) ;
106+ getRelativeTimeFormat ( 'en-US' ) ;
78107 expect ( getNumberFormatCacheSize ( ) ) . toBe ( 1 ) ;
79108 expect ( getDateTimeFormatCacheSize ( ) ) . toBe ( 1 ) ;
109+ expect ( getRelativeTimeFormatCacheSize ( ) ) . toBe ( 1 ) ;
80110 clearIntlCache ( ) ;
81111 expect ( getNumberFormatCacheSize ( ) ) . toBe ( 0 ) ;
82112 expect ( getDateTimeFormatCacheSize ( ) ) . toBe ( 0 ) ;
113+ expect ( getRelativeTimeFormatCacheSize ( ) ) . toBe ( 0 ) ;
83114 } ) ;
84115 } ) ;
85116} ) ;
0 commit comments