function-spread.js 889 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. test("basic functionality", () => {
  2. const sum = (a, b, c) => a + b + c;
  3. const a = [1, 2, 3];
  4. expect(sum(...a)).toBe(6);
  5. expect(sum(1, ...a)).toBe(4);
  6. expect(sum(...a, 10)).toBe(6);
  7. const foo = (a, b, c) => c;
  8. const o = { bar: [1, 2, 3] };
  9. expect(foo(...o.bar)).toBe(3);
  10. expect(foo(..."abc")).toBe("c");
  11. });
  12. test("spreading custom iterable", () => {
  13. let o = {
  14. [Symbol.iterator]() {
  15. return {
  16. i: 0,
  17. next() {
  18. if (this.i++ === 3) {
  19. return { done: true };
  20. }
  21. return { value: this.i };
  22. },
  23. };
  24. },
  25. };
  26. expect(Math.max(...o)).toBe(3);
  27. });
  28. test("spreading non iterable", () => {
  29. expect(() => {
  30. [...1];
  31. }).toThrowWithMessage(TypeError, "1 is not iterable");
  32. });