Skip to content

Commit

Permalink
fix(mock-doc): add HTMLUListElement (#5169)
Browse files Browse the repository at this point in the history
add a definition for `HTMLUListElement`. this allows `createElement`
calls to return a mocked version of `HTMLUListElement` that passes
`instanceof` calls.

the `type` and `compact` properties were not implemented, as they are
deprecated in the spec.

prior to this commit, folks attempting to use an `HTMLUListElement` in a
stencil test like so would receive an error:
```ts
describe('my-component', () => {
  it('using HTMLUListElement works', async () => {
    const unorderedList = document.createElement('ul');
    console.log(unorderedList instanceof HTMLUListElement);
  });
});
```
```
[46:08.8]  jest args: --spec --e2e --max-workers=8
 FAIL  src/my-component.spec.ts
  my-component
    ✕ using HTMLUListElement works (4 ms)

  ● my-component › using HTMLUListElement works

    ReferenceError: HTMLUListElement is not defined

      2 |   it('using HTMLUListElement works', async () => {
      3 |     const unorderedList = document.createElement('ul');
    > 4 |     console.log(unorderedList instanceof HTMLUListElement);
        |                                          ^
      5 |   });
      6 | });
      7 |

      at Object.<anonymous> (src/my-component.spec.ts:4:42)
```

Fixes: #3382
STENCIL-466 bug: HTMLUListElement is not defined
  • Loading branch information
rwaskiewicz committed Dec 18, 2023
1 parent 964c801 commit 6233cb5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/mock-doc/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export function createElement(ownerDocument: any, tagName: string): any {

case 'title':
return new MockTitleElement(ownerDocument);

case 'ul':
return new MockUListElement(ownerDocument);
}

if (ownerDocument != null && tagName.includes('-')) {
Expand Down Expand Up @@ -493,6 +496,12 @@ export class MockTitleElement extends MockHTMLElement {
}
}

export class MockUListElement extends MockHTMLElement {
constructor(ownerDocument: any) {
super(ownerDocument, 'ul');
}
}

export class MockCanvasElement extends MockHTMLElement {
constructor(ownerDocument: any) {
super(ownerDocument, 'canvas');
Expand Down
2 changes: 2 additions & 0 deletions src/mock-doc/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
MockStyleElement,
MockTemplateElement,
MockTitleElement,
MockUListElement,
} from './element';
import { MockCustomEvent, MockEvent, MockFocusEvent, MockKeyboardEvent, MockMouseEvent } from './event';
import { MockHeaders } from './headers';
Expand Down Expand Up @@ -177,4 +178,5 @@ const GLOBAL_CONSTRUCTORS: [string, any][] = [
['HTMLStyleElement', MockStyleElement],
['HTMLTemplateElement', MockTemplateElement],
['HTMLTitleElement', MockTitleElement],
['HTMLUListElement', MockUListElement],
];
10 changes: 9 additions & 1 deletion src/mock-doc/test/element.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MockDocument } from '../document';
import { MockAnchorElement, MockMetaElement, MockSVGElement } from '../element';
import { MockAnchorElement, MockMetaElement, MockSVGElement, MockUListElement } from '../element';
import { MockElement, MockHTMLElement } from '../node';
import { cloneWindow, MockWindow } from '../window';

Expand Down Expand Up @@ -479,4 +479,12 @@ describe('element', () => {
expect(elm.pathname).toBe('/path/to/page');
});
});

describe('ul', () => {
it('textContent', () => {
const elm: MockUListElement = doc.createElement('ul');
elm.textContent = 'this is an item in an unordered list';
expect(elm.textContent).toBe('this is an item in an unordered list');
});
});
});

0 comments on commit 6233cb5

Please sign in to comment.