| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | const testObjSpread = obj => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(obj).toEqual({ | 
					
						
							|  |  |  |         foo: 0, | 
					
						
							|  |  |  |         bar: 1, | 
					
						
							|  |  |  |         baz: 2, | 
					
						
							|  |  |  |         qux: 3, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | const testObjStrSpread = obj => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(obj).toEqual(["a", "b", "c", "d"]); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("spread object literal inside object literal", () => { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         foo: 0, | 
					
						
							|  |  |  |         ...{ bar: 1, baz: 2 }, | 
					
						
							|  |  |  |         qux: 3, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     testObjSpread(obj); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("spread object with assigned property inside object literal", () => { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { foo: 0, bar: 1, baz: 2 }; | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     obj.qux = 3; | 
					
						
							|  |  |  |     testObjSpread({ ...obj }); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("spread object inside object literal", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     let a = { bar: 1, baz: 2 }; | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { foo: 0, ...a, qux: 3 }; | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     testObjSpread(obj); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("complex nested object spreading", () => { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         ...{}, | 
					
						
							|  |  |  |         ...{ | 
					
						
							|  |  |  |             ...{ foo: 0, bar: 1, baz: 2 }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         qux: 3, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     testObjSpread(obj); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("spread string in object literal", () => { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { ..."abcd" }; | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     testObjStrSpread(obj); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("spread array in object literal", () => { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { ...["a", "b", "c", "d"] }; | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     testObjStrSpread(obj); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 22:49:36 +00:00
										 |  |  | 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" }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("spread string object in object literal", () => { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { ...String("abcd") }; | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     testObjStrSpread(obj); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("spread object with non-enumerable property", () => { | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const a = { foo: 0 }; | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     Object.defineProperty(a, "bar", { | 
					
						
							|  |  |  |         value: 1, | 
					
						
							|  |  |  |         enumerable: false, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  |     const obj = { ...a }; | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(obj.foo).toBe(0); | 
					
						
							|  |  |  |     expect(obj).not.toHaveProperty("bar"); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-04-27 21:52:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:39:36 -07:00
										 |  |  | 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"); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | test("spreading non-spreadable values", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     let empty = { | 
					
						
							|  |  |  |         ...undefined, | 
					
						
							|  |  |  |         ...null, | 
					
						
							|  |  |  |         ...1, | 
					
						
							|  |  |  |         ...true, | 
					
						
							|  |  |  |         ...function () {}, | 
					
						
							|  |  |  |         ...Date, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     expect(Object.getOwnPropertyNames(empty)).toHaveLength(0); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-07-13 08:27:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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]); | 
					
						
							|  |  |  | }); |