33import { render } from "@testing-library/react" ;
44import { vi } from "vitest" ;
55
6- import { I18NContext , LocalizedMessageKey , Messages } from "../i18n/index.ts" ;
76import { Paused , Started , Total } from "./timings.tsx" ;
87
98describe ( "Timings" , ( ) => {
10- const translations = new Messages (
11- {
12- [ LocalizedMessageKey . year ] : "{0} yr" ,
13- [ LocalizedMessageKey . month ] : "{0} mo" ,
14- [ LocalizedMessageKey . day ] : "{0} day" ,
15- [ LocalizedMessageKey . hour ] : "{0} hr" ,
16- [ LocalizedMessageKey . minute ] : "{0} min" ,
17- [ LocalizedMessageKey . second ] : "{0} sec" ,
18- [ LocalizedMessageKey . millisecond ] : "{0} ms" ,
19- [ LocalizedMessageKey . startedAgo ] : "Started {0} ago" ,
20- } ,
21- "en" ,
22- ) ;
23-
24- function process ( child : any ) {
25- return render (
26- < I18NContext . Provider value = { translations } > { child } </ I18NContext . Provider > ,
27- ) ;
28- }
29-
309 describe ( "Total" , ( ) => {
3110 function getTotal ( ms : number ) {
32- return process ( < Total ms = { ms } /> ) ;
11+ return render ( < Total ms = { ms } /> ) ;
3312 }
3413
3514 it ( "should format milliseconds to hours, minutes, and seconds" , ( ) => {
3615 // First check 359 days.
37- expect ( getTotal ( 31_017_600_000 ) . getByText ( "11 mo " ) ) . toBeInTheDocument ( ) ;
16+ expect ( getTotal ( 31_017_600_000 ) . getByText ( "11 mths " ) ) . toBeInTheDocument ( ) ;
3817 // And 362 days.
39- expect ( getTotal ( 31_276_800_000 ) . getByText ( "12 mo " ) ) . toBeInTheDocument ( ) ;
18+ expect ( getTotal ( 31_276_800_000 ) . getByText ( "12 mths " ) ) . toBeInTheDocument ( ) ;
4019 // 11.25 years - Check that if the first unit has 2 or more digits, a second unit isn't used.
41- expect ( getTotal ( 354_780_000_000 ) . getByText ( "11 yr " ) ) . toBeInTheDocument ( ) ;
20+ expect ( getTotal ( 354_780_000_000 ) . getByText ( "11y " ) ) . toBeInTheDocument ( ) ;
4221 // 9.25 years - Check that if the first unit has only 1 digit, a second unit is used.
43- expect (
44- getTotal ( 291_708_000_000 ) . getByText ( "9 yr 3 mo" ) ,
45- ) . toBeInTheDocument ( ) ;
22+ expect ( getTotal ( 291_708_000_000 ) . getByText ( "9y 3m" ) ) . toBeInTheDocument ( ) ;
4623 // 3 months 14 days
47- expect (
48- getTotal ( 8_985_600_000 ) . getByText ( "3 mo 14 day" ) ,
49- ) . toBeInTheDocument ( ) ;
24+ expect ( getTotal ( 8_985_600_000 ) . getByText ( "3m 14d" ) ) . toBeInTheDocument ( ) ;
5025 // 2 day 4 hours
51- expect ( getTotal ( 187_200_000 ) . getByText ( "2 day 4 hr " ) ) . toBeInTheDocument ( ) ;
26+ expect ( getTotal ( 187_200_000 ) . getByText ( "2d 4h " ) ) . toBeInTheDocument ( ) ;
5227 // 8 hours 46 minutes
53- expect ( getTotal ( 31_560_000 ) . getByText ( "8 hr 46 min " ) ) . toBeInTheDocument ( ) ;
28+ expect ( getTotal ( 31_560_000 ) . getByText ( "8h 46m " ) ) . toBeInTheDocument ( ) ;
5429 // 67 seconds -> 1 minute 7 seconds
55- expect ( getTotal ( 67_000 ) . getByText ( "1 min 7 sec " ) ) . toBeInTheDocument ( ) ;
30+ expect ( getTotal ( 67_000 ) . getByText ( "1m 7s " ) ) . toBeInTheDocument ( ) ;
5631 // 17 seconds - Check that times less than a minute only use seconds.
57- expect ( getTotal ( 17_000 ) . getByText ( "17 sec " ) ) . toBeInTheDocument ( ) ;
32+ expect ( getTotal ( 17_000 ) . getByText ( "17s " ) ) . toBeInTheDocument ( ) ;
5833 // 1712ms -> 1.7sec
59- expect ( getTotal ( 1_712 ) . getByText ( "1.7 sec " ) ) . toBeInTheDocument ( ) ;
34+ expect ( getTotal ( 1_712 ) . getByText ( "1.7s " ) ) . toBeInTheDocument ( ) ;
6035 // 171ms -> 0.17sec
61- expect ( getTotal ( 171 ) . getByText ( "0.17 sec " ) ) . toBeInTheDocument ( ) ;
62- // 101ms -> 0.10sec
63- expect ( getTotal ( 101 ) . getByText ( "0.1 sec " ) ) . toBeInTheDocument ( ) ;
36+ expect ( getTotal ( 171 ) . getByText ( "0.17s " ) ) . toBeInTheDocument ( ) ;
37+ // 101ms -> 0.1sec
38+ expect ( getTotal ( 101 ) . getByText ( "0.1s " ) ) . toBeInTheDocument ( ) ;
6439 // 17ms
65- expect ( getTotal ( 17 ) . getByText ( "17 ms " ) ) . toBeInTheDocument ( ) ;
40+ expect ( getTotal ( 17 ) . getByText ( "17ms " ) ) . toBeInTheDocument ( ) ;
6641 // 1ms
67- expect ( getTotal ( 1 ) . getByText ( "1 ms " ) ) . toBeInTheDocument ( ) ;
42+ expect ( getTotal ( 1 ) . getByText ( "1ms " ) ) . toBeInTheDocument ( ) ;
6843 } ) ;
6944 } ) ;
7045
7146 describe ( "paused" , ( ) => {
7247 function getPaused ( since : number ) {
73- return process ( < Paused since = { since } /> ) ;
48+ return render ( < Paused since = { since } /> ) ;
7449 }
7550
7651 it ( "should prefix the time with Queued" , ( ) => {
77- expect ( getPaused ( 1000 ) . getByText ( "Queued 1 sec " ) ) . toBeInTheDocument ( ) ;
78- expect ( getPaused ( 100 ) . getByText ( "Queued 0.1 sec " ) ) . toBeInTheDocument ( ) ;
79- expect ( getPaused ( 10 ) . getByText ( "Queued 10 ms " ) ) . toBeInTheDocument ( ) ;
80- expect ( getPaused ( 1 ) . getByText ( "Queued 1 ms " ) ) . toBeInTheDocument ( ) ;
52+ expect ( getPaused ( 1000 ) . getByText ( "Queued 1s " ) ) . toBeInTheDocument ( ) ;
53+ expect ( getPaused ( 100 ) . getByText ( "Queued 0.1s " ) ) . toBeInTheDocument ( ) ;
54+ expect ( getPaused ( 10 ) . getByText ( "Queued 10ms " ) ) . toBeInTheDocument ( ) ;
55+ expect ( getPaused ( 1 ) . getByText ( "Queued 1ms " ) ) . toBeInTheDocument ( ) ;
8156 } ) ;
8257 } ) ;
8358
@@ -93,7 +68,7 @@ describe("Timings", () => {
9368 } ) ;
9469
9570 function getStarted ( since : number ) {
96- return process ( < Started since = { since } /> ) ;
71+ return render ( < Started since = { since } /> ) ;
9772 }
9873
9974 it ( "should return empty element if since is 0" , ( ) => {
@@ -102,16 +77,16 @@ describe("Timings", () => {
10277
10378 it ( "should prefix the time with Started and end with ago" , ( ) => {
10479 expect (
105- getStarted ( now - 1000 ) . getByText ( "Started 1 sec ago" ) ,
80+ getStarted ( now - 1000 ) . getByText ( "Started 1s ago" ) ,
10681 ) . toBeInTheDocument ( ) ;
10782 expect (
108- getStarted ( now - 100 ) . getByText ( "Started 0.1 sec ago" ) ,
83+ getStarted ( now - 100 ) . getByText ( "Started 0.1s ago" ) ,
10984 ) . toBeInTheDocument ( ) ;
11085 expect (
111- getStarted ( now - 10 ) . getByText ( "Started 10 ms ago" ) ,
86+ getStarted ( now - 10 ) . getByText ( "Started 10ms ago" ) ,
11287 ) . toBeInTheDocument ( ) ;
11388 expect (
114- getStarted ( now - 1 ) . getByText ( "Started 1 ms ago" ) ,
89+ getStarted ( now - 1 ) . getByText ( "Started 1ms ago" ) ,
11590 ) . toBeInTheDocument ( ) ;
11691 } ) ;
11792 } ) ;
0 commit comments