Serenity Operating System
at master 143 lines 3.4 kB view raw
1test("single default parameter", () => { 2 function func(a = 6) { 3 return typeof a; 4 } 5 6 const arrowFunc = (a = 6) => typeof a; 7 8 expect(func()).toBe("number"); 9 expect(func(5)).toBe("number"); 10 expect(func(undefined)).toBe("number"); 11 expect(func(false)).toBe("boolean"); 12 expect(func(null)).toBe("object"); 13 expect(func({})).toBe("object"); 14 15 expect(arrowFunc()).toBe("number"); 16 expect(arrowFunc(5)).toBe("number"); 17 expect(arrowFunc(undefined)).toBe("number"); 18 expect(arrowFunc(false)).toBe("boolean"); 19 expect(arrowFunc(null)).toBe("object"); 20 expect(arrowFunc({})).toBe("object"); 21}); 22 23test("two parameters, second one is default", () => { 24 function func(a, b = 1) { 25 return a + b; 26 } 27 28 const arrowFunc = (a, b = 1) => a + b; 29 30 expect(func(4, 5)).toBe(9); 31 expect(func(4)).toBe(5); 32 expect(func(4, undefined)).toBe(5); 33 expect(func()).toBeNaN(); 34 35 expect(arrowFunc(4, 5)).toBe(9); 36 expect(arrowFunc(4)).toBe(5); 37 expect(arrowFunc(4, undefined)).toBe(5); 38 expect(arrowFunc()).toBeNaN(); 39}); 40 41test("two parameters, first one is default", () => { 42 function func(a = 5, b) { 43 return a + b; 44 } 45 46 const arrowFunc = (a = 5, b) => a + b; 47 48 expect(func(4, 5)).toBe(9); 49 expect(func(undefined, 4)).toBe(9); 50 expect(func()).toBeNaN(); 51 52 expect(arrowFunc(4, 5)).toBe(9); 53 expect(arrowFunc(undefined, 4)).toBe(9); 54 expect(arrowFunc()).toBeNaN(); 55}); 56 57test("default parameter references a previous parameter", () => { 58 function func(a, b = a) { 59 return a + b; 60 } 61 62 const arrowFunc = (a, b = a) => a + b; 63 64 expect(func(4, 5)).toBe(9); 65 expect(func(4)).toBe(8); 66 expect(func("hf")).toBe("hfhf"); 67 expect(func(true)).toBe(2); 68 expect(func()).toBeNaN(); 69 70 expect(arrowFunc(4, 5)).toBe(9); 71 expect(arrowFunc(4)).toBe(8); 72 expect(arrowFunc("hf")).toBe("hfhf"); 73 expect(arrowFunc(true)).toBe(2); 74 expect(arrowFunc()).toBeNaN(); 75}); 76 77test("parameter with a function default value", () => { 78 function func( 79 a = function () { 80 return 5; 81 } 82 ) { 83 return a(); 84 } 85 86 const arrowFunc = ( 87 a = function () { 88 return 5; 89 } 90 ) => a(); 91 92 expect(func()).toBe(5); 93 expect( 94 func(function () { 95 return 10; 96 }) 97 ).toBe(10); 98 expect(func(() => 10)).toBe(10); 99 100 expect(arrowFunc()).toBe(5); 101 expect( 102 arrowFunc(function () { 103 return 10; 104 }) 105 ).toBe(10); 106 expect(arrowFunc(() => 10)).toBe(10); 107}); 108 109test("parameter with an arrow function default value", () => { 110 function func(a = () => 5) { 111 return a(); 112 } 113 114 const arrowFunc = (a = () => 5) => a(); 115 116 expect(func()).toBe(5); 117 expect( 118 func(function () { 119 return 10; 120 }) 121 ).toBe(10); 122 expect(func(() => 10)).toBe(10); 123 expect(arrowFunc()).toBe(5); 124 expect( 125 arrowFunc(function () { 126 return 10; 127 }) 128 ).toBe(10); 129 expect(arrowFunc(() => 10)).toBe(10); 130}); 131 132test("parameter with an object default value", () => { 133 function func(a = { foo: "bar" }) { 134 return a.foo; 135 } 136 137 const arrowFunc = (a = { foo: "bar" }) => a.foo; 138 139 expect(func()).toBe("bar"); 140 expect(func({ foo: "baz" })).toBe("baz"); 141 expect(arrowFunc()).toBe("bar"); 142 expect(arrowFunc({ foo: "baz" })).toBe("baz"); 143});