class-methods.js 897 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. test("basic functionality", () => {
  2. class A {
  3. number() {
  4. return 2;
  5. }
  6. string() {
  7. return "foo";
  8. }
  9. }
  10. const a = new A();
  11. expect(a.number()).toBe(2);
  12. expect(a.string()).toBe("foo");
  13. });
  14. test("length", () => {
  15. class A {
  16. method1() {}
  17. method2(a, b, c, d) {}
  18. method3(a, b, ...c) {}
  19. }
  20. const a = new A();
  21. expect(a.method1).toHaveLength(0);
  22. expect(a.method2).toHaveLength(4);
  23. expect(a.method3).toHaveLength(2);
  24. });
  25. test("extended name syntax", () => {
  26. class A {
  27. "method with space"() {
  28. return 1;
  29. }
  30. 12() {
  31. return 2;
  32. }
  33. [`he${"llo"}`]() {
  34. return 3;
  35. }
  36. }
  37. const a = new A();
  38. expect(a["method with space"]()).toBe(1);
  39. expect(a[12]()).toBe(2);
  40. expect(a.hello()).toBe(3);
  41. });