Skip to main content

@cardsgame/server-testing

Other Type Aliases​

EntityMockingDefinition​

Ƭ EntityMockingDefinition: EntityMockingProps & ChildrenMockingArray & { selected?: number[] ; type?: string }

An object describing any possible entity. This is not an Entity in itself.

Example

"D7" card placed with its face down:

{ type: "classicCard", suit: "D", rank: "3", faceUp: false }

Example

Some container with 2 cards, with second one being selected:

{
children: [
{ type: "classicCard", name: "D7" },
{ type: "classicCard", name: "D3" },
],
selected: [1]
}
note

name: "D7" is a shorthand. Given this object is of type "classicCard", this object will get populated with 2 new props: rank: "7" and suit: "D"


StateMockingRecord​

Ƭ StateMockingRecord<S>: StateMockingProps<S> & Partial<{ clients: string[] ; players: Partial<PlayerDefinition>[] }>

An object describing an initial root state. This definition may contain state's props like "isGameStarted".

To populate state with entities - use populateState()

Example

Game state at round 10, with 2 players playing.

{
isGameStarted: true,
round: 10,
players: [
{ clientID: CLIENT_ID, name: "Darek" },
{ clientID: "clientB", name: "Pau" },
],
}

Type parameters​

NameType
Sextends State

StateMockingTuple​

Ƭ StateMockingTuple: [QuerableProps | undefined, EntityMockingDefinition]


Setup Type Aliases​

SetupAPI​

Ƭ SetupAPI<S, R>: Object

Type parameters​

NameType
Sextends State
Rextends Room<S>

Type declaration​

NameTypeDescription
executeEventExecuteEventTest how your Action would modify the game state.
initStateInitState<S>Populate your state with initial props (including players & clients). Returns state object for further initial modification. To populate state with entities - prefer to use populateState()
makeEventMakeEvent-
makeInteractionMakeInteractionConstruct interaction event object for use in testEvent()
populateStatePopulateState<S>Populates state with new entities. Use AFTER you prepared the base state yourself by using your game's own state preparation functions.
resetReset<S, R>Resets room and state, using roomConstructor provided earlier with setupServerTesting. To be used with beforeEach() of testing frameworks. Without calling this function, for example populateState(...entitiesMap) might end up populating state object of previous test runs (which is bad!).
testEventTestEventTest if given event would pass in gameplay under current conditions.

SetupOptions​

Ƭ SetupOptions<S, R>: Object

Type parameters​

NameType
Sextends State
Rextends Room<S>

Type declaration​

NameTypeDescription
actionActionDefinition<S>Used only in testEvent(), don't have to provide if you won't use that function.
gameEntities?Record<string, EntityConstructor>List of custom entities present in your game, if any. Used to figure out entity constructor just by it's type from statePreparation
roomConstructor?RoomConstructor<S, R>Used to construct State automatically and in executeEvent(), don't have to provide if you won't use that function.

Variables​

CLIENT_ID​

• Const CLIENT_ID: "CLIENT"

Client ID with which most of the events will be automatically created. Assign it to the first player in your testing environment.

Other Functions​

initState​

▸ initState<S>(state, statePreparation): S

Populate your state with initial props (including players & clients). Returns state object for further initial modification.

To populate state with entities - prefer to use populateState()

Example

state = initState({
isGameStarted: true,
round: 10,
players: [
{ clientID: "clientA", name: "Darek" },
{ clientID: "clientB", name: "Alan" },
],
})
  • Would mark game as already started and set round to 10
  • And create 2 Player entries in state.players array

Type parameters​

NameType
Sextends State<Record<string, unknown>, S>

Parameters​

NameType
stateS
statePreparationStateMockingRecord<S>

Returns​

S


makeEvent​

▸ makeEvent<S>(state, messageType, data?): ServerPlayerMessage

Construct message event object for use in testEvent()

Type parameters​

