Symbol.for.js 862 B

1234567891011121314151617181920212223
  1. test("basic functionality", () => {
  2. const localSym = Symbol("foo");
  3. const globalSym = Symbol.for("foo");
  4. expect(localSym).not.toBe(globalSym);
  5. expect(localSym).not.toBe(Symbol("foo"));
  6. expect(globalSym).not.toBe(Symbol("foo"));
  7. expect(globalSym).toBe(Symbol.for("foo"));
  8. expect(localSym.toString()).toBe("Symbol(foo)");
  9. expect(globalSym.toString()).toBe("Symbol(foo)");
  10. expect(Symbol.for(1).description).toBe("1");
  11. expect(Symbol.for(true).description).toBe("true");
  12. expect(Symbol.for({}).description).toBe("[object Object]");
  13. expect(Symbol.for().description).toBe("undefined");
  14. expect(Symbol.for(null).description).toBe("null");
  15. });
  16. test("symbol argument throws an error", () => {
  17. expect(() => {
  18. Symbol.for(Symbol());
  19. }).toThrowWithMessage(TypeError, "Cannot convert symbol to string");
  20. });