Skip to content

Commit 01e11ec

Browse files
committed
Add tests for invert function to validate key-value swapping and handle invalid inputs
1 parent 75ce6cb commit 01e11ec

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎Sprint-2/interpret/invert.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const invert = require("./invert.js");
2+
3+
describe("invert()", () => {
4+
test("swaps keys and values for a single key-value pair", () => {
5+
expect(invert({ a: 1 })).toEqual({ 1: "a" });
6+
});
7+
8+
test("swaps keys and values for multiple key-value pairs", () => {
9+
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
10+
expect(invert({ a: "1", b: "2" })).toEqual({ 1: "a", 2: "b" });
11+
});
12+
13+
test("handles empty objects", () => {
14+
expect(invert({})).toEqual({});
15+
});
16+
17+
test("handles non-object invalid inputs gracefully", () => {
18+
expect(invert(null)).toEqual({});
19+
expect(invert(["a", "b"])).toEqual({});
20+
expect(invert("string")).toEqual({});
21+
});
22+
});

0 commit comments

Comments
 (0)