Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#468@minor: Adds support for Event.composedPath().
- Loading branch information
1 parent
ae900ea
commit e92a7aa
Showing
2 changed files
with
138 additions
and
1 deletion.
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,103 @@ | ||
import IWindow from '../../src/window/IWindow'; | ||
import Window from '../../src/window/Window'; | ||
import IDocument from '../../src/nodes/document/IDocument'; | ||
import Event from '../../src/event/Event'; | ||
import CustomElement from '../CustomElement'; | ||
|
||
describe('Event', () => { | ||
let window: IWindow; | ||
let document: IDocument; | ||
|
||
beforeEach(() => { | ||
window = new Window(); | ||
document = window.document; | ||
|
||
window.customElements.define('custom-element', CustomElement); | ||
}); | ||
|
||
describe('composedPath()', () => { | ||
it('Returns a composed path.', () => { | ||
const div = document.createElement('div'); | ||
const span = document.createElement('span'); | ||
let composedPath = null; | ||
|
||
div.appendChild(span); | ||
document.body.appendChild(div); | ||
|
||
div.addEventListener('click', (event: Event) => { | ||
composedPath = event.composedPath(); | ||
}); | ||
|
||
span.dispatchEvent( | ||
new Event('click', { | ||
bubbles: true | ||
}) | ||
); | ||
|
||
expect(composedPath).toEqual([ | ||
span, | ||
div, | ||
document.body, | ||
document.documentElement, | ||
document, | ||
window | ||
]); | ||
}); | ||
|
||
it('Goes through shadow roots if composed is set to "true".', () => { | ||
const div = document.createElement('div'); | ||
const customELement = document.createElement('custom-element'); | ||
let composedPath = null; | ||
|
||
div.appendChild(customELement); | ||
|
||
document.body.appendChild(div); | ||
|
||
div.addEventListener('click', (event: Event) => { | ||
composedPath = event.composedPath(); | ||
}); | ||
|
||
customELement.shadowRoot.children[1].children[0].dispatchEvent( | ||
new Event('click', { | ||
bubbles: true, | ||
composed: true | ||
}) | ||
); | ||
|
||
expect(composedPath).toEqual([ | ||
customELement.shadowRoot.children[1].children[0], | ||
customELement.shadowRoot.children[1], | ||
customELement.shadowRoot, | ||
customELement, | ||
div, | ||
document.body, | ||
document.documentElement, | ||
document, | ||
window | ||
]); | ||
}); | ||
|
||
it('Does not go through shadow roots if composed is set to "false".', () => { | ||
const customELement = document.createElement('custom-element'); | ||
let composedPath = null; | ||
|
||
document.body.appendChild(customELement); | ||
|
||
customELement.shadowRoot.children[1].addEventListener('click', (event: Event) => { | ||
composedPath = event.composedPath(); | ||
}); | ||
|
||
customELement.shadowRoot.children[1].children[0].dispatchEvent( | ||
new Event('click', { | ||
bubbles: true | ||
}) | ||
); | ||
|
||
expect(composedPath).toEqual([ | ||
customELement.shadowRoot.children[1].children[0], | ||
customELement.shadowRoot.children[1], | ||
customELement.shadowRoot | ||
]); | ||
}); | ||
}); | ||
}); |