Proxy.revocable.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. test("length is 2", () => {
  2. expect(Proxy.revocable).toHaveLength(2);
  3. });
  4. describe("errors", () => {
  5. test("constructor argument count", () => {
  6. expect(() => {
  7. Proxy.revocable();
  8. }).toThrowWithMessage(
  9. TypeError,
  10. "Expected target argument of Proxy constructor to be object, got undefined"
  11. );
  12. expect(() => {
  13. Proxy.revocable({});
  14. }).toThrowWithMessage(
  15. TypeError,
  16. "Expected handler argument of Proxy constructor to be object, got undefined"
  17. );
  18. });
  19. test("constructor requires objects", () => {
  20. expect(() => {
  21. Proxy.revocable(1, {});
  22. }).toThrowWithMessage(
  23. TypeError,
  24. "Expected target argument of Proxy constructor to be object, got 1"
  25. );
  26. expect(() => {
  27. Proxy.revocable({}, 1);
  28. }).toThrowWithMessage(
  29. TypeError,
  30. "Expected handler argument of Proxy constructor to be object, got 1"
  31. );
  32. });
  33. });
  34. describe("normal behavior", () => {
  35. test("returns object with 'proxy' and 'revoke' properties", () => {
  36. const revocable = Proxy.revocable(
  37. {},
  38. {
  39. get() {
  40. return 42;
  41. },
  42. }
  43. );
  44. expect(typeof revocable).toBe("object");
  45. expect(Object.getPrototypeOf(revocable)).toBe(Object.prototype);
  46. expect(revocable.hasOwnProperty("proxy")).toBeTrue();
  47. expect(revocable.hasOwnProperty("revoke")).toBeTrue();
  48. expect(typeof revocable.revoke).toBe("function");
  49. // Can't `instanceof Proxy`, but this should do the trick :^)
  50. expect(revocable.proxy.foo).toBe(42);
  51. });
  52. test("'revoke' function revokes Proxy", () => {
  53. const revocable = Proxy.revocable({}, {});
  54. expect(revocable.proxy.foo).toBeUndefined();
  55. expect(revocable.revoke()).toBeUndefined();
  56. expect(() => {
  57. revocable.proxy.foo;
  58. }).toThrowWithMessage(TypeError, "An operation was performed on a revoked Proxy object");
  59. });
  60. test("'revoke' called multiple times is a noop", () => {
  61. const revocable = Proxy.revocable({}, {});
  62. expect(revocable.revoke()).toBeUndefined();
  63. expect(revocable.revoke()).toBeUndefined();
  64. });
  65. });