function-default-parameters.js 3.3 KB

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