Reflect.apply.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. test("length is 3", () => {
  2. expect(Reflect.apply).toHaveLength(3);
  3. });
  4. describe("errors", () => {
  5. test("target must be a function", () => {
  6. [null, undefined, "foo", 123, NaN, Infinity, {}].forEach(value => {
  7. expect(() => {
  8. Reflect.apply(value);
  9. }).toThrowWithMessage(
  10. TypeError,
  11. "First argument of Reflect.apply() must be a function"
  12. );
  13. });
  14. });
  15. test("arguments list must be an object", () => {
  16. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  17. expect(() => {
  18. Reflect.apply(() => {}, undefined, value);
  19. }).toThrowWithMessage(TypeError, "Arguments list must be an object");
  20. });
  21. });
  22. });
  23. describe("normal behavior", () => {
  24. test("calling built-in functions", () => {
  25. expect(Reflect.apply(String.prototype.charAt, "foo", [0])).toBe("f");
  26. expect(Reflect.apply(Array.prototype.indexOf, ["hello", 123, "foo", "bar"], ["foo"])).toBe(
  27. 2
  28. );
  29. });
  30. test("|this| argument is forwarded to called function", () => {
  31. function Foo(foo) {
  32. this.foo = foo;
  33. }
  34. var o = {};
  35. expect(o.foo).toBeUndefined();
  36. Reflect.apply(Foo, o, ["bar"]);
  37. expect(o.foo).toBe("bar");
  38. });
  39. });