createElement.spec.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React from '../../src/May';
  2. describe("createElement", function () {
  3. it("type", () => {
  4. var el = React.createElement("p", null, "aaa");
  5. expect(el.type).toBe("p");
  6. expect(el.mtype).toBe(1);
  7. expect(el.props.children).toBe("aaa");
  8. expect(el.props.children.length).toBe(3);
  9. });
  10. it("children", () => {
  11. var el = React.createElement("p", null, "aaa", "bbb", "ccc");
  12. expect(el.props.children).toEqual(["aaa", "bbb", "ccc"]);
  13. el = React.createElement("p", null, null);
  14. expect(el.props.children).toEqual(null);
  15. el = React.createElement("div", { key: "xxx" });
  16. expect(el.key).toBe("xxx");
  17. el = React.createElement("p", null, []);
  18. expect(el.props.children.length).toBe(0);
  19. el = React.createElement("p", { children: ["aaa", "bbb"] });
  20. expect(el.props.children.length).toBe(2);
  21. el = React.createElement("p", null);
  22. expect(el.props.children).toBe(void 666);
  23. });
  24. it("Children.only", () => {
  25. var el = React.createElement("p", null, "aaa", "bbb", "ccc");
  26. var a = React.Children.only(el);
  27. expect(a.type).toBe("p");
  28. expect(a.props.children).toEqual(["aaa", "bbb", "ccc"]);
  29. el = React.createElement("p", null, null);
  30. expect(el.props.children).toEqual(null);
  31. el = React.createElement("p", null, []);
  32. expect(el.props.children.length).toBe(0);
  33. expect(el.mtype).toBe(1);
  34. el = React.createElement("p", { children: ["aaa", "bbb"] });
  35. expect(el.props.children.length).toBe(2);
  36. });
  37. it("flatChildren", () => {
  38. var el = React.createElement("p", null, "aaa", false, "ccc");
  39. expect(el.props.children).toEqual(["aaa", false, "ccc"]);
  40. var el = React.createElement("p", null, "aaa", true, "ccc");
  41. expect(el.props.children).toEqual(["aaa", true, "ccc"]);
  42. var el = React.createElement("p", null, "aaa", 111, "ccc");
  43. expect(el.props.children).toEqual(["aaa", 111, "ccc"]);
  44. var el = React.createElement("p", null, "aaa", "", "ccc");
  45. expect(el.props.children).toEqual(["aaa", "", "ccc"]);
  46. var el = React.createElement("p", null, "aaa", "ccc", "");
  47. expect(el.props.children).toEqual(["aaa", "ccc", ""]);
  48. var el = React.createElement("p", null, "aaa", "", "ccc");
  49. expect(el.props.children).toEqual(["aaa", "", "ccc"]);
  50. var el = React.createElement("p", null, 111, 222, 333);
  51. expect(el.props.children).toEqual([111, 222, 333]);
  52. var el = React.createElement("p", null, 111, "ddd", 333);
  53. expect(el.props.children).toEqual([111, "ddd", 333]);
  54. });
  55. it("class render", () => {
  56. class A extends React.Component {
  57. render() {
  58. return <div id="aaa" />;
  59. }
  60. }
  61. var el = React.createElement(A, {});
  62. expect(el.mtype).toBe(2);
  63. el = React.createElement(function () { }, {});
  64. expect(el.mtype).toBe(2);
  65. var obj = new A().render();
  66. expect(obj.props.children).toEqual(void 666);
  67. expect(obj.props.id).toBe("aaa");
  68. expect(obj.props).toEqual({
  69. id: "aaa"
  70. });
  71. expect(obj.type).toEqual("div");
  72. expect(obj.key == null).toBe(true);
  73. // expect(typeof obj._owner).toBe("object");
  74. });
  75. });