summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/object-spread.js
blob: 93008a83522c2ca0bd87905fe99c1329f4f16c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const testObjSpread = obj => {
    expect(obj).toEqual({
        foo: 0,
        bar: 1,
        baz: 2,
        qux: 3,
    });
};

const testObjStrSpread = obj => {
    expect(obj).toEqual(["a", "b", "c", "d"]);
};

test("spread object literal inside object literal", () => {
    const obj = {
        foo: 0,
        ...{ bar: 1, baz: 2 },
        qux: 3,
    };
    testObjSpread(obj);
});

test("spread object with assigned property inside object literal", () => {
    const obj = { foo: 0, bar: 1, baz: 2 };
    obj.qux = 3;
    testObjSpread({ ...obj });
});

test("spread object inside object literal", () => {
    let a = { bar: 1, baz: 2 };
    const obj = { foo: 0, ...a, qux: 3 };
    testObjSpread(obj);
});

test("complex nested object spreading", () => {
    const obj = {
        ...{},
        ...{
            ...{ foo: 0, bar: 1, baz: 2 },
        },
        qux: 3,
    };
    testObjSpread(obj);
});

test("spread string in object literal", () => {
    const obj = { ..."abcd" };
    testObjStrSpread(obj);
});

test("spread array in object literal", () => {
    const obj = { ...["a", "b", "c", "d"] };
    testObjStrSpread(obj);
});

test("spread array with holes in object literal", () => {
    const obj = { ...[, , "a", , , , "b", "c", , "d", , ,] };
    expect(obj).toEqual({ 2: "a", 6: "b", 7: "c", 9: "d" });
});

test("spread string object in object literal", () => {
    const obj = { ...String("abcd") };
    testObjStrSpread(obj);
});

test("spread object with non-enumerable property", () => {
    const a = { foo: 0 };
    Object.defineProperty(a, "bar", {
        value: 1,
        enumerable: false,
    });
    const obj = { ...a };
    expect(obj.foo).toBe(0);
    expect(obj).not.toHaveProperty("bar");
});

test("spread object with symbol keys", () => {
    const s = Symbol("baz");
    const a = {
        foo: "bar",
        [s]: "qux",
    };
    const obj = { ...a };
    expect(obj.foo).toBe("bar");
    expect(obj[s]).toBe("qux");
});

test("spreading non-spreadable values", () => {
    let empty = {
        ...undefined,
        ...null,
        ...1,
        ...true,
        ...function () {},
        ...Date,
    };
    expect(Object.getOwnPropertyNames(empty)).toHaveLength(0);
});

test("respects custom Symbol.iterator method", () => {
    let o = {
        [Symbol.iterator]() {
            return {
                i: 0,
                next() {
                    if (this.i++ == 3) {
                        return { done: true };
                    }
                    return { value: this.i, done: false };
                },
            };
        },
    };

    let a = [...o];
    expect(a).toEqual([1, 2, 3]);
});