2021-06-12 11:35:53 +01:00
|
|
|
test("length is 2", () => {
|
|
|
|
|
expect(Object.assign).toHaveLength(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("errors", () => {
|
|
|
|
|
test("first argument must coercible to object", () => {
|
|
|
|
|
expect(() => {
|
|
|
|
|
Object.assign(null);
|
|
|
|
|
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
|
|
|
|
expect(() => {
|
|
|
|
|
Object.assign(undefined);
|
|
|
|
|
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("normal behavior", () => {
|
|
|
|
|
test("returns first argument coerced to object", () => {
|
|
|
|
|
const o = {};
|
|
|
|
|
expect(Object.assign(o)).toBe(o);
|
|
|
|
|
expect(Object.assign(o, {})).toBe(o);
|
|
|
|
|
expect(Object.assign(42)).toEqual(new Number(42));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("alters first argument object if sources are given", () => {
|
|
|
|
|
const o = { foo: 0 };
|
|
|
|
|
expect(Object.assign(o, { foo: 1 })).toBe(o);
|
|
|
|
|
expect(o).toEqual({ foo: 1 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("merges objects", () => {
|
|
|
|
|
const s = Symbol();
|
2025-10-31 11:50:48 -04:00
|
|
|
expect(Object.assign({}, { foo: 0, bar: "baz" }, { [s]: [1, 2, 3] }, { foo: 1 }, { [42]: "test" })).toEqual({
|
|
|
|
|
foo: 1,
|
|
|
|
|
bar: "baz",
|
|
|
|
|
[s]: [1, 2, 3],
|
|
|
|
|
42: "test",
|
|
|
|
|
});
|
2021-06-12 11:35:53 +01:00
|
|
|
});
|
|
|
|
|
});
|