Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useActiveElement): support shadow roots (#2592)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
4 changed files
with
75 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,61 @@ | ||
import { useActiveElement } from '.' | ||
|
||
describe('useActiveElement', () => { | ||
let shadowHost: HTMLElement | ||
let input: HTMLInputElement | ||
let shadowInput: HTMLInputElement | ||
let shadowRoot: ShadowRoot | ||
|
||
beforeEach(() => { | ||
shadowHost = document.createElement('div') | ||
shadowRoot = shadowHost.attachShadow({ mode: 'open' }) | ||
input = document.createElement('input') | ||
shadowInput = input.cloneNode() as HTMLInputElement | ||
shadowRoot.appendChild(shadowInput) | ||
document.body.appendChild(input) | ||
document.body.appendChild(shadowHost) | ||
}) | ||
|
||
afterEach(() => { | ||
shadowHost.remove() | ||
input.remove() | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(useActiveElement).toBeDefined() | ||
}) | ||
|
||
it('should initialise correctly', () => { | ||
const activeElement = useActiveElement() | ||
|
||
expect(activeElement.value).to.equal(document.body) | ||
}) | ||
|
||
it('should initialise with already-active element', () => { | ||
input.focus() | ||
|
||
const activeElement = useActiveElement() | ||
|
||
expect(activeElement.value).to.equal(input) | ||
}) | ||
|
||
it('should accept custom document', () => { | ||
const activeElement = useActiveElement({ document: shadowRoot }) | ||
|
||
shadowInput.focus() | ||
|
||
expect(activeElement.value).to.equal(shadowInput) | ||
}) | ||
|
||
it('should observe focus/blur events', () => { | ||
const activeElement = useActiveElement() | ||
|
||
input.focus() | ||
|
||
expect(activeElement.value).to.equal(input) | ||
|
||
input.blur() | ||
|
||
expect(activeElement.value).to.equal(document.body) | ||
}) | ||
}) |
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