class-static.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. test("basic functionality", () => {
  2. class A {
  3. static method() {
  4. return 10;
  5. }
  6. }
  7. expect(A.method()).toBe(10);
  8. expect(new A().method).toBeUndefined();
  9. });
  10. test("extended name syntax", () => {
  11. class A {
  12. static method() {
  13. return 1;
  14. }
  15. static 12() {
  16. return 2;
  17. }
  18. static [`he${"llo"}`]() {
  19. return 3;
  20. }
  21. }
  22. expect(A.method()).toBe(1);
  23. expect(A[12]()).toBe(2);
  24. expect(A.hello()).toBe(3);
  25. });
  26. test("bound |this|", () => {
  27. class A {
  28. static method() {
  29. expect(this).toBe(A);
  30. }
  31. }
  32. A.method();
  33. });
  34. test("inherited static methods", () => {
  35. class Parent {
  36. static method() {
  37. return 3;
  38. }
  39. }
  40. class Child extends Parent {}
  41. expect(Parent.method()).toBe(3);
  42. expect(Child.method()).toBe(3);
  43. expect(new Parent()).not.toHaveProperty("method");
  44. expect(new Child()).not.toHaveProperty("method");
  45. });
  46. test("static method overriding", () => {
  47. class Parent {
  48. static method() {
  49. return 3;
  50. }
  51. }
  52. class Child extends Parent {
  53. static method() {
  54. return 10;
  55. }
  56. }
  57. expect(Parent.method()).toBe(3);
  58. expect(Child.method()).toBe(10);
  59. });
  60. test("static function named 'async'", () => {
  61. class A {
  62. static async() {
  63. return "static function named async";
  64. }
  65. }
  66. expect("async" in A).toBeTrue();
  67. expect(A.async()).toBe("static function named async");
  68. });
  69. test("can call other private methods from static method", () => {
  70. class A {
  71. static #a() {
  72. return 1;
  73. }
  74. static async #b() {
  75. return 2;
  76. }
  77. static syncA() {
  78. return this.#a();
  79. }
  80. static async asyncA() {
  81. return this.#a();
  82. }
  83. static syncB() {
  84. return this.#b();
  85. }
  86. static async asyncB() {
  87. return this.#b();
  88. }
  89. }
  90. var called = false;
  91. async function check() {
  92. called = true;
  93. expect(A.syncA()).toBe(1);
  94. expect(await A.asyncA()).toBe(1);
  95. expect(await A.syncB()).toBe(2);
  96. expect(await A.asyncB()).toBe(2);
  97. return 3;
  98. }
  99. var error = null;
  100. var failed = false;
  101. check().then(
  102. value => {
  103. expect(called).toBeTrue();
  104. expect(value).toBe(3);
  105. },
  106. thrownError => {
  107. failed = true;
  108. error = thrownError;
  109. }
  110. );
  111. runQueuedPromiseJobs();
  112. expect(called).toBeTrue();
  113. if (failed) throw error;
  114. expect(failed).toBeFalse();
  115. });