NameType
Sextends State<Record<string, unknown>, S>

Parameters​

NameType
stateS
messageTypestring
data?unknown

Returns​

ServerPlayerMessage


makeInteraction​

▸ makeInteraction<S>(state, entityQuery, interaction?): ServerPlayerMessage

Construct interaction event object for use in testEvent()

Type parameters​

NameType
Sextends State<Record<string, unknown>, S>

Parameters​

NameTypeDefault value
stateSundefined
entityQueryQuerablePropsundefined
interactionInteractionType"tap"

Returns​

ServerPlayerMessage


populateState​

▸ populateState<S>(state, entitiesMap, gameEntities?): S

this function can be run independently of setupServerTesting()

Use AFTER you prepared the base state yourself by using your game's own state preparation functions. Modifies state in-place.

Example

populateState(
state,
[
[ { name: "hand0" }, { children: [{ name: "SK" }], selected: [0] } ]
]
)
  • Would find entity named "hand0" (eg. first player's hand)
  • Create new card King of Spades
  • And finally mark that card selected ([0] in this case assuming it's first and only card in "hand0")

Example

populateState(
state,
[
[ null, { name: "C7" } ]
]
)
  • Would add card 7 of Clubs directly on the state/table
  • Note null -> no query = fallback to root state object

Example

Perform multiple additions in one go:

populateState(
state,
[
[ { name: "hand0" }, { children: [{ name: "SK" }], selected: [0] } ],
[ null, { name: "C7" } ],
[ { type: "pile" }, { children: [{ name: "C2" }, { name: "C3" }, { name: "C4" }] } ]
]
)

Type parameters​

NameType
Sextends State<Record<string, unknown>, S>

Parameters​

NameTypeDescription
stateSState to populate with new entities
entitiesMapStateMockingTuple[]Tuples of "entity queries" to "entity definitions". Finds an already existing entity and fills it with new data/entities to test.
gameEntities?Record<string, EntityConstructor>optional record of your game's custom entities.

Returns​

S

the same state just for convenience.


testEvent​

▸ testEvent<S>(state, action, message): boolean

Test if given event would pass in gameplay under current conditions.

Type parameters​

NameType
Sextends State<Record<string, unknown>, S>

Parameters​

NameType
stateS
actionActionDefinition<S>
messageServerPlayerMessage

Returns​

boolean


Setup Functions​

setupServerTesting​

▸ setupServerTesting<S, R>(options): SetupAPI<S, R>

Type parameters​

NameType
Sextends State<Record<string, unknown>, S>
Rextends Room<S, Record<string, unknown>, R> = Room<S, Record<string, unknown>>

Parameters​

NameType
optionsSetupOptions<S, R>

Returns​

SetupAPI<S, R>


Utility Functions​

childrenNamed​

▸ childrenNamed(names): EntityMockingDefinition

Creates EntityMockingDefinition object containing children with objects containing names.

Example

given multiple names

childrenNamed(["D7", "D8", "D9"])
// returns:
{
children: [
{ name: "D7"}, { name: "D8"}, { name: "D9"}
]
}

Example

single string param

childrenNamed("D7")
// returns:
{
children: [{ name: "D7"}]
}

Parameters​

NameTypeDescription
namesstring | string[]can be single name, will be wrapped in an array anyway

Returns​

EntityMockingDefinition


objectsNamed​

▸ objectsNamed(names): EntityMockingDefinition[]

Turns an array of strings into array of objects with name. Useful in creating children prop in EntityMockingDefinition.

Example

given multiple names

objectsNamed(["D7", "D8", "D9"])
// returns:
[{ name: "D7"}, { name: "D8"}, { name: "D9"}]

Example

single string param

objectsNamed("D7")
// returns:
[{ name: "D7"}]

Parameters​

NameTypeDescription
namesstring | string[]can be single name, will be wrapped in an array anyway

Returns​

EntityMockingDefinition[]