Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ const App = () => {
};
```

### Two and more timers

```tsx
import React from 'react';
import { useTimer } from 'use-timer';

const App = () => {
const { time: firstTimer, start: startFirstTimer, pause: pauseFirstTimer } = useTimer();
const { time: secondTimer, start: startSecondTimer, pause: pauseSecondTimer } = useTimer();

return (
...
);
};
```

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is not related to the rest of the PR. I would advise against it.

### Decremental timer

```tsx
Expand Down Expand Up @@ -113,13 +129,13 @@ const { time, start, pause, reset, status } = useTimer({

The configuration and all its parameters are optional.

| Property | Type | Default value | Description |
| ------------ | -------- | ------------- | -------------------------------------------------------------------------------------- |
| autostart | boolean | false | Pass true to start timer automatically |
| endTime | number | null | The value for which timer stops |
| initialTime | number | 0 | The starting value for the timer |
| interval | number | 1000 | The interval in milliseconds |
| onTimeOver | function | | Callback function that is called when time is over |
| onTimeUpdate | function | | Callback function that is called when time is updated |
| step | number | 1 | The value to add to each increment / decrement |
| timerType | string | "INCREMENTAL" | The choice between a value that increases ("INCREMENTAL") or decreases ("DECREMENTAL") |
| Property | Type | Default value | Description |
| ------------ | -------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------|
| autostart | boolean | false | Pass true to start timer automatically |
| endTime | number | null || 0 | The value for which timer stops. Null by default if timerType is "INCREMENTAL" and 0 by default if timerType is "DECREMENTAL" |
| initialTime | number | 0 | The starting value for the timer |
| interval | number | 1000 | The interval in milliseconds |
| onTimeOver | function | | Callback function that is called when time is over |
| onTimeUpdate | function | | Callback function that is called when time is updated |
| step | number | 1 | The value to add to each increment / decrement |
| timerType | string | "INCREMENTAL" | The choice between a value that increases ("INCREMENTAL") or decreases ("DECREMENTAL") |
29 changes: 29 additions & 0 deletions src/useTimer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ describe('Stop', () => {
// Then
expect(getByTestId('time').textContent).toBe('10');
});

it('should stop decremental timer when time is 0', () => {
// Given
const Component = () => {
const { time, start } = useTimer({
initialTime: 30,
timerType: 'DECREMENTAL',
});

return (
<div>
<button onClick={start}>Start</button>
<p data-testid="time">{time}</p>
</div>
);
};

const { getByRole, getByTestId } = render(<Component />);

// When
fireEvent.click(getByRole('button'));

act(() => {
jest.advanceTimersByTime(30000);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to advance time by more than 30000 to really test that the timer will stop at 0. I would replace 30000 by 40000 for example.

});

// Then
expect(getByTestId('time').textContent).toBe('0');
});
});

describe('Pause', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/useTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import reducer from './state/reducer';

export const useTimer = ({
autostart = false,
endTime,
timerType = 'INCREMENTAL',
endTime = timerType === 'INCREMENTAL' ? null : 0,
initialTime = 0,
interval = 1000,
onTimeOver,
onTimeUpdate,
step = 1,
timerType = 'INCREMENTAL',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the alphabetical order of properties, to keep the author's logic.

}: Partial<Config> = {}): ReturnValue => {
const [state, dispatch] = useReducer(reducer, {
status: 'STOPPED',
Expand Down