object-spread.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const testObjSpread = obj => {
  2. expect(obj).toEqual({
  3. foo: 0,
  4. bar: 1,
  5. baz: 2,
  6. qux: 3,
  7. });
  8. };
  9. const testObjStrSpread = obj => {
  10. expect(obj).toEqual(["a", "b", "c", "d"]);
  11. };
  12. test("spread object literal inside object literal", () => {
  13. const obj = {
  14. foo: 0,
  15. ...{ bar: 1, baz: 2 },
  16. qux: 3,
  17. };
  18. testObjSpread(obj);
  19. });
  20. test("spread object with assigned property inside object literal", () => {
  21. const obj = { foo: 0, bar: 1, baz: 2 };
  22. obj.qux = 3;
  23. testObjSpread({ ...obj });
  24. });
  25. test("spread object inside object literal", () => {
  26. let a = { bar: 1, baz: 2 };
  27. const obj = { foo: 0, ...a, qux: 3 };
  28. testObjSpread(obj);
  29. });
  30. test("complex nested object spreading", () => {
  31. const obj = {
  32. ...{},
  33. ...{
  34. ...{ foo: 0, bar: 1, baz: 2 },
  35. },
  36. qux: 3,
  37. };
  38. testObjSpread(obj);
  39. });
  40. test("spread string in object literal", () => {
  41. const obj = { ..."abcd" };
  42. testObjStrSpread(obj);
  43. });
  44. test("spread array in object literal", () => {
  45. const obj = { ...["a", "b", "c", "d"] };
  46. testObjStrSpread(obj);
  47. });
  48. test("spread array with holes in object literal", () => {
  49. const obj = { ...[, , "a", , , , "b", "c", , "d", , ,] };
  50. expect(obj).toEqual({ 2: "a", 6: "b", 7: "c", 9: "d" });
  51. });
  52. test("spread string object in object literal", () => {
  53. const obj = { ...String("abcd") };
  54. testObjStrSpread(obj);
  55. });
  56. test("spread object with non-enumerable property", () => {
  57. const a = { foo: 0 };
  58. Object.defineProperty(a, "bar", {
  59. value: 1,
  60. enumerable: false,
  61. });
  62. const obj = { ...a };
  63. expect(obj.foo).toBe(0);
  64. expect(obj).not.toHaveProperty("bar");
  65. });
  66. test("spread object with symbol keys", () => {
  67. const s = Symbol("baz");
  68. const a = {
  69. foo: "bar",
  70. [s]: "qux",
  71. };
  72. const obj = { ...a };
  73. expect(obj.foo).toBe("bar");
  74. expect(obj[s]).toBe("qux");
  75. });
  76. test("spreading non-spreadable values", () => {
  77. let empty = {
  78. ...undefined,
  79. ...null,
  80. ...1,
  81. ...true,
  82. ...function () {},
  83. ...Date,
  84. };
  85. expect(Object.getOwnPropertyNames(empty)).toHaveLength(0);
  86. });
  87. test("respects custom Symbol.iterator method", () => {
  88. let o = {
  89. [Symbol.iterator]() {
  90. return {
  91. i: 0,
  92. next() {
  93. if (this.i++ == 3) {
  94. return { done: true };
  95. }
  96. return { value: this.i, done: false };
  97. },
  98. };
  99. },
  100. };
  101. let a = [...o];
  102. expect(a).toEqual([1, 2, 3]);
  103. });