Reflect.ownKeys.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. test("length is 1", () => {
  2. expect(Reflect.ownKeys).toHaveLength(1);
  3. });
  4. describe("errors", () => {
  5. test("target must be an object", () => {
  6. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  7. expect(() => {
  8. Reflect.ownKeys(value);
  9. }).toThrowWithMessage(
  10. TypeError,
  11. "First argument of Reflect.ownKeys() must be an object"
  12. );
  13. });
  14. });
  15. });
  16. describe("normal behavior", () => {
  17. test("regular empty object has no own keys", () => {
  18. var objectOwnKeys = Reflect.ownKeys({});
  19. expect(objectOwnKeys instanceof Array).toBeTrue();
  20. expect(objectOwnKeys).toHaveLength(0);
  21. });
  22. test("regular object with some properties has own keys", () => {
  23. var objectOwnKeys = Reflect.ownKeys({ foo: "bar", bar: "baz", 0: 42 });
  24. expect(objectOwnKeys instanceof Array).toBeTrue();
  25. expect(objectOwnKeys).toHaveLength(3);
  26. expect(objectOwnKeys[0]).toBe("0");
  27. expect(objectOwnKeys[1]).toBe("foo");
  28. expect(objectOwnKeys[2]).toBe("bar");
  29. });
  30. test("empty array has only 'length' own key", () => {
  31. var arrayOwnKeys = Reflect.ownKeys([]);
  32. expect(arrayOwnKeys instanceof Array).toBeTrue();
  33. expect(arrayOwnKeys).toHaveLength(1);
  34. expect(arrayOwnKeys[0]).toBe("length");
  35. });
  36. test("array with some values has 'lenght' and indices own keys", () => {
  37. var arrayOwnKeys = Reflect.ownKeys(["foo", [], 123, undefined]);
  38. expect(arrayOwnKeys instanceof Array).toBeTrue();
  39. expect(arrayOwnKeys).toHaveLength(5);
  40. expect(arrayOwnKeys[0]).toBe("0");
  41. expect(arrayOwnKeys[1]).toBe("1");
  42. expect(arrayOwnKeys[2]).toBe("2");
  43. expect(arrayOwnKeys[3]).toBe("3");
  44. expect(arrayOwnKeys[4]).toBe("length");
  45. });
  46. });