Proxy.handler-has.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. describe("[[Has]] trap normal behavior", () => {
  2. test("forwarding when not defined in handler", () => {
  3. expect("foo" in new Proxy({}, { has: null })).toBe(false);
  4. expect("foo" in new Proxy({}, { has: undefined})).toBe(false);
  5. expect("foo" in new Proxy({}, {})).toBe(false);
  6. });
  7. test("correct arguments supplied to trap", () => {
  8. let o = {};
  9. let p = new Proxy(o, {
  10. has(target, prop) {
  11. expect(target).toBe(o);
  12. expect(prop).toBe("foo");
  13. return true;
  14. }
  15. });
  16. "foo" in p;
  17. });
  18. test("conditional return", () => {
  19. let o = {};
  20. let p = new Proxy(o, {
  21. has(target, prop) {
  22. if (target.checkedFoo)
  23. return true;
  24. if (prop === "foo")
  25. target.checkedFoo = true;
  26. return false;
  27. }
  28. });
  29. expect("foo" in p).toBe(false);
  30. expect("foo" in p).toBe(true);
  31. });
  32. });
  33. describe("[[Has]] invariants", () => {
  34. test("cannot return false if the property exists and is non-configurable", () => {
  35. let o = {};
  36. Object.defineProperty(o, "foo", { configurable: false });
  37. p = new Proxy(o, {
  38. has() {
  39. return false;
  40. }
  41. });
  42. expect(() => {
  43. "foo" in p;
  44. }).toThrowWithMessage(TypeError, "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target as a non-configurable property");
  45. });
  46. test("cannot return false if the property exists and the target is non-extensible", () => {
  47. let o = {};
  48. Object.defineProperty(o, "foo", { value: 10, configurable: true });
  49. let p = new Proxy(o, {
  50. has() {
  51. return false;
  52. }
  53. });
  54. Object.preventExtensions(o);
  55. expect(() => {
  56. "foo" in p;
  57. }).toThrowWithMessage(TypeError, "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target and the target is non-extensible");
  58. });
  59. });