| 1 |
import each from "jest-each"; |
| 2 |
|
| Warning |
Row 3, Column 10: "'anyString' is defined but never used."
@typescript-eslint/no-unused-vars
|
| 3 |
import { anyString, anything, instance, mock, verify, when } from "ts-mockito"; |
| 4 |
|
| 5 |
import { ComponentName, Engine, Scheduler } from "jga-games-engine"; |
| 6 |
import { Directions, GameMap } from "jga-games-maps"; |
| 7 |
import { Display } from "jga-games-display"; |
| 8 |
import { GlyphInterface } from "jga-games-glyphs"; |
| 9 |
import { Keys } from "jga-games-commands"; |
| 10 |
import { Roll } from "jga-games-opend6-dices"; |
| 11 |
import { TileInterface } from "jga-games-tiles"; |
| 12 |
|
| 13 |
import { PlayScreen } from "@roguelike/index"; |
| 14 |
|
| 15 |
describe("PlayScreen", () => { |
| 16 |
let glyphMock: GlyphInterface; |
| 17 |
let tileMock: TileInterface; |
| 18 |
let gameMapMock: GameMap; |
| 19 |
let engine: Engine; |
| 20 |
let displayMock: Display; |
| 21 |
|
| 22 |
const mockDisplay: Display = mock<Display>(); |
| 23 |
const mockGlyph: GlyphInterface = mock<GlyphInterface>(); |
| 24 |
const mockTile: TileInterface = mock<TileInterface>(); |
| 25 |
const mockGameMap: GameMap = mock<GameMap>(); |
| 26 |
|
| 27 |
beforeEach(() => { |
| 28 |
when(mockTile.getGlyph()).thenReturn(glyphMock); |
| 29 |
|
| 30 |
displayMock = instance(mockDisplay); |
| 31 |
glyphMock = instance(mockGlyph); |
| 32 |
tileMock = instance(mockTile); |
| 33 |
|
| 34 |
when(mockGameMap.getTile(anything())).thenReturn(tileMock); |
| 35 |
|
| 36 |
gameMapMock = instance(mockGameMap); |
| 37 |
|
| 38 |
engine = new Engine(new Scheduler(), gameMapMock); |
| 39 |
|
| 40 |
engine.createPlayer(); |
| 41 |
|
| 42 |
jest.clearAllMocks(); |
| 43 |
}); |
| 44 |
|
| 45 |
it("should be instantiable", () => { |
| 46 |
const screen = new PlayScreen(engine, displayMock); |
| 47 |
expect(screen).not.toBeNull(); |
| 48 |
}); |
| 49 |
|
| 50 |
// eslint-disable-next-line jest/expect-expect |
| 51 |
it("should be able to render", () => { |
| 52 |
const screen = new PlayScreen(engine, displayMock); |
| 53 |
|
| 54 |
screen.enter(); |
| 55 |
screen.render(new Display(24, 80)); |
| 56 |
|
| 57 |
verify(mockDisplay.draw(anything(), anything())).times(101); |
| 58 |
}); |
| 59 |
|
| 60 |
it("should be able to retrieve current x position", () => { |
| 61 |
const screen = new PlayScreen(engine, displayMock); |
| 62 |
const player = engine.getPlayer(); |
| 63 |
const expected = player.getComponent(ComponentName.POSITION).getPosition().x; |
| 64 |
|
| 65 |
expect(screen.getCurrentX()).toBe(expected); |
| 66 |
}); |
| 67 |
|
| 68 |
it("should be able to retrieve current y position", () => { |
| 69 |
const screen = new PlayScreen(engine, displayMock); |
| 70 |
const player = engine.getPlayer(); |
| 71 |
const expected = player.getComponent(ComponentName.POSITION).getPosition().y; |
| 72 |
|
| 73 |
expect(screen.getCurrentY()).toBe(expected); |
| 74 |
}); |
| 75 |
|
| 76 |
describe("should be able to move", () => { |
| 77 |
each([["up"], ["down"], ["left"], ["right"], ["nowhere"]]).test(" %s", (direction) => { |
| 78 |
const screen = new PlayScreen(engine, displayMock); |
| 79 |
const player = engine.getPlayer(); |
| 80 |
|
| 81 |
screen.enter(); |
| 82 |
screen.move(direction); |
| 83 |
|
| 84 |
const expectedX = Math.max( |
| 85 |
0, |
| 86 |
Math.min(gameMapMock.getWidth() - 1, player.getComponent(ComponentName.POSITION).getPosition().x), |
| 87 |
); |
| 88 |
|
| 89 |
const expectedY = Math.max( |
| 90 |
0, |
| 91 |
Math.min(gameMapMock.getHeight() - 1, player.getComponent(ComponentName.POSITION).getPosition().y), |
| 92 |
); |
| 93 |
|
| 94 |
expect(screen.getCurrentX()).toEqual(expectedX); |
| 95 |
expect(screen.getCurrentY()).toEqual(expectedY); |
| 96 |
}); |
| 97 |
}); |
| 98 |
|
| 99 |
describe("exit", () => { |
| 100 |
it("should return void", () => { |
| 101 |
const screen = new PlayScreen(engine, displayMock); |
| 102 |
|
| 103 |
expect(screen.exit()).toBeUndefined(); |
| 104 |
}); |
| 105 |
}); |
| 106 |
|
| 107 |
describe("handleInput", () => { |
| 108 |
it("should return null if `inputType` is different from `keydown`", () => { |
| 109 |
const screen = new PlayScreen(engine, displayMock); |
| 110 |
|
| 111 |
expect(screen.handleInput("keyup")).toBeNull(); |
| 112 |
}); |
| 113 |
|
| 114 |
it("should return null if `code` is not mapped", () => { |
| 115 |
const screen = new PlayScreen(engine, displayMock); |
| 116 |
|
| 117 |
expect(screen.handleInput("keydown", { code: "Digit8" })).toBeNull(); |
| 118 |
}); |
| 119 |
|
| 120 |
it("should roll if `cpde` is `KeyR`", () => { |
| 121 |
// const rollGetResultSpy = jest.spyOn(Roll.prototype, "getResult"); |
| 122 |
const screenNotifySpy = jest.spyOn(PlayScreen.prototype, "notify"); |
| 123 |
const screen = new PlayScreen(engine, displayMock); |
| 124 |
const expected = 6; |
| 125 |
|
| 126 |
Roll.prototype.getResult = jest.fn().mockReturnValue(expected); |
| 127 |
|
| 128 |
screen.handleInput("keydown", { code: "KeyR" }); |
| 129 |
|
| 130 |
// expect(rollGetResultSpy).toHaveBeenCalledTimes(1); |
| 131 |
expect(screenNotifySpy).toHaveBeenCalledWith(`Roll => ${expected}`); |
| 132 |
}); |
| 133 |
|
| 134 |
each([ |
| 135 |
[Keys.ESCAPE, "lose"], |
| 136 |
[Keys.RETURN, "win"], |
| 137 |
]).test("code `%s` should return `%s`", (code, expected) => { |
| 138 |
const screen = new PlayScreen(engine, displayMock); |
| 139 |
const result = screen.handleInput("keydown", { code }); |
| 140 |
|
| 141 |
expect(result).toBe(expected); |
| 142 |
}); |
| 143 |
|
| 144 |
each([ |
| 145 |
[Keys.UP, Directions.UP], |
| 146 |
[Keys.DOWN, Directions.DOWN], |
| 147 |
[Keys.LEFT, Directions.LEFT], |
| 148 |
[Keys.RIGHT, Directions.RIGHT], |
| 149 |
]).test("code `%s` should move `%s`", (code, direction) => { |
| 150 |
const screenMoveSpy = jest.spyOn(PlayScreen.prototype, "move"); |
| 151 |
const screenNotifySpy = jest.spyOn(PlayScreen.prototype, "notify"); |
| 152 |
|
| 153 |
const screen = new PlayScreen(engine, displayMock); |
| 154 |
// screen.enter(); |
| 155 |
|
| 156 |
screen.handleInput("keydown", { code }); |
| 157 |
|
| 158 |
expect(screenMoveSpy).toHaveBeenCalledWith(direction); |
| 159 |
expect(screenNotifySpy).toHaveBeenCalledWith(`Move ${direction}`); |
| 160 |
}); |
| 161 |
}); |
| 162 |
}); |
| 163 |
|