Skip to content

Commit

Permalink
test: adjust mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
greenpixels committed Apr 7, 2024
1 parent 3c23456 commit 8633e4a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions client/src/handlers/PlayerHandler/PlayerHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PlayerDTO, KeyMap } from '@shared/index'
import { ApplicationMock } from '../../__mocks__/Application.mock'
import { PlayerHandler } from './PlayerHandler'
import { Player } from '../../classes/Player'
import { Sprite } from 'pixi.js'

describe(`Testing PlayerHandler`, () => {
const application = ApplicationMock()
Expand All @@ -23,19 +24,22 @@ describe(`Testing PlayerHandler`, () => {
})
})

function createPlayerSyncTest(playerHandler: PlayerHandler, handleEvent: (players: KeyMap<PlayerDTO>) => void) {
function createPlayerSyncTest(
playerHandler: PlayerHandler,
handleEvent: (players: KeyMap<PlayerDTO>, crownSprite: Sprite) => void
) {
const mockPlayer = createMockPlayer('player_id_first')
test(`${handleEvent.name}: Should add a new player if the player does not yet exist on the client`, () => {
playerHandler.addPlayer = vi.fn()
handleEvent.call(playerHandler, { [mockPlayer.id]: mockPlayer })
handleEvent.call(playerHandler, { [mockPlayer.id]: mockPlayer }, { position: { x: 0, y: 0 } } as Sprite)
expect(playerHandler.addPlayer).toBeCalled()
})

test(`${handleEvent.name}: Should sync players if the player already exists on the client`, () => {
playerHandler.addPlayer = vi.fn()
const syncMock = vi.fn()
playerHandler.players = { [mockPlayer.id]: { sync: syncMock } as unknown as Player }
handleEvent.call(playerHandler, { [mockPlayer.id]: mockPlayer })
handleEvent.call(playerHandler, { [mockPlayer.id]: mockPlayer }, { position: { x: 0, y: 0 } } as Sprite)
expect(playerHandler.addPlayer).not.toBeCalled()
expect(syncMock).toBeCalled()
})
Expand Down

0 comments on commit 8633e4a

Please sign in to comment.