@ray-core/runtime
Version:
Ray 是一个全新的基于 React 的小程序开发框架
303 lines (302 loc) • 11.8 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
import * as React from 'react';
import './helpers/setupGlobals';
import View from './helpers/View';
import render from '../render';
import { reset as resetInstanceId } from '../instanceId';
import Container from '../Container';
import { useNativeEffect } from '../hooks';
import { RuntimeOptions } from '@ray-core/framework-shared';
var p = {
setData: function (state, callback) {
setTimeout(function () {
if (typeof callback === 'function') {
callback();
}
});
},
};
describe('remax render', function () {
beforeAll(function () {
RuntimeOptions.apply({ platform: 'toutiao' });
});
afterEach(function () {
RuntimeOptions.reset();
resetInstanceId();
});
it('render correctly', function () {
var Page = function () { return React.createElement(View, { className: "foo" }, "hello"); };
var container = new Container(p);
render(React.createElement(Page, null), container);
expect(container.root).toMatchSnapshot();
});
it('insert new element', function () {
var Page = /** @class */ (function (_super) {
__extends(Page, _super);
function Page() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
list: [1, 3],
};
return _this;
}
Page.prototype.update = function () {
this.setState({
list: [1, 2, 3],
});
};
Page.prototype.render = function () {
var list = this.state.list;
return (React.createElement(View, null, list.map(function (i) { return (React.createElement(View, { key: i }, i)); })));
};
return Page;
}(React.Component));
var container = new Container(p);
var page = React.createRef();
render(React.createElement(Page, { ref: page }), container);
expect(container.root).toMatchSnapshot();
page.current.update();
expect(container.root).toMatchSnapshot();
});
it("change elements' order", function () {
var Page = /** @class */ (function (_super) {
__extends(Page, _super);
function Page() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
list: [1, 2, 3],
};
return _this;
}
Page.prototype.updateA = function () {
this.setState({
list: [2, 1, 3],
});
};
Page.prototype.updateB = function () {
this.setState({
list: [2, 3, 1],
});
};
Page.prototype.render = function () {
var list = this.state.list;
return (React.createElement(View, null, list.map(function (i) { return (React.createElement(View, { key: i }, i)); })));
};
return Page;
}(React.Component));
var container = new Container(p);
var page = React.createRef();
render(React.createElement(Page, { ref: page }), container);
expect(container.root).toMatchSnapshot();
page.current.updateA();
expect(container.root).toMatchSnapshot();
page.current.updateB();
expect(container.root).toMatchSnapshot();
});
it('umount component', function () {
var Page = /** @class */ (function (_super) {
__extends(Page, _super);
function Page() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
show: true,
};
return _this;
}
Page.prototype.hide = function () {
this.setState({ show: false });
};
Page.prototype.render = function () {
return React.createElement(View, null, this.state.show && React.createElement(View, null, "foo"));
};
return Page;
}(React.Component));
var container = new Container(p);
var page = React.createRef();
render(React.createElement(Page, { ref: page }), container);
expect(container.root).toMatchSnapshot();
page.current.hide();
expect(container.root).toMatchSnapshot();
});
it('renders style', function () {
var Page = function () { return React.createElement(View, { style: { width: '100px', height: '100px' } }, "hello"); };
var container = new Container(p);
render(React.createElement(Page, null), container);
expect(container.root).toMatchSnapshot();
});
it('renders vendor prefix style', function () {
var Page = function () { return React.createElement(View, { style: { WebkitLineClamp: 2, height: '100px' } }, "hello"); };
var container = new Container(p);
render(React.createElement(Page, null), container);
expect(container.root).toMatchSnapshot();
});
it('renders empty style', function () {
var Page = function () { return React.createElement(View, { style: undefined }, "hello"); };
var container = new Container(p);
render(React.createElement(Page, null), container);
expect(container.root).toMatchSnapshot();
});
it('renders conditional fragment correctly', function () {
var Page = /** @class */ (function (_super) {
__extends(Page, _super);
function Page() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
show: false,
};
return _this;
}
Page.prototype.show = function () {
this.setState({ show: true });
};
Page.prototype.render = function () {
if (this.state.show) {
return React.createElement(View, null, "foo");
}
return (React.createElement(React.Fragment, null,
React.createElement(View, null, "one"),
React.createElement(View, null, "two")));
};
return Page;
}(React.Component));
var container = new Container(p);
var page = React.createRef();
render(React.createElement(Page, { ref: page }), container);
expect(container.root).toMatchSnapshot();
page.current.show();
expect(container.root).toMatchSnapshot();
});
it('render native component correctly', function () {
var NativeComponent = function (_a) {
var fooBar = _a.fooBar, onClick = _a.onClick, className = _a.className;
return React.createElement('native-component', {
fooBar: fooBar,
className: className,
onClick: onClick,
});
};
var actions = [];
var p = {
setData: function (data) { return actions.push(data); },
};
var container = new Container(p);
render(React.createElement(NativeComponent, { fooBar: "fooBar", onClick: function () { return void 0; }, className: "class" }), container);
expect(actions).toMatchSnapshot();
});
});
it('create proxy for onClick callback', function () {
var view = React.createRef();
var handleClick = function (event) {
expect(event.stopPropagation).not.toBeUndefined();
};
var handleAnimationStart = function (event) {
expect(event.stopPropagation).toBeUndefined();
};
var Page = /** @class */ (function (_super) {
__extends(Page, _super);
function Page() {
return _super !== null && _super.apply(this, arguments) || this;
}
Page.prototype.render = function () {
return React.createElement(View, { ref: view, onClick: handleClick, onAnimationStart: handleAnimationStart });
};
return Page;
}(React.Component));
var container = new Container(p);
render(React.createElement(Page, null), container);
function findFn(name) {
var fnKeys = Object.keys(view.current.container.context);
var fnKey = fnKeys.find(function (key) { return key.indexOf(name) !== -1; }) || '';
return view.current.container.context[fnKey];
}
var newHandleClick = findFn('onClick');
var newHandleAnimationStart = findFn('onAnimationStart');
newHandleClick({});
newHandleAnimationStart({});
});
it('useEffect works', function (done) {
var Page = function () {
React.useEffect(function () {
done();
});
return React.createElement(View, null, "app");
};
var container = new Container(p);
render(React.createElement(Page, null), container);
});
it('useNativeEffect once works', function (done) {
var count = 0;
var Page = function () {
var _a = __read(React.useState(0), 2), width = _a[0], setWidth = _a[1];
useNativeEffect(function () {
count += 1;
setTimeout(function () {
if (count === 1) {
done();
}
}, 500);
}, []);
React.useEffect(function () {
setTimeout(function () {
setWidth(100);
}, 100);
}, []);
return React.createElement(View, null, width);
};
var container = new Container(p);
render(React.createElement(Page, null), container);
});
it('useNativeEffect deps works', function (done) {
var count = 0;
var Page = function () {
var _a = __read(React.useState(0), 2), width = _a[0], setWidth = _a[1];
var _b = __read(React.useState(0), 2), height = _b[0], setheight = _b[1];
useNativeEffect(function () {
count += 1;
if (count === 2) {
done();
}
}, [width]);
React.useEffect(function () {
setheight(100);
setTimeout(function () {
setWidth(100);
}, 1000);
}, []);
return (React.createElement(View, null,
width,
height));
};
var container = new Container(p);
render(React.createElement(Page, null), container);
});