|
| 1 | +const invert = require("./invert.js"); |
| 2 | + |
| 3 | +describe("invert function", () => { |
| 4 | + test("inverts a simple object with one key-value pair", () => { |
| 5 | + expect(invert({ a: 1 })).toEqual({ "1": "a" }); |
| 6 | + }); |
| 7 | + |
| 8 | + test("inverts an object with multiple key-value pairs", () => { |
| 9 | + expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); |
| 10 | + }); |
| 11 | + |
| 12 | + test("inverts an object with string values", () => { |
| 13 | + expect(invert({ name: "John", city: "New York" })).toEqual({ |
| 14 | + "John": "name", |
| 15 | + "New York": "city" |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + test("inverts an object with mixed value types", () => { |
| 20 | + expect(invert({ x: 10, y: "hello", z: true })).toEqual({ |
| 21 | + "10": "x", |
| 22 | + "hello": "y", |
| 23 | + "true": "z" |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + test("handles empty object", () => { |
| 28 | + expect(invert({})).toEqual({}); |
| 29 | + }); |
| 30 | + |
| 31 | + test("handles object with duplicate values (later ones overwrite)", () => { |
| 32 | + expect(invert({ a: 1, b: 1 })).toEqual({ "1": "b" }); |
| 33 | + }); |
| 34 | + |
| 35 | + test("handles object with numeric keys", () => { |
| 36 | + const result = invert({ 1: "one", 2: "two" }); |
| 37 | + expect(result).toEqual({ one: "1", two: "2" }); |
| 38 | + }); |
| 39 | + |
| 40 | + test("preserves all properties", () => { |
| 41 | + const original = { color: "red", size: "large", price: 100 }; |
| 42 | + const inverted = invert(original); |
| 43 | + |
| 44 | + // Check that all original values become keys in the inverted object |
| 45 | + // Sort the arrays to compare regardless of order (numeric keys are sorted differently) |
| 46 | + expect(Object.keys(inverted).sort()).toEqual(["red", "large", "100"].sort()); |
| 47 | + // Check that inverted values are the original keys |
| 48 | + expect(Object.values(inverted).sort()).toEqual(["color", "size", "price"].sort()); |
| 49 | + }); |
| 50 | +}); |
0 commit comments