-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2090 from mainmatter/root-element-shadow-dom
Support ShadowRoot, add tests for rootElement, fixes #2089
- Loading branch information
Showing
5 changed files
with
168 additions
and
5 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,152 @@ | ||
import { describe, beforeEach, test, expect } from 'vitest'; | ||
|
||
import TestAssertions from '../helpers/test-assertions'; | ||
|
||
describe('assert.dom(..., rootElement)', () => { | ||
let assert: TestAssertions; | ||
|
||
beforeEach(() => { | ||
assert = new TestAssertions(); | ||
}); | ||
|
||
test('passing an Element as rootElement', () => { | ||
document.body.innerHTML = ` | ||
<span class="target">dedcoy<span> | ||
<h1 class="parent"> | ||
<span class="target">real target<span> | ||
</h1> | ||
`; | ||
|
||
const rootElement = document.querySelector('.parent'); | ||
|
||
assert.dom('.target', rootElement).exists({ count: 1 }); | ||
assert.dom('.target', rootElement).hasText('real target'); | ||
|
||
expect(assert.results).toEqual([ | ||
{ | ||
result: true, | ||
actual: 'Element .target exists once', | ||
expected: 'Element .target exists once', | ||
message: 'Element .target exists once', | ||
}, | ||
{ | ||
result: true, | ||
actual: 'real target', | ||
expected: 'real target', | ||
message: 'Element .target has text "real target"', | ||
}, | ||
]); | ||
}); | ||
|
||
test('not passing anything as rootElement', () => { | ||
document.body.innerHTML = ` | ||
<span class="target">decoy<span> | ||
<h1 class="parent"> | ||
<span class="target">real target<span> | ||
</h1> | ||
`; | ||
|
||
assert.dom('.target').exists({ count: 2 }); | ||
|
||
expect(assert.results).toEqual([ | ||
{ | ||
result: true, | ||
actual: 'Element .target exists twice', | ||
expected: 'Element .target exists twice', | ||
message: 'Element .target exists twice', | ||
}, | ||
]); | ||
}); | ||
|
||
test('passing document as rootElement', () => { | ||
document.body.innerHTML = ` | ||
<span class="target">decoy<span> | ||
<h1 class="parent"> | ||
<span class="target">real target<span> | ||
</h1> | ||
`; | ||
|
||
assert.dom('.target', document).exists({ count: 2 }); | ||
|
||
expect(assert.results).toEqual([ | ||
{ | ||
result: true, | ||
actual: 'Element .target exists twice', | ||
expected: 'Element .target exists twice', | ||
message: 'Element .target exists twice', | ||
}, | ||
]); | ||
}); | ||
|
||
test('passing null as rootElement', () => { | ||
document.body.innerHTML = ` | ||
<span class="target">decoy<span> | ||
<h1 class="parent"> | ||
<span class="target">real target<span> | ||
</h1> | ||
`; | ||
|
||
assert.dom('.target', null).exists({ count: 2 }); | ||
|
||
expect(assert.results).toEqual([ | ||
{ | ||
result: true, | ||
actual: 'Element .target exists twice', | ||
expected: 'Element .target exists twice', | ||
message: 'Element .target exists twice', | ||
}, | ||
]); | ||
}); | ||
|
||
test('passing shadow root as rootElement', () => { | ||
document.body.innerHTML = ` | ||
<div id="container"> | ||
<span class="target">decoy<span> | ||
</div> | ||
`; | ||
|
||
const container = document.getElementById('container'); | ||
const shadowRoot = container.attachShadow({ mode: 'closed' }); | ||
|
||
shadowRoot.innerHTML = '<span class="target">real target<span>'; | ||
|
||
assert.dom('.target').exists({ count: 1 }, 'Only decoy element is found outside shadow root'); | ||
assert.dom('.target').hasText('real target', 'decoy element text'); | ||
|
||
assert.dom('.target', shadowRoot).exists({ count: 1 }, 'Only target found in shadow root'); | ||
assert.dom('.target', shadowRoot).hasText('real target', 'Target element text'); | ||
|
||
console.log(assert.results); | ||
|
||
expect(assert.results).toEqual([ | ||
{ | ||
result: true, | ||
actual: 'Element .target exists once', | ||
expected: 'Element .target exists once', | ||
message: 'Only decoy element is found outside shadow root', | ||
}, | ||
{ | ||
result: false, | ||
actual: 'decoy', | ||
expected: 'real target', | ||
message: 'decoy element text', | ||
}, | ||
{ | ||
result: true, | ||
actual: 'Element .target exists once', | ||
expected: 'Element .target exists once', | ||
message: 'Only target found in shadow root', | ||
}, | ||
{ | ||
result: true, | ||
actual: 'real target', | ||
expected: 'real target', | ||
message: 'Target element text', | ||
}, | ||
]); | ||
}); | ||
}); |
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
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