Skip to content

Commit 8178d3a

Browse files
committed
main 🧊 timer on tick fix
1 parent 2934ddd commit 8178d3a

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

‎packages/core/src/bundle/hooks/useTimer/useTimer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export const useTimer = (...params) => {
6363
if (!active) return;
6464
optionsRef.current?.onStart?.();
6565
const onInterval = () => {
66-
optionsRef.current?.onTick?.(seconds);
6766
setSeconds((prevSeconds) => {
67+
optionsRef.current?.onTick?.(prevSeconds);
6868
const updatedSeconds = prevSeconds - 1;
6969
if (updatedSeconds === 0) {
7070
setActive(false);

‎packages/core/src/hooks/useTimer/useTimer.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,17 @@ it('Should call onStart when timer starts', () => {
174174

175175
it('Should call onTick on each second', () => {
176176
const onTick = vi.fn();
177-
renderHook(() => useTimer(1, { onTick }));
177+
renderHook(() => useTimer(2, { onTick }));
178178

179179
act(() => vi.advanceTimersToNextTimer());
180180

181181
expect(onTick).toBeCalledTimes(1);
182+
expect(onTick).toBeCalledWith(2);
183+
184+
act(() => vi.advanceTimersToNextTimer());
185+
186+
expect(onTick).toBeCalledTimes(2);
187+
expect(onTick).toBeCalledWith(1);
182188
});
183189

184190
it('Should pause timer', () => {

‎packages/core/src/hooks/useTimer/useTimer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ export const useTimer = ((...params: any[]) => {
131131

132132
optionsRef.current?.onStart?.();
133133
const onInterval = () => {
134-
optionsRef.current?.onTick?.(seconds);
135134
setSeconds((prevSeconds) => {
135+
optionsRef.current?.onTick?.(prevSeconds);
136136
const updatedSeconds = prevSeconds - 1;
137137
if (updatedSeconds === 0) {
138138
setActive(false);

0 commit comments

Comments
 (0)