2024-11-26 23:10:45 +08:00
|
|
|
import {createElementFromAttrs, createElementFromHTML, queryElemChildren, querySingleVisibleElem} from './dom.ts';
|
2024-06-07 21:42:31 +08:00
|
|
|
|
|
|
|
test('createElementFromHTML', () => {
|
|
|
|
expect(createElementFromHTML('<a>foo<span>bar</span></a>').outerHTML).toEqual('<a>foo<span>bar</span></a>');
|
2024-11-21 22:09:16 +08:00
|
|
|
expect(createElementFromHTML('<tr data-x="1"><td>foo</td></tr>').outerHTML).toEqual('<tr data-x="1"><td>foo</td></tr>');
|
2024-06-07 21:42:31 +08:00
|
|
|
});
|
2024-06-27 01:01:20 +08:00
|
|
|
|
|
|
|
test('createElementFromAttrs', () => {
|
|
|
|
const el = createElementFromAttrs('button', {
|
|
|
|
id: 'the-id',
|
|
|
|
class: 'cls-1 cls-2',
|
|
|
|
disabled: true,
|
2024-08-02 03:06:03 +08:00
|
|
|
checked: false,
|
2024-06-27 01:01:20 +08:00
|
|
|
required: null,
|
2024-08-02 03:06:03 +08:00
|
|
|
tabindex: 0,
|
2024-10-31 04:06:36 +08:00
|
|
|
'data-foo': 'the-data',
|
|
|
|
}, 'txt', createElementFromHTML('<span>inner</span>'));
|
|
|
|
expect(el.outerHTML).toEqual('<button id="the-id" class="cls-1 cls-2" disabled="" tabindex="0" data-foo="the-data">txt<span>inner</span></button>');
|
2024-06-27 01:01:20 +08:00
|
|
|
});
|
2024-11-07 04:21:53 +08:00
|
|
|
|
|
|
|
test('querySingleVisibleElem', () => {
|
|
|
|
let el = createElementFromHTML('<div><span>foo</span></div>');
|
|
|
|
expect(querySingleVisibleElem(el, 'span').textContent).toEqual('foo');
|
|
|
|
el = createElementFromHTML('<div><span style="display: none;">foo</span><span>bar</span></div>');
|
|
|
|
expect(querySingleVisibleElem(el, 'span').textContent).toEqual('bar');
|
|
|
|
el = createElementFromHTML('<div><span>foo</span><span>bar</span></div>');
|
|
|
|
expect(() => querySingleVisibleElem(el, 'span')).toThrowError('Expected exactly one visible element');
|
|
|
|
});
|
2024-11-26 23:10:45 +08:00
|
|
|
|
|
|
|
test('queryElemChildren', () => {
|
|
|
|
const el = createElementFromHTML('<div><span class="a">a</span><span class="b">b</span></div>');
|
|
|
|
const children = queryElemChildren(el, '.a');
|
|
|
|
expect(children.length).toEqual(1);
|
|
|
|
});
|