Skip to content

Commit

Permalink
Merge pull request #13 from bbc/issue-11
Browse files Browse the repository at this point in the history
Handle unregistering a node leaving no focusable node
  • Loading branch information
thomascgray authored Jun 5, 2019
2 parents 2047b0e + 0e8589e commit bce632c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ export class Lrud {
if (parentNode.activeChild && parentNode.activeChild === nodeId) {
delete parentNode.activeChild
const top = this.climbUp(parentNode, '*')
const prev = this.getPrevChild(top)
const child = this.digDown(prev)
this.assignFocus(child.id)
if (top) {
const prev = this.getPrevChild(top)
const child = this.digDown(prev)
this.assignFocus(child.id)
}
}

// ...we need to recalculate the indexes of all the parents children
Expand Down
38 changes: 38 additions & 0 deletions src/unregister.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,42 @@ describe('unregisterNode()', () => {
expect(navigation.tree).toMatchObject({})
expect(navigation.overrides).toMatchObject({})
})

test('unregistering the focused node when there is nothing else that can be focused on', () => {
const nav = new Lrud()

nav.registerNode('root', { orientation: 'vertical' })
nav.registerNode('row1', { orientation: 'horizontal', parent: 'root' })
nav.registerNode('item1', { isFocusable: true, parent: 'row1' })

// nothing else to focus on, but we shouldn't throw an exception
expect(() => {
nav.unregisterNode('item1')
}).not.toThrow()

// root should still have an activeChild of row 1
expect(nav.getNode('root').activeChild).toEqual('row1')
expect(nav.getNode('row1').activeChild).toEqual(undefined)
})

test('unregistering the focused node when there is nothing else that can be focused on - more nesting', () => {
const nav = new Lrud()

nav.registerNode('root', { orientation: 'vertical' })
nav.registerNode('boxa', { orientation: 'horizontal', parent: 'root' })
nav.registerNode('boxb', { orientation: 'horizontal', parent: 'boxa' })
nav.registerNode('boxc', { orientation: 'horizontal', parent: 'boxb' })
nav.registerNode('item1', { isFocusable: true, parent: 'boxc' })

// nothing else to focus on, but we shouldn't throw an exception
expect(() => {
nav.unregisterNode('item1')
}).not.toThrow()

// root should still have an activeChild of row 1
expect(nav.getNode('root').activeChild).toEqual('boxa')
expect(nav.getNode('boxa').activeChild).toEqual('boxb')
expect(nav.getNode('boxb').activeChild).toEqual('boxc')
expect(nav.getNode('boxc').activeChild).toEqual(undefined)
})
})

0 comments on commit bce632c

Please sign in to comment.