Skip to content

Commit bb9be28

Browse files
committed
Write tests for invert.js
1 parent 1fe2ffa commit bb9be28

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const invert = require("./invert.js");
2+
3+
describe("invert", () => {
4+
test("should swap keys and values for a simple object", () => {
5+
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
6+
});
7+
8+
test("should work with a single key-value pair", () => {
9+
expect(invert({ a: 1 })).toEqual({ 1: "a" });
10+
});
11+
12+
test("should return an empty object when given an empty object", () => {
13+
expect(invert({})).toEqual({});
14+
});
15+
16+
test("should overwrite earlier keys when two values are the same", () => {
17+
// if two keys share the same value, the later key wins after inversion
18+
expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" });
19+
});
20+
21+
test("should coerce numeric and boolean values to string keys", () => {
22+
expect(invert({ a: true, b: false })).toEqual({ true: "a", false: "b" });
23+
});
24+
25+
test("should not mutate the original object", () => {
26+
const original = { a: 1, b: 2 };
27+
invert(original);
28+
expect(original).toEqual({ a: 1, b: 2 });
29+
});
30+
});

0 commit comments

Comments
 (0)