class-static.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. });