class-static.js 1.3 KB

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