ordinary-to-primitive.js 740 B

1234567891011121314151617181920212223
  1. test("object with custom toString", () => {
  2. const o = { toString: () => "foo" };
  3. expect(o + "bar").toBe("foobar");
  4. expect([o, "bar"].toString()).toBe("foo,bar");
  5. });
  6. test("object with uncallable toString and custom valueOf", () => {
  7. const o = { toString: undefined, valueOf: () => "foo" };
  8. expect(o + "bar").toBe("foobar");
  9. expect([o, "bar"].toString()).toBe("foo,bar");
  10. });
  11. test("object with custom valueOf", () => {
  12. const o = { valueOf: () => 42 };
  13. expect(Number(o)).toBe(42);
  14. expect(o + 1).toBe(43);
  15. });
  16. test("object with uncallable valueOf and custom toString", () => {
  17. const o = { valueOf: undefined, toString: () => "42" };
  18. expect(Number(o)).toBe(42);
  19. expect(o + 1).toBe("421");
  20. });