-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Test navigation via URI fragment (#1188)
* Use test index as URI fragment with TestRun * Update test navigator to use correct URLs * Update test navigator * Handle tests that are test indexes beyond max * e2e test for clamping of the test index to the bounds of the test list * Try catch for ScrollFixer querySelector
- Loading branch information
Showing
9 changed files
with
160 additions
and
81 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,33 @@ | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const useUrlTestIndex = maxTestIndex => { | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
const [currentTestIndex, setCurrentTestIndex] = useState(0); | ||
|
||
const getTestIndex = () => { | ||
// Remove the '#' character | ||
const fragment = parseInt(location.hash.slice(1), 10) || 1; | ||
|
||
if (!maxTestIndex || maxTestIndex < 1) { | ||
// If the max test index is less than 1, return 0 | ||
return 0; | ||
} | ||
// Subtract 1 to make it 0-indexed | ||
// and clamp to the max test index | ||
return Math.max(0, Math.min(fragment - 1, maxTestIndex - 1)); | ||
}; | ||
|
||
useEffect(() => { | ||
setCurrentTestIndex(getTestIndex()); | ||
}, [location.hash, maxTestIndex]); | ||
|
||
const updateTestIndex = index => { | ||
// Add 1 to make it 1-indexed | ||
const newIndex = index + 1; | ||
navigate(`${location.pathname}#${newIndex}`, { replace: true }); | ||
}; | ||
|
||
return [currentTestIndex, updateTestIndex]; | ||
}; |
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
Oops, something went wrong.