| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("single default parameter", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     function func(a = 6) { | 
					
						
							|  |  |  |         return typeof a; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const arrowFunc = (a = 6) => typeof a; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(func()).toBe("number"); | 
					
						
							|  |  |  |     expect(func(5)).toBe("number"); | 
					
						
							|  |  |  |     expect(func(undefined)).toBe("number"); | 
					
						
							|  |  |  |     expect(func(false)).toBe("boolean"); | 
					
						
							|  |  |  |     expect(func(null)).toBe("object"); | 
					
						
							|  |  |  |     expect(func({})).toBe("object"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(arrowFunc()).toBe("number"); | 
					
						
							|  |  |  |     expect(arrowFunc(5)).toBe("number"); | 
					
						
							|  |  |  |     expect(arrowFunc(undefined)).toBe("number"); | 
					
						
							|  |  |  |     expect(arrowFunc(false)).toBe("boolean"); | 
					
						
							|  |  |  |     expect(arrowFunc(null)).toBe("object"); | 
					
						
							|  |  |  |     expect(arrowFunc({})).toBe("object"); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("two parameters, second one is default", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     function func(a, b = 1) { | 
					
						
							|  |  |  |         return a + b; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     const arrowFunc = (a, b = 1) => a + b; | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(func(4, 5)).toBe(9); | 
					
						
							|  |  |  |     expect(func(4)).toBe(5); | 
					
						
							|  |  |  |     expect(func(4, undefined)).toBe(5); | 
					
						
							|  |  |  |     expect(func()).toBeNaN(); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(arrowFunc(4, 5)).toBe(9); | 
					
						
							|  |  |  |     expect(arrowFunc(4)).toBe(5); | 
					
						
							|  |  |  |     expect(arrowFunc(4, undefined)).toBe(5); | 
					
						
							|  |  |  |     expect(arrowFunc()).toBeNaN(); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("two parameters, first one is default", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     function func(a = 5, b) { | 
					
						
							|  |  |  |         return a + b; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     const arrowFunc = (a = 5, b) => a + b; | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(func(4, 5)).toBe(9); | 
					
						
							|  |  |  |     expect(func(undefined, 4)).toBe(9); | 
					
						
							|  |  |  |     expect(func()).toBeNaN(); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(arrowFunc(4, 5)).toBe(9); | 
					
						
							|  |  |  |     expect(arrowFunc(undefined, 4)).toBe(9); | 
					
						
							|  |  |  |     expect(arrowFunc()).toBeNaN(); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("default parameter references a previous parameter", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     function func(a, b = a) { | 
					
						
							|  |  |  |         return a + b; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const arrowFunc = (a, b = a) => a + b; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(func(4, 5)).toBe(9); | 
					
						
							|  |  |  |     expect(func(4)).toBe(8); | 
					
						
							|  |  |  |     expect(func("hf")).toBe("hfhf"); | 
					
						
							|  |  |  |     expect(func(true)).toBe(2); | 
					
						
							|  |  |  |     expect(func()).toBeNaN(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(arrowFunc(4, 5)).toBe(9); | 
					
						
							|  |  |  |     expect(arrowFunc(4)).toBe(8); | 
					
						
							|  |  |  |     expect(arrowFunc("hf")).toBe("hfhf"); | 
					
						
							|  |  |  |     expect(arrowFunc(true)).toBe(2); | 
					
						
							|  |  |  |     expect(arrowFunc()).toBeNaN(); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("parameter with a function default value", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     function func( | 
					
						
							|  |  |  |         a = function () { | 
					
						
							|  |  |  |             return 5; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         return a(); | 
					
						
							| 
									
										
										
										
											2020-05-02 11:46:39 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     const arrowFunc = ( | 
					
						
							|  |  |  |         a = function () { | 
					
						
							|  |  |  |             return 5; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) => a(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(func()).toBe(5); | 
					
						
							|  |  |  |     expect( | 
					
						
							|  |  |  |         func(function () { | 
					
						
							|  |  |  |             return 10; | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     ).toBe(10); | 
					
						
							|  |  |  |     expect(func(() => 10)).toBe(10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(arrowFunc()).toBe(5); | 
					
						
							|  |  |  |     expect( | 
					
						
							|  |  |  |         arrowFunc(function () { | 
					
						
							|  |  |  |             return 10; | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     ).toBe(10); | 
					
						
							|  |  |  |     expect(arrowFunc(() => 10)).toBe(10); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("parameter with an arrow function default vlaue", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     function func(a = () => 5) { | 
					
						
							|  |  |  |         return a(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const arrowFunc = (a = () => 5) => a(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(func()).toBe(5); | 
					
						
							|  |  |  |     expect( | 
					
						
							|  |  |  |         func(function () { | 
					
						
							|  |  |  |             return 10; | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     ).toBe(10); | 
					
						
							|  |  |  |     expect(func(() => 10)).toBe(10); | 
					
						
							|  |  |  |     expect(arrowFunc()).toBe(5); | 
					
						
							|  |  |  |     expect( | 
					
						
							|  |  |  |         arrowFunc(function () { | 
					
						
							|  |  |  |             return 10; | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     ).toBe(10); | 
					
						
							|  |  |  |     expect(arrowFunc(() => 10)).toBe(10); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("parameter with an object default value", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     function func(a = { foo: "bar" }) { | 
					
						
							|  |  |  |         return a.foo; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     const arrowFunc = (a = { foo: "bar" }) => a.foo; | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(func()).toBe("bar"); | 
					
						
							|  |  |  |     expect(func({ foo: "baz" })).toBe("baz"); | 
					
						
							|  |  |  |     expect(arrowFunc()).toBe("bar"); | 
					
						
							|  |  |  |     expect(arrowFunc({ foo: "baz" })).toBe("baz"); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); |