mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +00:00
LibJS: Implement Object.assign()
This commit is contained in:
parent
de77946a8c
commit
7e1bffdeb8
4 changed files with 78 additions and 0 deletions
|
@ -0,0 +1,42 @@
|
|||
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();
|
||||
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" });
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue