Proxy.handler-apply.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. describe("[[Call]] trap normal behavior", () => {
  2. test("forwarding when not defined in handler", () => {
  3. let p = new Proxy(() => 5, { apply: null });
  4. expect(p()).toBe(5);
  5. p = new Proxy(() => 5, { apply: undefined });
  6. expect(p()).toBe(5);
  7. p = new Proxy(() => 5, {});
  8. expect(p()).toBe(5);
  9. });
  10. test("correct arguments supplied to trap", () => {
  11. const f = (a, b) => a + b;
  12. const handler = {
  13. apply(target, this_, arguments) {
  14. expect(target).toBe(f);
  15. expect(this_).toBe(handler);
  16. if (arguments[2])
  17. return arguments[0] * arguments[1];
  18. return f(...arguments);
  19. },
  20. };
  21. p = new Proxy(f, handler);
  22. expect(p(2, 4)).toBe(6);
  23. expect(p(2, 4, true)).toBe(8);
  24. });
  25. });
  26. describe("[[Call]] invariants", () => {
  27. test("target must have a [[Call]] slot", () => {
  28. [{}, [], new Proxy({}, {})].forEach(item => {
  29. expect(() => {
  30. new Proxy(item, {})();
  31. }).toThrowWithMessage(TypeError, "[object ProxyObject] is not a function");
  32. });
  33. });
  34. });