|
| 1 | +import { omit } from "./omit"; |
| 2 | + |
| 3 | +describe("omit", () => { |
| 4 | + it("omits specified keys from a simple object", () => { |
| 5 | + const obj = { a: 1, b: 2, c: 3 }; |
| 6 | + const result = omit(obj, ["b"] as const); |
| 7 | + expect(result).toEqual({ a: 1, c: 3 }); |
| 8 | + }); |
| 9 | + |
| 10 | + it("returns a new object and does not mutate the source", () => { |
| 11 | + const obj = { a: 1, b: 2 } as const; |
| 12 | + const result = omit(obj, ["a"] as const); |
| 13 | + expect(result).toEqual({ b: 2 }); |
| 14 | + expect(obj).toEqual({ a: 1, b: 2 }); |
| 15 | + expect(result).not.toBe(obj); |
| 16 | + }); |
| 17 | + |
| 18 | + it("works with empty keys list", () => { |
| 19 | + const obj = { a: 1, b: 2 }; |
| 20 | + const result = omit(obj, [] as const); |
| 21 | + expect(result).toEqual({ a: 1, b: 2 }); |
| 22 | + }); |
| 23 | + |
| 24 | + it("ignores keys that are not present on the object", () => { |
| 25 | + const obj: Record<string, number> = { a: 1 }; |
| 26 | + const result = omit(obj, ["b"] as const); |
| 27 | + expect(result).toEqual({ a: 1 }); |
| 28 | + }); |
| 29 | + |
| 30 | + it("handles symbol keys by preserving them when not omitted", () => { |
| 31 | + const sym = Symbol("x"); |
| 32 | + // Our omit implementation uses Object.entries, which enumerates string keys. |
| 33 | + // This test documents current behavior: symbol-keyed properties are preserved |
| 34 | + // when not omitted and are also preserved when omitted since they are not enumerated. |
| 35 | + const obj = { a: 1, [sym]: 2 } as Record<PropertyKey, unknown> as { a: number } & { [k: symbol]: number }; |
| 36 | + const result = omit(obj as { a: number }, ["a"] as const); |
| 37 | + expect("a" in result).toBe(false); |
| 38 | + // The symbol property remains untouched on the original object |
| 39 | + expect((obj as any)[sym]).toBe(2); |
| 40 | + }); |
| 41 | +}); |
0 commit comments