|
1 | 1 | import {JSDOM} from 'jsdom'
|
2 | 2 | import * as dtl from '../'
|
3 | 3 |
|
| 4 | +beforeEach(() => { |
| 5 | + const dom = new JSDOM() |
| 6 | + global.document = dom.window.document |
| 7 | + global.window = dom.window |
| 8 | + global.Node = dom.window.Node |
| 9 | +}) |
| 10 | + |
4 | 11 | test('works without a global dom', async () => {
|
5 | 12 | const container = new JSDOM(`
|
6 | 13 | <html>
|
@@ -77,6 +84,60 @@ test('works without a browser context on a dom node (JSDOM Fragment)', () => {
|
77 | 84 | `)
|
78 | 85 | })
|
79 | 86 |
|
| 87 | +test('works with a custom configured element query for shadow dom elements', async () => { |
| 88 | + const window = new JSDOM(` |
| 89 | + <html> |
| 90 | + <body> |
| 91 | + <example-input></example-input> |
| 92 | + </body> |
| 93 | + </html> |
| 94 | + `).window |
| 95 | + const document = window.document |
| 96 | + const container = document.body |
| 97 | + |
| 98 | + // Given I have defined a component with shadow dom |
| 99 | + window.customElements.define( |
| 100 | + 'example-input', |
| 101 | + class extends window.HTMLElement { |
| 102 | + constructor() { |
| 103 | + super() |
| 104 | + const shadow = this.attachShadow({mode: 'open'}) |
| 105 | + |
| 106 | + const div = document.createElement('div') |
| 107 | + const label = document.createElement('label') |
| 108 | + label.setAttribute('for', 'invisible-from-outer-dom') |
| 109 | + label.innerHTML = |
| 110 | + 'Visible in browser, invisible for traditional queries' |
| 111 | + const input = document.createElement('input') |
| 112 | + input.setAttribute('id', 'invisible-from-outer-dom') |
| 113 | + div.appendChild(label) |
| 114 | + div.appendChild(input) |
| 115 | + shadow.appendChild(div) |
| 116 | + } |
| 117 | + }, |
| 118 | + ) |
| 119 | + |
| 120 | + // Then it is part of the document |
| 121 | + expect( |
| 122 | + dtl.queryByLabelText( |
| 123 | + container, |
| 124 | + /Visible in browser, invisible for traditional queries/i, |
| 125 | + ), |
| 126 | + ).toBeInTheDocument() |
| 127 | + |
| 128 | + // And it returns the expected item |
| 129 | + expect( |
| 130 | + dtl.getByLabelText( |
| 131 | + container, |
| 132 | + /Visible in browser, invisible for traditional queries/i, |
| 133 | + ), |
| 134 | + ).toMatchInlineSnapshot(` |
| 135 | + <input |
| 136 | + id=invisible-from-outer-dom |
| 137 | + /> |
| 138 | + `) |
| 139 | +}) |
| 140 | + |
80 | 141 | test('byRole works without a global DOM', () => {
|
81 | 142 | const {
|
82 | 143 | window: {
|
|
0 commit comments