Reflect.apply.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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(TypeError, `${value} is not a function`);
  10. });
  11. });
  12. test("arguments list must be an object", () => {
  13. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  14. expect(() => {
  15. Reflect.apply(() => {}, undefined, value);
  16. }).toThrowWithMessage(TypeError, `${value} is not an object`);
  17. });
  18. });
  19. });
  20. describe("normal behavior", () => {
  21. test("calling built-in functions", () => {
  22. expect(Reflect.apply(String.prototype.charAt, "foo", [0])).toBe("f");
  23. expect(Reflect.apply(Array.prototype.indexOf, ["hello", 123, "foo", "bar"], ["foo"])).toBe(
  24. 2
  25. );
  26. });
  27. test("|this| argument is forwarded to called function", () => {
  28. function Foo(foo) {
  29. this.foo = foo;
  30. }
  31. var o = {};
  32. expect(o.foo).toBeUndefined();
  33. Reflect.apply(Foo, o, ["bar"]);
  34. expect(o.foo).toBe("bar");
  35. });
  36. });