gitea/web_src/js/utils/dom.test.ts
wxiaoguang f63822fe64
Fix autofocus behavior (#34397)
The "autofocus" was abused or misbehaved:

1. When users visit a page but they are not going to change a field,
then the field shouldn't get "autofocus"
* the "auth" / "user" page: in most cases, users do not want to change
the names
    * see also the GitHub's "settings" page behavior.
2. There shouldn't be duplicate "autofocus" inputs in most cases, only
the first one focuses
3. When a panel is shown, the "autofocus" should get focus
    * "add ssh key" panel

This PR fixes all these problems and by the way remove duplicate
"isElemHidden" function.
2025-05-08 18:26:18 +00:00

55 lines
2.3 KiB
TypeScript

import {
createElementFromAttrs,
createElementFromHTML,
queryElemChildren,
querySingleVisibleElem,
toggleElem,
} from './dom.ts';
test('createElementFromHTML', () => {
expect(createElementFromHTML('<a>foo<span>bar</span></a>').outerHTML).toEqual('<a>foo<span>bar</span></a>');
expect(createElementFromHTML('<tr data-x="1"><td>foo</td></tr>').outerHTML).toEqual('<tr data-x="1"><td>foo</td></tr>');
});
test('createElementFromAttrs', () => {
const el = createElementFromAttrs('button', {
id: 'the-id',
class: 'cls-1 cls-2',
disabled: true,
checked: false,
required: null,
tabindex: 0,
'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>');
});
test('querySingleVisibleElem', () => {
let el = createElementFromHTML('<div></div>');
expect(querySingleVisibleElem(el, 'span')).toBeNull();
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 class="some-class tw-hidden">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');
});
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);
});
test('toggleElem', () => {
const el = createElementFromHTML('<p><div>a</div><div class="tw-hidden">b</div></p>');
toggleElem(el.children);
expect(el.outerHTML).toEqual('<p><div class="tw-hidden">a</div><div class="">b</div></p>');
toggleElem(el.children, false);
expect(el.outerHTML).toEqual('<p><div class="tw-hidden">a</div><div class="tw-hidden">b</div></p>');
toggleElem(el.children, true);
expect(el.outerHTML).toEqual('<p><div class="">a</div><div class="">b</div></p>');
});