Reflect.has.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. test("length is 2", () => {
  2. expect(Reflect.has).toHaveLength(2);
  3. });
  4. describe("errors", () => {
  5. test("target must be an object", () => {
  6. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  7. expect(() => {
  8. Reflect.has(value);
  9. }).toThrowWithMessage(TypeError, `${value} is not an object`);
  10. });
  11. });
  12. });
  13. describe("normal behavior", () => {
  14. test("regular object has property", () => {
  15. expect(Reflect.has({})).toBeFalse();
  16. expect(Reflect.has({ undefined })).toBeTrue();
  17. expect(Reflect.has({ 0: "1" }, "0")).toBeTrue();
  18. expect(Reflect.has({ foo: "bar" }, "foo")).toBeTrue();
  19. expect(Reflect.has({ bar: "baz" }, "foo")).toBeFalse();
  20. expect(Reflect.has({}, "toString")).toBeTrue();
  21. });
  22. test("array has property", () => {
  23. expect(Reflect.has([])).toBeFalse();
  24. expect(Reflect.has([], 0)).toBeFalse();
  25. expect(Reflect.has([1, 2, 3], "0")).toBeTrue();
  26. expect(Reflect.has([1, 2, 3], 0)).toBeTrue();
  27. expect(Reflect.has([1, 2, 3], 1)).toBeTrue();
  28. expect(Reflect.has([1, 2, 3], 2)).toBeTrue();
  29. expect(Reflect.has([1, 2, 3], 3)).toBeFalse();
  30. expect(Reflect.has([], "pop")).toBeTrue();
  31. });
  32. test("string object has property", () => {
  33. expect(Reflect.has(new String())).toBeFalse();
  34. expect(Reflect.has(new String("foo"), "0")).toBeTrue();
  35. expect(Reflect.has(new String("foo"), 0)).toBeTrue();
  36. expect(Reflect.has(new String("foo"), 1)).toBeTrue();
  37. expect(Reflect.has(new String("foo"), 2)).toBeTrue();
  38. expect(Reflect.has(new String("foo"), 3)).toBeFalse();
  39. expect(Reflect.has(new String("foo"), "charAt")).toBeTrue();
  40. });
  41. });