Object.create.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. test("length is 2", () => {
  2. expect(Object.create).toHaveLength(2);
  3. });
  4. describe("errors", () => {
  5. test("non-object protpotype value", () => {
  6. expect(() => Object.create(42)).toThrowWithMessage(
  7. TypeError,
  8. "Prototype must be an object or null"
  9. );
  10. });
  11. });
  12. describe("normal behavior", () => {
  13. test("creates object with given prototype", () => {
  14. let o;
  15. o = Object.create(null);
  16. expect(o).toEqual({});
  17. expect(Object.getPrototypeOf(o)).toBe(null);
  18. const p = {};
  19. o = Object.create(p);
  20. expect(o).toEqual({});
  21. expect(Object.getPrototypeOf(o)).toBe(p);
  22. });
  23. test("creates object with properties from propertiesObject, if given", () => {
  24. const o = Object.create(
  25. {},
  26. {
  27. foo: {
  28. writable: true,
  29. configurable: true,
  30. value: "foo",
  31. },
  32. bar: {
  33. enumerable: true,
  34. value: "bar",
  35. },
  36. }
  37. );
  38. expect(Object.getOwnPropertyNames(o)).toEqual(["foo", "bar"]);
  39. expect(Object.getOwnPropertyDescriptor(o, "foo")).toEqual({
  40. value: "foo",
  41. writable: true,
  42. enumerable: false,
  43. configurable: true,
  44. });
  45. expect(Object.getOwnPropertyDescriptor(o, "bar")).toEqual({
  46. value: "bar",
  47. writable: false,
  48. enumerable: true,
  49. configurable: false,
  50. });
  51. });
  52. });