!140 test(no-vdom): add test

* test(no-vdom): add test
This commit is contained in:
Hoikan 2024-02-08 02:03:28 +00:00 committed by 陈超涛
parent a7a57426b0
commit 813d819fa7
4 changed files with 75 additions and 3 deletions

View File

@ -23,9 +23,24 @@ export function runComponent<T>(Comp: ComponentConstructor<T>, props: T = {} as
return untrack(() => Comp(props));
}
const ELEMENT_NODE = 1;
const DOCUMENT_NODE = 9;
const COMMENT_NODE = 8;
const DOCUMENT_FRAGMENT_NODE = 11;
function isValidContainer(container: HTMLElement | null): boolean {
return !!(
container &&
(container.nodeType === ELEMENT_NODE ||
container.nodeType === DOCUMENT_FRAGMENT_NODE ||
container.nodeType === DOCUMENT_NODE ||
container.nodeType === COMMENT_NODE)
);
}
export function render(codeFn: CodeFunction, element: HTMLElement): () => void {
if (!element) {
throw new Error('Render target is not provided');
if (!isValidContainer(element)) {
throw new Error('Render target is not valid.');
}
const disposer = (): void => {

View File

@ -21,6 +21,7 @@ import {
effect as $$effect,
style as $$style,
className as $$className,
setAttribute as $$setAttribute,
} from '../src/dom';
import { runComponent as $$runComponent, render } from '../src/core';
import { delegateEvents as $$delegateEvents, addEventListener as $$on } from '../src/event';
@ -262,6 +263,14 @@ describe('render', () => {
expect(container.querySelector('h1').innerHTML).toMatchInlineSnapshot('"1"');
});
it('should throw error when container is not valid', () => {
[undefined, null, 0, 1, true, false, 'string', Symbol('symbol'), {}].forEach(container => {
expect(() => render(() => $$runComponent(() => null, undefined), container)).toThrowError(
'Render target is not valid.'
);
});
});
it('should render string of style', ({ container }) => {
/**
*
@ -550,4 +559,52 @@ describe('render', () => {
render(() => $$runComponent(Comp, {}), container);
expect(container.querySelector('div').className).toEqual('red green');
});
it('should render attribute', ({ container }) => {
/**
*
* function App() {
* return (
* <h1 id="test">parallel</h1>
* );
* }
*/
// 编译后:
const $tmpl = /*#__PURE__*/ $$template('<div id="test">Count value is 0.');
const Comp = () => {
return (() => {
const _el$ = $tmpl();
$$setAttribute(_el$, 'id', 'test');
return _el$;
})();
};
render(() => $$runComponent(Comp, {}), container);
expect(container.querySelector('div').id).toEqual('test');
});
it('should update attribute', ({ container }) => {
/**
*
* function App() {
* const id = reactive('el');
* return (
* <h1 id={id.get()}>parallel</h1>
* );
* }
*/
// 编译后:
const $tmpl = /*#__PURE__*/ $$template('<div id="test">Count value is 0.');
const id = reactive('el');
const Comp = () => {
return (() => {
const _el$ = $tmpl();
$$effect(() => $$setAttribute(_el$, 'id', id.get()));
return _el$;
})();
};
render(() => $$runComponent(Comp, {}), container);
expect(container.querySelector('div').id).toEqual('el');
id.set('test');
expect(container.querySelector('div').id).toEqual('test');
});
});