1.0.41 • Published 1 month ago

ttpg-mock v1.0.41

Weekly downloads
-
License
Unlicense
Repository
github
Last release
1 month ago

ttpg-mock

Mock @tabletop-playground/api for TypeScript-compatible jest unittesting. Write your scripts normally, jest will redirect that api reference to the mocked version.

!Note Your normal build process will never use any ttpg-mock assets, those are only used during jest test runs (your test scripts may import them to access mock classes; production scripts never will).

Using the package manager of your choice, install (yarn in this case):

yarn add -D ttpg-mock
yarn add -D three

Create or edit your jest.config.js file to have this moduleNameMapper entry, redirecting the TTPG api to our mock version:

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",

  moduleNameMapper: {
    "^@tabletop-playground/api$": "ttpg-mock",
  },
};

Edd a "test" entry to the scripts in your package.json:

{
  ...
  "scripts": {
    ...
    "test": "jest"
    ...
  },
  ...
}

Run jest with a .test.ts file or directory:

% yarn test src/.../my-file.test.ts

Environment

There may be places your scripts want to behave differently when under test (e.g., setTimeout). You can check GameWorld.getExectutionReason() === "unittest".

Mock classes

When needed, create mock instances of objects often filling any necessary state in the constructor. Import mock classes from ttpg-mock, for instance:

import { MockGameObject, MockGameObjectParams } from "ttpg-mock";

it("mock class", () => {
    const params: MockGameObjectParams = {
        position: [1, 0, 0],
        templateMetadata: "my-metadata",
    };
    const obj: GameObject = new MockGameObject(params);
    expect(obj.getPosition().magnitude()).toEqual(1);
    expect(obj.getTemplateMetadata()).toEqual("my-metadata");
});

Populating the world

Mock GameObjects, tables, and Players are registered with world automatically. You can explicitly set the available items using mockWorld (same object as world but with some new methods exposed):

import { GameObject, world } from "@tabletop-playground/api";
import { MockGameObject, mockWorld } from "ttpg-mock";

it("mockWorld._reset", () => {
    const obj: GameObject = new MockGameObject();
    mockWorld._reset({ gameObjects: [obj] });
    expect(world.getAllObjects()).toEqual([obj]);

    mockWorld._reset(); // clears everything
});

Events

Some events like onObjectCreated and onPlayerJoined are sent when creating a new GameObject or Player. Likewise GameObject.destroy sends onObjectDestroyed and Player.switchSlot sends onPlayerSwitchedSlots.

Many event senders are available when casting to the mock class, e.g.:

  • MockButton._clickAsPlayer
  • MockContentButton._clickAsPlayer
  • MockImageButton._clickAsPlayer
  • MockCard._addCardsAsPlayer
  • MockCard._takeCardsAsPlayer
  • MockContainer._addObjectsAsPlayer
  • MockContainer._takeAsPlayer
  • MockGameObject._flipOrUprightAsPlayer
  • MockGameObject._grabAsPlayer
  • MockGameObject._releaseAsPlayer
  • MockGameObject._snapAsPlayer (GameObject.onSnapped, StaticObject.onSnappedTo)
  • MockGameObject._primaryActionAsPlayer
  • MockGameObject._secondaryActionAsPlayer
  • MockGameObject._numberActionAsPlayer
  • MockGameObject._customActionAsPlayer
  • mockGlobalEvents._chatMessageAsPlayer
  • mockGlobalEvents._customActionAsPlayer(): void {}
  • mockGlobalEvents._diceRolledAsPlayer(): void {}
  • mockGlobalEvents._scriptButtonPressedAsPlayer(): void {}
  • mockGlobalEvents._scriptButtonReleasedAsPlayer(): void {}
  • mockGlobalEvents._shakeAsPlayer(): void {}
  • mockGlobalEvents._teamChatAsPlayer(): void {}
  • mockGlobalEvents._whisperAsPlayer(): void {}

Moreover, Delegate and MulticastDelegate can be cast to their mock version to trigger events:

import { globalEvents } from "@tabletop-playground/api";
import { MockMulticastDelegate, MockPlayer } from "ttpg-mock";

it("events", () => {
    const fakePlayer = new MockPlayer();
    const fakeMessage = "hello";

    // Listen for onChatMessage, require the fake info.
    let listenerCalled = false;
    globalEvents.onChatMessage.add((sender, message) => {
        if (sender !== fakePlayer || message !== fakeMessage) {
            throw new Error("bad");
        }
        listenerCalled = true;
    });

    // Cast to the mock version to access _trigger.
    const mock = globalEvents.onChatMessage as MockMulticastDelegate<
        (sender: Player, message: string) => void
    >;
    mock._trigger(fakePlayer, fakeMessage);
    expect(listenerCalled).toEqual(true);

    globalEvents.onChatMessage.clear();
});
1.0.41

1 month ago

1.0.39

3 months ago

1.0.38

3 months ago

1.0.40

3 months ago

1.0.37

3 months ago

1.0.33

4 months ago

1.0.32

4 months ago

1.0.31

4 months ago

1.0.36

3 months ago

1.0.35

4 months ago

1.0.34

4 months ago

1.0.26

4 months ago

1.0.29

4 months ago

1.0.28

4 months ago

1.0.27

4 months ago

1.0.30

4 months ago

1.0.25

4 months ago

1.0.24

4 months ago

1.0.23

4 months ago

1.0.22

4 months ago

1.0.21

4 months ago

1.0.20

4 months ago

1.0.19

4 months ago

1.0.18

4 months ago

1.0.17

4 months ago

1.0.16

4 months ago

1.0.15

4 months ago

1.0.14

4 months ago

1.0.13

4 months ago

1.0.12

5 months ago

1.0.11

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.10

5 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago