Skip to content

Commit

Permalink
fix(FocusManager): setFocus validates and fixes provided path
Browse files Browse the repository at this point in the history
  • Loading branch information
vovacodes committed Mar 18, 2020
1 parent 91b3a26 commit 0cde7a6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
3 changes: 0 additions & 3 deletions packages/react-sunbeam/src/focus/FocusManager.spec.ts

This file was deleted.

32 changes: 32 additions & 0 deletions packages/react-sunbeam/src/focus/FocusManager.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as React from "react"
import { act, render } from "@testing-library/react"
import { SunbeamProvider } from "./components/SunbeamProvider"
import { Focusable } from "./components/Focusable"
import { FocusManager } from "./FocusManager"

describe("FocusManager", () => {
describe("setFocus", () => {
it("should validate and fix the provided focusPath", () => {
const focusManager = new FocusManager({ initialFocusPath: ["left-parent", "left-child"] })

render(
<SunbeamProvider focusManager={focusManager}>
<Focusable focusKey="left-parent">
<Focusable focusKey="left-child">Left child</Focusable>
</Focusable>
<Focusable focusKey="right-parent">
<Focusable focusKey="right-child">Right child</Focusable>
</Focusable>
</SunbeamProvider>
)

expect(focusManager.getFocusPath()).toEqual(["left-parent", "left-child"])

act(() => {
focusManager.setFocus(["right-parent"])
})

expect(focusManager.getFocusPath()).toEqual(["right-parent", "right-child"])
})
})
})
6 changes: 5 additions & 1 deletion packages/react-sunbeam/src/focus/FocusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export class FocusManager {
}

public setFocus(focusPath: FocusPath): void {
this.focusPath = focusPath
if (!this.focusableRoot) return

const fixedFocusPath = validateAndFixFocusPathIfNeeded(focusPath, this.focusableRoot)
this.focusPath = fixedFocusPath ?? focusPath

this.notifySubscribers()
}

Expand Down

0 comments on commit 0cde7a6

Please sign in to comment.