Object.values.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. describe("correct behavior", () => {
  2. test("lengths", () => {
  3. expect(Object.values).toHaveLength(1);
  4. expect(Object.values(true)).toHaveLength(0);
  5. expect(Object.values(45)).toHaveLength(0);
  6. expect(Object.values(-998)).toHaveLength(0);
  7. expect(Object.values("abcd")).toHaveLength(4);
  8. expect(Object.values([1, 2, 3])).toHaveLength(3);
  9. expect(Object.values({ a: 1, b: 2, c: 3 })).toHaveLength(3);
  10. });
  11. test("object argument", () => {
  12. let values = Object.values({ foo: 1, bar: 2, baz: 3 });
  13. expect(values).toEqual([1, 2, 3]);
  14. });
  15. test("object argument with symbol keys", () => {
  16. let values = Object.values({ foo: 1, [Symbol("bar")]: 2, baz: 3 });
  17. expect(values).toEqual([1, 3]);
  18. });
  19. test("array argument", () => {
  20. let values = Object.values(["a", "b", "c"]);
  21. expect(values).toEqual(["a", "b", "c"]);
  22. });
  23. test("ignores non-enumerable properties", () => {
  24. let obj = { foo: 1 };
  25. Object.defineProperty(obj, "getFoo", {
  26. value: function () {
  27. return this.foo;
  28. },
  29. });
  30. let values = Object.values(obj);
  31. expect(values).toEqual([1]);
  32. });
  33. });
  34. describe("errors", () => {
  35. test("null argument", () => {
  36. expect(() => {
  37. Object.values(null);
  38. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  39. });
  40. test("undefined argument", () => {
  41. expect(() => {
  42. Object.values(undefined);
  43. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  44. });
  45. });