|
| 1 | +const index = require("./index"); |
| 2 | +const solution = require("./solution"); |
| 3 | + |
| 4 | +// Use solution when TEST_SOLUTIONS is true, otherwise try to use index (may be empty) |
| 5 | +const { Robot, Humanoid } = process.env.TEST_SOLUTIONS ? solution : index || {}; |
| 6 | + |
| 7 | +describe("02 - Prototypes to Classes", () => { |
| 8 | + // Skip tests if Robot/Humanoid classes are not implemented |
| 9 | + const shouldSkip = !Robot || !Humanoid; |
| 10 | + |
| 11 | + describe("Robot", () => { |
| 12 | + (shouldSkip ? test.skip : test)( |
| 13 | + "creates a robot with name, abilities, and power", |
| 14 | + () => { |
| 15 | + const robot = new Robot("R2-D2", ["beep", "navigate"]); |
| 16 | + |
| 17 | + expect(robot.name).toBe("R2-D2"); |
| 18 | + expect(robot.abilities).toEqual(["beep", "navigate"]); |
| 19 | + expect(robot.power).toBe(100); |
| 20 | + }, |
| 21 | + ); |
| 22 | + |
| 23 | + (shouldSkip ? test.skip : test)( |
| 24 | + "announce method logs greetings and abilities", |
| 25 | + () => { |
| 26 | + const consoleSpy = jest.spyOn(console, "log").mockImplementation(); |
| 27 | + const robot = new Robot("C-3PO", ["translate", "worry"]); |
| 28 | + |
| 29 | + robot.announce(); |
| 30 | + |
| 31 | + expect(consoleSpy).toHaveBeenCalledWith("Greetings. I am C-3PO."); |
| 32 | + expect(consoleSpy).toHaveBeenCalledWith("I am able to translate."); |
| 33 | + expect(consoleSpy).toHaveBeenCalledWith("I am able to worry."); |
| 34 | + |
| 35 | + consoleSpy.mockRestore(); |
| 36 | + }, |
| 37 | + ); |
| 38 | + |
| 39 | + (shouldSkip ? test.skip : test)( |
| 40 | + "charge method increases power and logs status", |
| 41 | + () => { |
| 42 | + const consoleSpy = jest.spyOn(console, "log").mockImplementation(); |
| 43 | + const robot = new Robot("Wall-E", ["clean"]); |
| 44 | + robot.power = 50; |
| 45 | + |
| 46 | + robot.charge(30); |
| 47 | + |
| 48 | + expect(robot.power).toBe(80); |
| 49 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 50 | + "Recharged power supplies to 80.", |
| 51 | + ); |
| 52 | + |
| 53 | + consoleSpy.mockRestore(); |
| 54 | + }, |
| 55 | + ); |
| 56 | + |
| 57 | + (shouldSkip ? test.skip : test)( |
| 58 | + "charge method caps power at 100 and logs optimal capacity", |
| 59 | + () => { |
| 60 | + const consoleSpy = jest.spyOn(console, "log").mockImplementation(); |
| 61 | + const robot = new Robot("BB-8", ["roll"]); |
| 62 | + robot.power = 90; |
| 63 | + |
| 64 | + robot.charge(20); |
| 65 | + |
| 66 | + expect(robot.power).toBe(100); |
| 67 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 68 | + "Recharged power supplies to 100.", |
| 69 | + ); |
| 70 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 71 | + "I am at optimal operational capacity.", |
| 72 | + ); |
| 73 | + |
| 74 | + consoleSpy.mockRestore(); |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
| 78 | + (shouldSkip ? test.skip : test)( |
| 79 | + "move method reduces power and logs movement", |
| 80 | + () => { |
| 81 | + const consoleSpy = jest.spyOn(console, "log").mockImplementation(); |
| 82 | + const robot = new Robot("EVE", ["scan"]); |
| 83 | + |
| 84 | + robot.move(25); |
| 85 | + |
| 86 | + expect(robot.power).toBe(75); |
| 87 | + expect(consoleSpy).toHaveBeenCalledWith("Moving 25 units."); |
| 88 | + |
| 89 | + consoleSpy.mockRestore(); |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + (shouldSkip ? test.skip : test)( |
| 94 | + "move method prevents movement when insufficient power", |
| 95 | + () => { |
| 96 | + const consoleSpy = jest.spyOn(console, "log").mockImplementation(); |
| 97 | + const robot = new Robot("WALL-E", ["compact"]); |
| 98 | + robot.power = 10; |
| 99 | + |
| 100 | + robot.move(25); |
| 101 | + |
| 102 | + expect(robot.power).toBe(10); |
| 103 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 104 | + "I do not have enough power to move 25 units.", |
| 105 | + ); |
| 106 | + |
| 107 | + consoleSpy.mockRestore(); |
| 108 | + }, |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("Humanoid", () => { |
| 113 | + (shouldSkip ? test.skip : test)( |
| 114 | + "creates a humanoid with name, abilities, power, and catchphrase", |
| 115 | + () => { |
| 116 | + const humanoid = new Humanoid( |
| 117 | + "Data", |
| 118 | + ["compute", "analyze"], |
| 119 | + "Fascinating", |
| 120 | + ); |
| 121 | + |
| 122 | + expect(humanoid.name).toBe("Data"); |
| 123 | + expect(humanoid.abilities).toEqual(["compute", "analyze"]); |
| 124 | + expect(humanoid.power).toBe(100); |
| 125 | + expect(humanoid.catchphrase).toBe("Fascinating"); |
| 126 | + }, |
| 127 | + ); |
| 128 | + |
| 129 | + (shouldSkip ? test.skip : test)( |
| 130 | + "announce method logs greetings, abilities, and catchphrase", |
| 131 | + () => { |
| 132 | + const consoleSpy = jest.spyOn(console, "log").mockImplementation(); |
| 133 | + const humanoid = new Humanoid( |
| 134 | + "T-800", |
| 135 | + ["terminate", "protect"], |
| 136 | + "I'll be back", |
| 137 | + ); |
| 138 | + |
| 139 | + humanoid.announce(); |
| 140 | + |
| 141 | + expect(consoleSpy).toHaveBeenCalledWith("Greetings. I am T-800."); |
| 142 | + expect(consoleSpy).toHaveBeenCalledWith("I am able to terminate."); |
| 143 | + expect(consoleSpy).toHaveBeenCalledWith("I am able to protect."); |
| 144 | + expect(consoleSpy).toHaveBeenCalledWith(" > I'll be back <"); |
| 145 | + |
| 146 | + consoleSpy.mockRestore(); |
| 147 | + }, |
| 148 | + ); |
| 149 | + |
| 150 | + (shouldSkip ? test.skip : test)("inherits Robot methods correctly", () => { |
| 151 | + const humanoid = new Humanoid( |
| 152 | + "Vision", |
| 153 | + ["phase", "fly"], |
| 154 | + "I am inevitable", |
| 155 | + ); |
| 156 | + |
| 157 | + expect(typeof humanoid.charge).toBe("function"); |
| 158 | + expect(typeof humanoid.move).toBe("function"); |
| 159 | + expect(humanoid instanceof Robot).toBe(true); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments