class-static-getters.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. describe("correct behavior", () => {
  2. test("basic functionality", () => {
  3. class A {
  4. static get x() {
  5. return 10;
  6. }
  7. }
  8. expect(A.x).toBe(10);
  9. expect(new A()).not.toHaveProperty("x");
  10. });
  11. test("name", () => {
  12. class A {
  13. static get x() {}
  14. }
  15. const d = Object.getOwnPropertyDescriptor(A, "x");
  16. expect(d.get.name).toBe("get x");
  17. });
  18. test("extended name syntax", () => {
  19. const s = Symbol("foo");
  20. class A {
  21. static get "method with space"() {
  22. return 1;
  23. }
  24. static get 12() {
  25. return 2;
  26. }
  27. static get [`he${"llo"}`]() {
  28. return 3;
  29. }
  30. static get [s]() {
  31. return 4;
  32. }
  33. }
  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 static getter", () => {
  40. class Parent {
  41. static get x() {
  42. return 3;
  43. }
  44. }
  45. class Child extends Parent {}
  46. expect(Parent.x).toBe(3);
  47. expect(Child.x).toBe(3);
  48. });
  49. test("inherited static getter overriding", () => {
  50. class Parent {
  51. static get x() {
  52. return 3;
  53. }
  54. }
  55. class Child extends Parent {
  56. static get x() {
  57. return 10;
  58. }
  59. }
  60. expect(Parent.x).toBe(3);
  61. expect(Child.x).toBe(10);
  62. });
  63. });
  64. describe("errors", () => {
  65. test('"get static" is a syntax error', () => {
  66. expect(`
  67. class A {
  68. get static foo() {}
  69. }`).not.toEval();
  70. });
  71. });