|
| 1 | +const invert = require("./invert.js"); |
1 | 2 |
|
| 3 | +/* |
| 4 | +Implement a function called invert that swaps the keys and values of an object. |
| 5 | +
|
| 6 | +E.g. invert({ x: 10, y: 20 }) |
| 7 | +// returns { "10": "x", "20": "y" } |
| 8 | +*/ |
| 9 | + |
| 10 | +// Acceptance criteria: |
| 11 | + |
| 12 | +// Given an empty object |
| 13 | +// When passed to invert |
| 14 | +// Then it should return an empty object |
| 15 | +test("returns an empty object when given an empty object", () => { |
| 16 | + expect(invert({})).toEqual({}); |
| 17 | +}); |
| 18 | + |
| 19 | +// Given an object with one property |
| 20 | +// When passed to invert |
| 21 | +// Then it should swap the key and value |
| 22 | +test("inverts an object with one key-value pair", () => { |
| 23 | + expect(invert({ a: 1 })).toEqual({ |
| 24 | + "1": "a", |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +// Given an object with multiple properties |
| 29 | +// When passed to invert |
| 30 | +// Then it should swap all keys and values |
| 31 | +test("inverts an object with multiple key-value pairs", () => { |
| 32 | + expect(invert({ a: 1, b: 2 })).toEqual({ |
| 33 | + "1": "a", |
| 34 | + "2": "b", |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +// Given an object with string values |
| 39 | +// When passed to invert |
| 40 | +// Then it should swap the keys and values |
| 41 | +test("inverts string values correctly", () => { |
| 42 | + expect(invert({ first: "apple", second: "banana" })).toEqual({ |
| 43 | + apple: "first", |
| 44 | + banana: "second", |
| 45 | + }); |
| 46 | +}); |
0 commit comments