arrow-functions.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. test("no arguments", () => {
  2. let getNumber = () => {
  3. return 42;
  4. };
  5. expect(getNumber()).toBe(42);
  6. getNumber = () => 42;
  7. expect(getNumber()).toBe(42);
  8. getNumber = () => {
  9. return 99;
  10. };
  11. expect(getNumber()).toBe(99);
  12. getNumber = () => 99;
  13. expect(getNumber()).toBe(99);
  14. });
  15. test("arguments", () => {
  16. let add = (a, b) => a + b;
  17. expect(add(2, 3)).toBe(5);
  18. const addBlock = (a, b) => {
  19. let res = a + b;
  20. return res;
  21. };
  22. expect(addBlock(5, 4)).toBe(9);
  23. });
  24. test("inside an array", () => {
  25. let chompy = [x => x, 2];
  26. expect(chompy).toHaveLength(2);
  27. expect(chompy[0](1)).toBe(1);
  28. });
  29. test("return object literal", () => {
  30. const makeObject = (a, b) => ({ a, b });
  31. const obj = makeObject(33, 44);
  32. expect(typeof obj).toBe("object");
  33. expect(obj.a).toBe(33);
  34. expect(obj.b).toBe(44);
  35. });
  36. test("return undefined", () => {
  37. let returnUndefined = () => {};
  38. expect(returnUndefined()).toBeUndefined();
  39. });
  40. test("return array literal", () => {
  41. const makeArray = (a, b) => [a, b];
  42. const array = makeArray("3", { foo: 4 });
  43. expect(array[0]).toBe("3");
  44. expect(array[1].foo).toBe(4);
  45. });
  46. test("return numeric expression", () => {
  47. let square = x => x * x;
  48. expect(square(3)).toBe(9);
  49. let squareBlock = x => {
  50. return x * x;
  51. };
  52. expect(squareBlock(4)).toBe(16);
  53. });
  54. test("return called arrow function expression", () => {
  55. const message = (who => "Hello " + who)("friends!");
  56. expect(message).toBe("Hello friends!");
  57. const sum = ((x, y, z) => x + y + z)(1, 2, 3);
  58. expect(sum).toBe(6);
  59. const product = ((x, y, z) => {
  60. let res = x * y * z;
  61. return res;
  62. })(5, 4, 2);
  63. expect(product).toBe(40);
  64. const half = (x => {
  65. return x / 2;
  66. })(10);
  67. expect(half).toBe(5);
  68. });
  69. test("currying", () => {
  70. let add = a => b => a + b;
  71. expect(typeof add(1)).toBe("function");
  72. expect(typeof add(1, 2)).toBe("function");
  73. expect(add(1)(2)).toBe(3);
  74. });
  75. test("with comma operator", () => {
  76. let foo, bar;
  77. (foo = bar), baz => {};
  78. expect(foo).toBe(undefined);
  79. expect(bar).toBe(undefined);
  80. });
  81. test("arrow functions in objects", () => {
  82. function FooBar() {
  83. this.x = {
  84. y: () => this,
  85. z: function () {
  86. return (() => this)();
  87. },
  88. };
  89. }
  90. const foobar = new FooBar();
  91. expect(foobar.x.y()).toBe(foobar);
  92. expect(foobar.x.z()).toBe(foobar.x);
  93. });
  94. test("strict mode propagation", () => {
  95. (() => {
  96. "use strict";
  97. expect(isStrictMode()).toBeTrue();
  98. (() => {
  99. expect(isStrictMode()).toBeTrue();
  100. })();
  101. })();
  102. (() => {
  103. "use strict";
  104. expect(isStrictMode()).toBeTrue();
  105. })();
  106. (() => {
  107. expect(isStrictMode()).toBeFalse();
  108. (() => {
  109. "use strict";
  110. expect(isStrictMode()).toBeTrue();
  111. })();
  112. expect(isStrictMode()).toBeFalse();
  113. })();
  114. });
  115. test("no prototype", () => {
  116. let foo = () => {};
  117. expect(foo).not.toHaveProperty("prototype");
  118. });
  119. test("cannot be constructed", () => {
  120. let foo = () => {};
  121. expect(() => {
  122. new foo();
  123. }).toThrowWithMessage(
  124. TypeError,
  125. "[object ECMAScriptFunctionObject] is not a constructor (evaluated from 'foo')"
  126. );
  127. });
  128. test("syntax errors", () => {
  129. expect("a, => {}").not.toEval();
  130. expect("(a, => {}").not.toEval();
  131. expect("(,) => {}").not.toEval();
  132. expect("(,,) => {}").not.toEval();
  133. expect("(a,,) => {}").not.toEval();
  134. expect("(a,,b) => {}").not.toEval();
  135. expect("(a, ...b, ...c) => {}").not.toEval();
  136. expect("(a b) => {}").not.toEval();
  137. expect("(a ...b) => {}").not.toEval();
  138. expect("(a = 1 = 2) => {}").not.toEval();
  139. expect("()\n=> {}").not.toEval();
  140. });
  141. test("destructuring parameters", () => {
  142. expect(`({ a }) => {}`).toEval();
  143. expect(`([ a ]) => {}`).toEval();
  144. expect(`{ a } => {}`).not.toEval();
  145. expect(`[ a ] => {}`).not.toEval();
  146. });