68 lines
3.5 KiB
JavaScript
68 lines
3.5 KiB
JavaScript
// Regression smoke test for the dependency-free remote module.
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
const source = fs.readFileSync('src/remote/ui/SegmentedSelector/SegmentedSelector.remote.js', 'utf8');
|
|
assert.equal(/^\s*import\s/m.test(source), false, 'Remote must be import-free');
|
|
assert.equal(/@chakra-ui\/react|@emotion\/|framer-motion/.test(source), false, 'Remote must be self-contained');
|
|
|
|
let component;
|
|
const callbacks = { next: null };
|
|
const fakeReact = {
|
|
Fragment: 'fragment',
|
|
isValidElement: node => !!node && typeof node === 'object' && 'type' in node,
|
|
createElement: (type, props, ...children) => ({ type, props: {...props, children: children.length === 1 ? children[0] : children} }),
|
|
useEffect() {}, useLayoutEffect() {},
|
|
useRef: value => ({current: value}),
|
|
useState: initial => [initial, () => {}]
|
|
};
|
|
let latestTree;
|
|
const scope = {
|
|
console,
|
|
queueMicrotask,
|
|
window: { __UIKIT_REACT_REMOTE_DEPS__: {
|
|
React: fakeReact,
|
|
createRoot: () => ({ render(node) { latestTree = node.type(node.props); }, unmount() {} }),
|
|
}},
|
|
document: { createElement(tag) { return { tagName: tag, style: {}, set textContent(value) { this._text = value; }, append() {} }; } },
|
|
HTMLElement: class { attachShadow() { return {append() {}}; } setAttribute(name, value) { this[name] = value; } get isConnected() {return true;} },
|
|
customElements: { get() {return component;}, define(name, ctor) {component = ctor; } },
|
|
};
|
|
vm.runInNewContext(source, scope);
|
|
assert.equal(typeof component, 'function');
|
|
const element = new component();
|
|
const items = [
|
|
{value:'a', title:'اول', tooltip:'راهنما'},
|
|
{value:'b', title:'دوم'},
|
|
{value:'c', title:'سوم', isDisabled:true},
|
|
];
|
|
element.componentProps = {items, mode:'text', value:'a', onChange:value=>{callbacks.next=value;}};
|
|
assert.equal(latestTree.props.children[0].props['data-mode'], 'options');
|
|
let controls = latestTree.props.children[0].props.children;
|
|
assert.equal(controls[0].props.children.props['aria-pressed'], true);
|
|
assert.equal(controls[1].props.children.props['aria-pressed'], false);
|
|
controls[1].props.children.props.onClick();
|
|
assert.equal(callbacks.next, 'b');
|
|
controls[2].props.children.props.onClick();
|
|
assert.equal(callbacks.next, 'b', 'Disabled item must not select');
|
|
assert.equal(controls[0].props.children.props.children.props.children, 'اول');
|
|
element.componentProps = {items, selectionMode:'tabs', value:'b', compact:true};
|
|
controls = latestTree.props.children[0].props.children;
|
|
assert.equal(latestTree.props.children[0].props.role, 'tablist', 'Tabs mode must expose tab semantics');
|
|
assert.equal(latestTree.props.children[0].props['data-mode'], 'tabs');
|
|
assert.equal(controls[1].props.children.props['aria-selected'], true);
|
|
assert.equal(controls[1].props.children.props.role, 'tab');
|
|
assert.equal(controls[2].props.children.props.disabled, true);
|
|
let wrapCalls = 0;
|
|
element.componentProps = {items, renderItem:(control)=>{wrapCalls++; return fakeReact.createElement('span', {role:'presentation'}, control);}};
|
|
assert.equal(wrapCalls, 3);
|
|
console.log('PASS: remote independence, options/tabs, disabled, callbacks, renderItem');
|
|
|
|
let focused = false;
|
|
element.componentProps = {items, selectionMode:'tabs', value:'a', dir:'rtl', onChange:value=>{callbacks.next=value;}};
|
|
controls = latestTree.props.children[0].props.children;
|
|
controls[1].props.children.props.ref({focus(){focused=true;}});
|
|
controls[0].props.children.props.onKeyDown({key:'ArrowLeft', preventDefault(){}});
|
|
assert.equal(callbacks.next, 'b');
|
|
assert.equal(focused, true);
|