Object.assign.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. test("length is 2", () => {
  2. expect(Object.assign).toHaveLength(2);
  3. });
  4. describe("errors", () => {
  5. test("first argument must coercible to object", () => {
  6. expect(() => {
  7. Object.assign(null);
  8. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  9. expect(() => {
  10. Object.assign(undefined);
  11. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  12. });
  13. });
  14. describe("normal behavior", () => {
  15. test("returns first argument coerced to object", () => {
  16. const o = {};
  17. expect(Object.assign(o)).toBe(o);
  18. expect(Object.assign(o, {})).toBe(o);
  19. expect(Object.assign(42)).toEqual(new Number(42));
  20. });
  21. test("alters first argument object if sources are given", () => {
  22. const o = { foo: 0 };
  23. expect(Object.assign(o, { foo: 1 })).toBe(o);
  24. expect(o).toEqual({ foo: 1 });
  25. });
  26. test("merges objects", () => {
  27. const s = Symbol();
  28. expect(
  29. Object.assign(
  30. {},
  31. { foo: 0, bar: "baz" },
  32. { [s]: [1, 2, 3] },
  33. { foo: 1 },
  34. { [42]: "test" }
  35. )
  36. ).toEqual({ foo: 1, bar: "baz", [s]: [1, 2, 3], 42: "test" });
  37. });
  38. });