forked from tomcru/holy-loader
-
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.
Add delay prop to HolyLoader component
Fixes tomcru#2 Add delay prop to HolyLoader component to delay the start of the progress bar. * Add `delay` prop to `HolyLoaderProps` interface in `src/index.tsx` with a default value of 0. * Modify `HolyLoader` component in `src/index.tsx` to use the `delay` prop to delay the start of the progress bar. * Update `useEffect` hook in `src/index.tsx` to handle the delay logic before starting the progress bar. * Add documentation for the new `delay` prop in the API section of `README.md`. * Add tests in `src/__tests__/HolyLoader.test.ts` to verify the functionality of the `delay` prop.
- Loading branch information
Showing
3 changed files
with
79 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import HolyLoader, { startHolyLoader, stopHolyLoader } from '../index'; | ||
|
||
describe('HolyLoader', () => { | ||
it('should start the progress bar after the specified delay', () => { | ||
vi.useFakeTimers(); | ||
const { container } = render(<HolyLoader delay={200} />); | ||
const anchor = document.createElement('a'); | ||
anchor.href = '/test'; | ||
document.body.appendChild(anchor); | ||
|
||
fireEvent.click(anchor); | ||
expect(container.querySelector('#holy-progress')).toBeNull(); | ||
|
||
vi.advanceTimersByTime(200); | ||
expect(container.querySelector('#holy-progress')).not.toBeNull(); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
|
||
it('should not start the progress bar if navigation happens within the delay', () => { | ||
vi.useFakeTimers(); | ||
const { container } = render(<HolyLoader delay={200} />); | ||
const anchor = document.createElement('a'); | ||
anchor.href = '/test'; | ||
document.body.appendChild(anchor); | ||
|
||
fireEvent.click(anchor); | ||
expect(container.querySelector('#holy-progress')).toBeNull(); | ||
|
||
fireEvent.click(anchor); | ||
vi.advanceTimersByTime(200); | ||
expect(container.querySelector('#holy-progress')).toBeNull(); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
|
||
it('should start the progress bar immediately if delay is 0', () => { | ||
const { container } = render(<HolyLoader delay={0} />); | ||
const anchor = document.createElement('a'); | ||
anchor.href = '/test'; | ||
document.body.appendChild(anchor); | ||
|
||
fireEvent.click(anchor); | ||
expect(container.querySelector('#holy-progress')).not.toBeNull(); | ||
}); | ||
|
||
it('should manually start and stop the progress bar', () => { | ||
const { container } = render(<HolyLoader delay={0} />); | ||
|
||
startHolyLoader(); | ||
expect(container.querySelector('#holy-progress')).not.toBeNull(); | ||
|
||
stopHolyLoader(); | ||
expect(container.querySelector('#holy-progress')).toBeNull(); | ||
}); | ||
}); |
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