-
Notifications
You must be signed in to change notification settings - Fork 165
fix(auth, a11y): oauth redirect validation and focus-trap test typing #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Junman140
merged 2 commits into
Pi-Defi-world:dev
from
meetdarc-tech:fix/oauth-redirect-validation-focus-trap-typing
Aug 31, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,192 +1,207 @@ | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' | ||
| import { renderHook, act } from '@testing-library/react' | ||
| import { useFocusTrap } from '../use-focus-trap' | ||
| import React from 'react' | ||
| import { describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
| import { renderHook, act } from "@testing-library/react"; | ||
| import { useFocusTrap } from "../use-focus-trap"; | ||
| import React from "react"; | ||
|
|
||
| describe('useFocusTrap', () => { | ||
| let container: HTMLDivElement | ||
| describe("useFocusTrap", () => { | ||
| let container: HTMLDivElement; | ||
|
|
||
| beforeEach(() => { | ||
| // Create a test container with focusable elements | ||
| container = document.createElement('div') | ||
| container = document.createElement("div"); | ||
| container.innerHTML = ` | ||
| <button id="button-1">Button 1</button> | ||
| <input id="input-1" type="text" /> | ||
| <a id="link-1" href="#">Link 1</a> | ||
| <button id="button-2">Button 2</button> | ||
| ` | ||
| document.body.appendChild(container) | ||
| }) | ||
| `; | ||
| document.body.appendChild(container); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| document.body.removeChild(container) | ||
| }) | ||
| document.body.removeChild(container); | ||
| }); | ||
|
|
||
| it('traps focus on Tab from last element to first', () => { | ||
| const containerRef = React.createRef<HTMLDivElement>() | ||
| // Mock the ref | ||
| ;(containerRef as any).current = container | ||
| it("traps focus on Tab from last element to first", () => { | ||
| const containerRef: React.RefObject<HTMLDivElement> = { | ||
| current: container, | ||
| }; | ||
|
|
||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })) | ||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })); | ||
|
|
||
| const lastButton = container.querySelector('#button-2') as HTMLButtonElement | ||
| lastButton.focus() | ||
| expect(document.activeElement).toBe(lastButton) | ||
| const lastButton = container.querySelector( | ||
| "#button-2", | ||
| ) as HTMLButtonElement; | ||
| lastButton.focus(); | ||
| expect(document.activeElement).toBe(lastButton); | ||
|
|
||
| // Simulate Tab key on last element | ||
| const event = new KeyboardEvent('keydown', { | ||
| key: 'Tab', | ||
| const event = new KeyboardEvent("keydown", { | ||
| key: "Tab", | ||
| bubbles: true, | ||
| }) | ||
| }); | ||
| act(() => { | ||
| container.dispatchEvent(event) | ||
| }) | ||
| container.dispatchEvent(event); | ||
| }); | ||
|
|
||
| // Focus should wrap to first element | ||
| const firstButton = container.querySelector('#button-1') as HTMLButtonElement | ||
| expect(document.activeElement).toBe(firstButton) | ||
| }) | ||
| const firstButton = container.querySelector( | ||
| "#button-1", | ||
| ) as HTMLButtonElement; | ||
| expect(document.activeElement).toBe(firstButton); | ||
| }); | ||
|
|
||
| it('traps focus on Shift+Tab from first element to last', () => { | ||
| const containerRef = React.createRef<HTMLDivElement>() | ||
| ;(containerRef as any).current = container | ||
| it("traps focus on Shift+Tab from first element to last", () => { | ||
| const containerRef: React.RefObject<HTMLDivElement> = { | ||
| current: container, | ||
| }; | ||
|
|
||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })) | ||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })); | ||
|
|
||
| const firstButton = container.querySelector('#button-1') as HTMLButtonElement | ||
| firstButton.focus() | ||
| expect(document.activeElement).toBe(firstButton) | ||
| const firstButton = container.querySelector( | ||
| "#button-1", | ||
| ) as HTMLButtonElement; | ||
| firstButton.focus(); | ||
| expect(document.activeElement).toBe(firstButton); | ||
|
|
||
| // Simulate Shift+Tab key on first element | ||
| const event = new KeyboardEvent('keydown', { | ||
| key: 'Tab', | ||
| const event = new KeyboardEvent("keydown", { | ||
| key: "Tab", | ||
| shiftKey: true, | ||
| bubbles: true, | ||
| }) | ||
| }); | ||
| act(() => { | ||
| container.dispatchEvent(event) | ||
| }) | ||
| container.dispatchEvent(event); | ||
| }); | ||
|
|
||
| // Focus should wrap to last element | ||
| const lastButton = container.querySelector('#button-2') as HTMLButtonElement | ||
| expect(document.activeElement).toBe(lastButton) | ||
| }) | ||
| const lastButton = container.querySelector( | ||
| "#button-2", | ||
| ) as HTMLButtonElement; | ||
| expect(document.activeElement).toBe(lastButton); | ||
| }); | ||
|
|
||
| it('does not trap focus when inactive', () => { | ||
| const containerRef = React.createRef<HTMLDivElement>() | ||
| ;(containerRef as any).current = container | ||
| it("does not trap focus when inactive", () => { | ||
| const containerRef: React.RefObject<HTMLDivElement> = { | ||
| current: container, | ||
| }; | ||
|
|
||
| renderHook(() => useFocusTrap(containerRef, { isActive: false })) | ||
| renderHook(() => useFocusTrap(containerRef, { isActive: false })); | ||
|
|
||
| const lastButton = container.querySelector('#button-2') as HTMLButtonElement | ||
| lastButton.focus() | ||
| const lastButton = container.querySelector( | ||
| "#button-2", | ||
| ) as HTMLButtonElement; | ||
| lastButton.focus(); | ||
|
|
||
| // Simulate Tab key | ||
| const event = new KeyboardEvent('keydown', { | ||
| key: 'Tab', | ||
| const event = new KeyboardEvent("keydown", { | ||
| key: "Tab", | ||
| bubbles: true, | ||
| }) | ||
| }); | ||
|
|
||
| // Should not prevent default when inactive | ||
| expect(event.defaultPrevented).toBe(false) | ||
| }) | ||
| expect(event.defaultPrevented).toBe(false); | ||
| }); | ||
|
|
||
| it('finds various focusable element types', () => { | ||
| const containerRef = React.createRef<HTMLDivElement>() | ||
| ;(containerRef as any).current = container | ||
| it("finds various focusable element types", () => { | ||
| const containerRef: React.RefObject<HTMLDivElement> = { | ||
| current: container, | ||
| }; | ||
|
|
||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })) | ||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })); | ||
|
|
||
| // Container has button, input, link, button - all focusable | ||
| // If we can Tab to different elements, the focus trap is finding them | ||
| expect(true).toBe(true) // Placeholder assertion | ||
| }) | ||
| expect(true).toBe(true); // Placeholder assertion | ||
| }); | ||
|
|
||
| it('ignores disabled elements', () => { | ||
| const disabledContainer = document.createElement('div') | ||
| it("ignores disabled elements", () => { | ||
| const disabledContainer = document.createElement("div"); | ||
| disabledContainer.innerHTML = ` | ||
| <button id="disabled-button-1">Button 1</button> | ||
| <button id="disabled-button-2" disabled>Button 2 (Disabled)</button> | ||
| <button id="disabled-button-3">Button 3</button> | ||
| ` | ||
| document.body.appendChild(disabledContainer) | ||
| `; | ||
| document.body.appendChild(disabledContainer); | ||
|
|
||
| const containerRef = React.createRef<HTMLDivElement>() | ||
| ;(containerRef as any).current = disabledContainer | ||
| const containerRef: React.RefObject<HTMLDivElement> = { | ||
| current: disabledContainer, | ||
| }; | ||
|
|
||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })) | ||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })); | ||
|
|
||
| const firstButton = disabledContainer.querySelector( | ||
| '#disabled-button-1', | ||
| ) as HTMLButtonElement | ||
| "#disabled-button-1", | ||
| ) as HTMLButtonElement; | ||
| const thirdButton = disabledContainer.querySelector( | ||
| '#disabled-button-3', | ||
| ) as HTMLButtonElement | ||
| "#disabled-button-3", | ||
| ) as HTMLButtonElement; | ||
|
|
||
| if (!firstButton || !thirdButton) { | ||
| document.body.removeChild(disabledContainer) | ||
| return | ||
| document.body.removeChild(disabledContainer); | ||
| return; | ||
| } | ||
|
|
||
| firstButton.focus() | ||
| firstButton.focus(); | ||
|
|
||
| // Simulate Shift+Tab from first to wrap around | ||
| const event = new KeyboardEvent('keydown', { | ||
| key: 'Tab', | ||
| const event = new KeyboardEvent("keydown", { | ||
| key: "Tab", | ||
| shiftKey: true, | ||
| bubbles: true, | ||
| }) | ||
| }); | ||
| act(() => { | ||
| disabledContainer.dispatchEvent(event) | ||
| }) | ||
| disabledContainer.dispatchEvent(event); | ||
| }); | ||
|
|
||
| // Should skip disabled button and focus on the last focusable (button-3) | ||
| expect(document.activeElement).toBe(thirdButton) | ||
| expect(document.activeElement).toBe(thirdButton); | ||
|
|
||
| document.body.removeChild(disabledContainer) | ||
| }) | ||
| document.body.removeChild(disabledContainer); | ||
| }); | ||
|
|
||
| it('ignores hidden elements', () => { | ||
| const hiddenContainer = document.createElement('div') | ||
| it("ignores hidden elements", () => { | ||
| const hiddenContainer = document.createElement("div"); | ||
| hiddenContainer.innerHTML = ` | ||
| <button id="hidden-button-1">Button 1</button> | ||
| <button id="hidden-button-2" style="display: none;">Button 2 (Hidden)</button> | ||
| <button id="hidden-button-3">Button 3</button> | ||
| ` | ||
| document.body.appendChild(hiddenContainer) | ||
| `; | ||
| document.body.appendChild(hiddenContainer); | ||
|
|
||
| const containerRef = React.createRef<HTMLDivElement>() | ||
| ;(containerRef as any).current = hiddenContainer | ||
| const containerRef: React.RefObject<HTMLDivElement> = { | ||
| current: hiddenContainer, | ||
| }; | ||
|
|
||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })) | ||
| renderHook(() => useFocusTrap(containerRef, { isActive: true })); | ||
|
|
||
| const firstButton = hiddenContainer.querySelector( | ||
| '#hidden-button-1', | ||
| ) as HTMLButtonElement | ||
| "#hidden-button-1", | ||
| ) as HTMLButtonElement; | ||
| const thirdButton = hiddenContainer.querySelector( | ||
| '#hidden-button-3', | ||
| ) as HTMLButtonElement | ||
| "#hidden-button-3", | ||
| ) as HTMLButtonElement; | ||
|
|
||
| if (!firstButton || !thirdButton) { | ||
| document.body.removeChild(hiddenContainer) | ||
| return | ||
| document.body.removeChild(hiddenContainer); | ||
| return; | ||
| } | ||
|
|
||
| firstButton.focus() | ||
| firstButton.focus(); | ||
|
|
||
| // Simulate Shift+Tab from first to wrap around | ||
| const event = new KeyboardEvent('keydown', { | ||
| key: 'Tab', | ||
| const event = new KeyboardEvent("keydown", { | ||
| key: "Tab", | ||
| shiftKey: true, | ||
| bubbles: true, | ||
| }) | ||
| }); | ||
| act(() => { | ||
| hiddenContainer.dispatchEvent(event) | ||
| }) | ||
| hiddenContainer.dispatchEvent(event); | ||
| }); | ||
|
|
||
| // Should skip hidden button and focus on the last focusable (button-3) | ||
| expect(document.activeElement).toBe(thirdButton) | ||
| expect(document.activeElement).toBe(thirdButton); | ||
|
|
||
| document.body.removeChild(hiddenContainer) | ||
| }) | ||
| }) | ||
| document.body.removeChild(hiddenContainer); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Dispatch the event in the inactive-state test.
Line 103 always passes because
eventis never dispatched. Dispatch it throughcontainerand assert that focus remains onlastButton. The test will then detect a regression that traps focus whenisActiveis false.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents