Proxy.handler-apply.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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]) return arguments[0] * arguments[1];
  17. return f(...arguments);
  18. },
  19. };
  20. p = new Proxy(f, handler);
  21. expect(p(2, 4)).toBe(6);
  22. expect(p(2, 4, true)).toBe(8);
  23. });
  24. });
  25. describe("[[Call]] invariants", () => {
  26. test("target must have a [[Call]] slot", () => {
  27. [{}, [], new Proxy({}, {})].forEach(item => {
  28. expect(() => {
  29. new Proxy(item, {})();
  30. }).toThrowWithMessage(TypeError, "[object ProxyObject] is not a function");
  31. });
  32. });
  33. });