class-static-setters.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. describe("correct behavior", () => {
  2. test("basic functionality", () => {
  3. class A {
  4. static get x() {
  5. return this._x;
  6. }
  7. static set x(value) {
  8. this._x = value * 2;
  9. }
  10. }
  11. expect(A.x).toBeUndefined();
  12. expect(A).not.toHaveProperty("_x");
  13. A.x = 3;
  14. expect(A.x).toBe(6);
  15. expect(A).toHaveProperty("_x", 6);
  16. });
  17. test("name", () => {
  18. class A {
  19. static set x(v) {}
  20. }
  21. const d = Object.getOwnPropertyDescriptor(A, "x");
  22. expect(d.set.name).toBe("set x");
  23. });
  24. test("extended name syntax", () => {
  25. const s = Symbol("foo");
  26. class A {
  27. static set "method with space"(value) {
  28. this.a = value;
  29. }
  30. static set 12(value) {
  31. this.b = value;
  32. }
  33. static set [`he${"llo"}`](value) {
  34. this.c = value;
  35. }
  36. static set [s](value) {
  37. this.d = value;
  38. }
  39. }
  40. A["method with space"] = 1;
  41. A[12] = 2;
  42. A.hello = 3;
  43. A[s] = 4;
  44. expect(A.a).toBe(1);
  45. expect(A.b).toBe(2);
  46. expect(A.c).toBe(3);
  47. expect(A.d).toBe(4);
  48. });
  49. test("inherited static setter", () => {
  50. class Parent {
  51. static get x() {
  52. return this._x;
  53. }
  54. static set x(value) {
  55. this._x = value * 2;
  56. }
  57. }
  58. class Child extends Parent {}
  59. expect(Child.x).toBeUndefined();
  60. Child.x = 10;
  61. expect(Child.x).toBe(20);
  62. });
  63. test("inherited static setter overriding", () => {
  64. class Parent {
  65. static get x() {
  66. return this._x;
  67. }
  68. static set x(value) {
  69. this._x = value * 2;
  70. }
  71. }
  72. class Child extends Parent {
  73. static get x() {
  74. return this._x;
  75. }
  76. static set x(value) {
  77. this._x = value * 3;
  78. }
  79. }
  80. expect(Child.x).toBeUndefined();
  81. Child.x = 10;
  82. expect(Child.x).toBe(30);
  83. });
  84. });
  85. describe("errors", () => {
  86. test('"set static" is a syntax error', () => {
  87. expect(`
  88. class A {
  89. set static foo(value) {}
  90. }`).not.toEval();
  91. });
  92. });