class-expressions.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. test("basic functionality", () => {
  2. const A = class {
  3. constructor(x) {
  4. this.x = x;
  5. }
  6. getX() {
  7. return this.x * 2;
  8. }
  9. };
  10. expect(new A(10).getX()).toBe(20);
  11. });
  12. test("inline instantiation", () => {
  13. // prettier-ignore
  14. const a = new class {
  15. constructor() {
  16. this.x = 10;
  17. }
  18. getX() {
  19. return this.x * 2;
  20. }
  21. };
  22. expect(a.getX()).toBe(20);
  23. });
  24. test("inline instantiation with argument", () => {
  25. // prettier-ignore
  26. const a = new class {
  27. constructor(x) {
  28. this.x = x;
  29. }
  30. getX() {
  31. return this.x * 2;
  32. }
  33. }(10);
  34. expect(a.getX()).toBe(20);
  35. });
  36. test("extending class expressions", () => {
  37. class A extends class {
  38. constructor(x) {
  39. this.x = x;
  40. }
  41. } {
  42. constructor(y) {
  43. super(y);
  44. this.y = y * 2;
  45. }
  46. }
  47. const a = new A(10);
  48. expect(a.x).toBe(10);
  49. expect(a.y).toBe(20);
  50. });
  51. test("class expression name", () => {
  52. let A = class {};
  53. expect(A.name).toBe("A");
  54. let B = class C {};
  55. expect(B.name).toBe("C");
  56. });