class-static-initializers.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. test("basic functionality", () => {
  2. var called = false;
  3. class A {
  4. static {
  5. expect(called).toBeFalse();
  6. expect(this.name).toBe("A");
  7. called = true;
  8. }
  9. }
  10. expect(called).toBeTrue();
  11. new A();
  12. expect(called).toBeTrue();
  13. });
  14. test("called in order", () => {
  15. var i = 0;
  16. class A {
  17. static {
  18. expect(i).toBe(0);
  19. i++;
  20. }
  21. static method() {
  22. return 2;
  23. }
  24. static {
  25. expect(i).toBe(1);
  26. i++;
  27. }
  28. }
  29. expect(i).toBe(2);
  30. new A();
  31. expect(i).toBe(2);
  32. });
  33. test("correct this", () => {
  34. var thisValue = null;
  35. class A {
  36. static {
  37. thisValue = this;
  38. }
  39. }
  40. expect(thisValue).toBe(A);
  41. });
  42. describe("class like constructs can be used inside", () => {
  43. test("can use new.target", () => {
  44. let value = 1;
  45. class C {
  46. static {
  47. value = new.target;
  48. }
  49. }
  50. expect(value).toBeUndefined();
  51. });
  52. test("can use super property lookup", () => {
  53. function parent() {}
  54. parent.val = 3;
  55. let hit = false;
  56. class C extends parent {
  57. static {
  58. hit = true;
  59. expect(super.val).toBe(3);
  60. }
  61. }
  62. expect(hit).toBeTrue();
  63. });
  64. });