gitea源码

common-button.test.ts 1.2KB

1234567891011121314151617181920212223242526
  1. import {assignElementProperty, type ElementWithAssignableProperties} from './common-button.ts';
  2. test('assignElementProperty', () => {
  3. const elForm = document.createElement('form');
  4. assignElementProperty(elForm, 'action', '/test-link');
  5. expect(elForm.action).contains('/test-link'); // the DOM always returns absolute URL
  6. expect(elForm.getAttribute('action')).eq('/test-link');
  7. assignElementProperty(elForm, 'text-content', 'dummy');
  8. expect(elForm.textContent).toBe('dummy');
  9. // mock a form with its property "action" overwritten by an input element
  10. const elFormWithAction = new class implements ElementWithAssignableProperties {
  11. action = document.createElement('input'); // now "form.action" is not string, but an input element
  12. _attrs: Record<string, string> = {};
  13. setAttribute(name: string, value: string) { this._attrs[name] = value }
  14. getAttribute(name: string): string | null { return this._attrs[name] }
  15. }();
  16. assignElementProperty(elFormWithAction, 'action', '/bar');
  17. expect(elFormWithAction.getAttribute('action')).eq('/bar');
  18. const elInput = document.createElement('input');
  19. expect(elInput.readOnly).toBe(false);
  20. assignElementProperty(elInput, 'read-only', 'true');
  21. expect(elInput.readOnly).toBe(true);
  22. });