-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server-testing): support variants in tests
- Loading branch information
Showing
16 changed files
with
360 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
type Room, | ||
type State, | ||
type ServerPlayerMessage, | ||
defineRoom, | ||
} from "@cardsgame/server" | ||
|
||
import { executeEvent, executeEventSetup } from "../executeEvent.js" | ||
import { makeInteraction } from "../makeInteraction.js" | ||
|
||
const roomConstructor = defineRoom("TestingRoom", {}) | ||
let room: Room<State> | ||
let event: ServerPlayerMessage | ||
|
||
const roomGetter = () => room | ||
|
||
beforeEach(() => { | ||
room = new roomConstructor() | ||
room.handleMessage = jest.fn() | ||
room.onCreate() | ||
|
||
event = makeInteraction(room.state, { type: "state" }, "tap") | ||
}) | ||
|
||
describe("calls room.handleMessage", () => { | ||
test("executeEvent", () => { | ||
executeEvent(room, event) | ||
|
||
expect(room.handleMessage).toHaveBeenCalledWith(event) | ||
}) | ||
test("executeEventInner", () => { | ||
const executeEventInner = executeEventSetup(roomGetter) | ||
executeEventInner(event) | ||
|
||
expect(room.handleMessage).toHaveBeenCalledWith(event) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { defineRoom, Room, State } from "@cardsgame/server" | ||
|
||
import { Reset, reset, resetSetup } from "../reset" | ||
|
||
import { TestState } from "./helpers" | ||
|
||
const onInitGameMock = jest.fn() | ||
const stateConstructor = TestState | ||
const roomConstructor = defineRoom("TestingRoom", { | ||
onInitGame: onInitGameMock, | ||
stateConstructor, | ||
}) | ||
|
||
let state: State | ||
let room: Room<State> | ||
let resetInner: Reset<State, Room<State>> | ||
const callback = jest.fn((resetRoom, resetState) => { | ||
room = resetRoom | ||
state = resetState | ||
}) | ||
|
||
beforeEach(() => { | ||
resetInner = resetSetup(callback, roomConstructor) | ||
|
||
onInitGameMock.mockReset() | ||
}) | ||
|
||
describe("resetSetup", () => { | ||
it("makes it call callback with room and new state", () => { | ||
resetInner() | ||
|
||
expect(callback).toHaveBeenCalledWith(room, state) | ||
}) | ||
|
||
it("uses correct room constructor", () => { | ||
resetInner() | ||
|
||
expect(room instanceof roomConstructor).toBe(true) | ||
|
||
resetInner = resetSetup(callback) | ||
resetInner() | ||
|
||
expect(room instanceof roomConstructor).toBe(false) | ||
}) | ||
|
||
it("uses correct state constructor", () => { | ||
resetInner() | ||
|
||
expect(state instanceof TestState).toBe(true) | ||
expect(state.name).toBe("CustomTestState") | ||
|
||
resetInner = resetSetup(callback) | ||
resetInner() | ||
|
||
expect(state instanceof TestState).toBe(false) | ||
expect(state instanceof State).toBe(true) | ||
expect(state.name).toBe("Unnamed") | ||
}) | ||
}) | ||
|
||
describe("reset", () => {}) | ||
|
||
describe("callOnInitGame", () => { | ||
it("overwrites room.onInitGame", () => { | ||
resetInner({ callOnInitGame: false }) | ||
expect(onInitGameMock).not.toHaveBeenCalled() | ||
}) | ||
it("allows to call original room.onInitGame", () => { | ||
resetInner({ callOnInitGame: true }) | ||
expect(onInitGameMock).toHaveBeenCalled() | ||
}) | ||
}) |
Oops, something went wrong.