-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
|
||
import { useIntlContext } from './intl.provider'; | ||
|
||
type UseNowOptions = { | ||
updateInterval?: number; | ||
}; | ||
|
||
export function useNow(options?: UseNowOptions) { | ||
const updateInterval = options?.updateInterval; | ||
|
||
const { now: globalNow } = useIntlContext(); | ||
const [now, setNow] = React.useState(globalNow || new Date()); | ||
|
||
React.useEffect(() => { | ||
if (Boolean(updateInterval) === false) { | ||
return undefined; | ||
} | ||
|
||
const intervalId = setInterval(() => { | ||
setNow(new Date()); | ||
}, updateInterval); | ||
|
||
return () => { | ||
clearInterval(intervalId); | ||
}; | ||
}, [updateInterval]); | ||
|
||
return now; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { render, waitFor } from '@testing-library/react'; | ||
import { expect, it } from 'vitest'; | ||
|
||
import { IntlProvider } from '../src/intl.provider'; | ||
import { useNow } from '../src/use-now'; | ||
|
||
it('returns the current time', () => { | ||
function Component() { | ||
return <p>{useNow().toISOString()}</p>; | ||
} | ||
|
||
const { container } = render( | ||
<IntlProvider locale="en"> | ||
<Component /> | ||
</IntlProvider>, | ||
); | ||
expect(container.textContent).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/); | ||
}); | ||
|
||
it('can use a globally defined `now` value', () => { | ||
function Component() { | ||
return <p>{useNow().toISOString()}</p>; | ||
} | ||
|
||
const { container } = render( | ||
<IntlProvider locale="en" now={new Date('2018-10-06T10:36:01.516Z')}> | ||
<Component /> | ||
</IntlProvider>, | ||
); | ||
expect(container.textContent).toBe('2018-10-06T10:36:01.516Z'); | ||
}); | ||
|
||
it('can update a globally defined `now` value after the initial render', async () => { | ||
function Component() { | ||
return <p>{useNow({ updateInterval: 100 }).toISOString()}</p>; | ||
} | ||
|
||
const { container } = render( | ||
<IntlProvider locale="en" now={new Date('2018-10-06T10:36:01.516Z')}> | ||
<Component /> | ||
</IntlProvider>, | ||
); | ||
expect(container.textContent).toBe('2018-10-06T10:36:01.516Z'); | ||
|
||
await waitFor(() => { | ||
if (!container.textContent) throw new Error(); | ||
const curYear = parseInt(container.textContent); | ||
|
||
expect(curYear).toBeGreaterThan(2020); | ||
}); | ||
}); | ||
|
||
it('can update based on an interval', async () => { | ||
function Component() { | ||
return <p>{useNow({ updateInterval: 100 }).toISOString()}</p>; | ||
} | ||
|
||
const { container } = render( | ||
<IntlProvider locale="en"> | ||
<Component /> | ||
</IntlProvider>, | ||
); | ||
if (!container.textContent) throw new Error(); | ||
const initial = new Date(container.textContent).getTime(); | ||
|
||
await waitFor(() => { | ||
if (!container.textContent) throw new Error(); | ||
const now = new Date(container.textContent).getTime(); | ||
expect(now - 900).toBeGreaterThanOrEqual(initial); | ||
expect(4).toBeGreaterThanOrEqual(3); | ||
}); | ||
}); |