Skip to content

Commit

Permalink
additional unti tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Nov 23, 2023
1 parent 7007559 commit 8ead762
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions sync-server/tests/additional-emit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { World } from '../src/world'
import { Transmitter } from '../src/transmitter'
import MockSocketIo from '../src/testing/mock-socket'
import { beforeEach, test, expect, afterEach, vi } from 'vitest'

const CLIENT_ID = 'mock'
let socket

beforeEach(() => {
World.transport(MockSocketIo.serverIo, {
auth() {
return CLIENT_ID
}
})
Transmitter.encode = false

class Player {
hp = 10
}

World.setUserClass(Player)

socket = new MockSocketIo.ClientIo(CLIENT_ID)
socket.connection()
})

test('Merge Specific Value in Room', async () => {
const fn = vi.fn()

class Room {
value = 'test'

$additionalEmitProperties() {
return {
value: this.value
}
}
}

socket.on('w', fn)

const room = World.addRoom('room', Room)
await World.joinRoom(room.id, CLIENT_ID)
await World.send()

expect(fn).toHaveBeenCalled()
expect(fn).toHaveBeenCalledWith([
room.id,
expect.any(Number),
expect.objectContaining({ value: 'test' })
], undefined)
})

test('Merge Specific Value in Room (string)', async () => {
const fn = vi.fn()

class Room {
$additionalEmitProperties(user) {
return `users.${user.id}.hp`
}
}

socket.on('w', fn)

const room = World.addRoom('room', Room)
await World.joinRoom(room.id, CLIENT_ID)
await World.send()

expect(fn).toHaveBeenCalled()
expect(fn).toHaveBeenCalledWith([
room.id,
expect.any(Number),
expect.objectContaining({ users: { [CLIENT_ID]: expect.objectContaining({ hp: 10 }) } })
], undefined)
})

afterEach(() => {
World.clear()
})

0 comments on commit 8ead762

Please sign in to comment.