From 0c9ffc7767b033c6ee75682f1abc5f4ecf3e3a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 17 Jul 2023 14:07:16 +0200 Subject: [PATCH] Fix timer button rolling over after 24h (#13148) * Fix timer button rolling over after 24h * Use padStart function. --------- Co-authored-by: Dombi Attila <83396+dombesz@users.noreply.github.com> --- .../wp-timer-button/time-formatter.helper.ts | 16 ++++++++++++++-- modules/costs/spec/features/timer_spec.rb | 14 ++++++++++++++ .../components/work_packages/timer_button.rb | 4 ++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/frontend/src/app/features/work-packages/components/wp-timer-button/time-formatter.helper.ts b/frontend/src/app/features/work-packages/components/wp-timer-button/time-formatter.helper.ts index 4cef46442635..dfb18f501a65 100644 --- a/frontend/src/app/features/work-packages/components/wp-timer-button/time-formatter.helper.ts +++ b/frontend/src/app/features/work-packages/components/wp-timer-button/time-formatter.helper.ts @@ -1,9 +1,21 @@ import * as moment from 'moment/moment'; +function paddedNumber(input:number):string { + return input.toString().padStart(2, '0'); +} + export function formatElapsedTime(startTime:string):string { const start = moment(startTime); const now = moment(); - const offset = moment(now).diff(start); + const duration = now.diff(start, 'seconds'); + + const hours = Math.floor(duration / 3600); + const minutes = Math.floor((duration - (hours * 3600)) / 60); + const seconds = duration - (hours * 3600) - (minutes * 60); - return moment.utc(offset).format('HH:mm:ss'); + return [ + paddedNumber(hours), + paddedNumber(minutes), + paddedNumber(seconds), + ].join(':'); } diff --git a/modules/costs/spec/features/timer_spec.rb b/modules/costs/spec/features/timer_spec.rb index 54c313616b1e..5c4ce8447bd0 100644 --- a/modules/costs/spec/features/timer_spec.rb +++ b/modules/costs/spec/features/timer_spec.rb @@ -130,6 +130,20 @@ it_behaves_like 'allows time tracking' + context 'when an old timer exists' do + let!(:active_timer) do + Timecop.travel(2.days.ago) do + create(:time_entry, project:, work_package: work_package_a, user:, ongoing: true) + end + end + + it 'correctly shows active timers > 24 hours' do + wp_view_a.visit! + timer_button.expect_visible + timer_button.expect_time /48:0\d:\d+/ + end + end + it 'correctly handles timers in multiple tabs' do wp_view_a.visit! timer_button.expect_visible diff --git a/spec/support/components/work_packages/timer_button.rb b/spec/support/components/work_packages/timer_button.rb index bba258c17bcd..8f64550ba406 100644 --- a/spec/support/components/work_packages/timer_button.rb +++ b/spec/support/components/work_packages/timer_button.rb @@ -41,6 +41,10 @@ def expect_inactive expect(page).to have_selector('[data-qa-selector="timer-inactive"]', wait: 10) end + def expect_time(text) + expect(page).to have_selector('[data-qa-selector="timer-active"]', wait: 10, text:) + end + def expect_visible(visible: true) if visible expect(page).to have_selector('op-wp-timer-button')