class-getters.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. test("basic functionality", () => {
  2. class A {
  3. get x() {
  4. return 10;
  5. }
  6. }
  7. expect(A).not.toHaveProperty("x");
  8. expect(new A().x).toBe(10);
  9. });
  10. test("name", () => {
  11. class A {
  12. get x() {}
  13. }
  14. const d = Object.getOwnPropertyDescriptor(A.prototype, "x");
  15. expect(d.get.name).toBe("get x");
  16. });
  17. test("extended name syntax", () => {
  18. const s = Symbol("foo");
  19. class A {
  20. get "method with space"() {
  21. return 1;
  22. }
  23. get 12() {
  24. return 2;
  25. }
  26. get [`he${"llo"}`]() {
  27. return 3;
  28. }
  29. get [s]() {
  30. return 4;
  31. }
  32. }
  33. const a = new A();
  34. expect(a["method with space"]).toBe(1);
  35. expect(a[12]).toBe(2);
  36. expect(a.hello).toBe(3);
  37. expect(a[s]).toBe(4);
  38. });
  39. test("inherited getter", () => {
  40. class Parent {
  41. get x() {
  42. return 3;
  43. }
  44. }
  45. class Child extends Parent {}
  46. expect(Child).not.toHaveProperty("x");
  47. expect(new Child().x).toBe(3);
  48. });
  49. test("inherited getter overriding", () => {
  50. class Parent {
  51. get x() {
  52. return 3;
  53. }
  54. }
  55. class Child extends Parent {
  56. get x() {
  57. return 10;
  58. }
  59. }
  60. expect(new Child().x).toBe(10);
  61. });