Skip to content

Commit

Permalink
test: Add utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet committed Jan 14, 2025
1 parent 6458a52 commit 96d0432
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/utils/UWebSocketTransport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Transport, TransportEvents } from '@dcl/rpc'
import mitt, { Emitter } from 'mitt'

export const defer = Promise.prototype.then.bind(Promise.resolve())

export type RecognizedString =
| string
| ArrayBuffer
Expand Down Expand Up @@ -66,8 +64,12 @@ export function UWebSocketTransport<T extends { isConnected: boolean }>(
})

// socket already connected at this point
void defer(() => events.emit('connect', {}))
void defer(() => flush())
setImmediate(() => {
events.emit('connect', {})
})
setImmediate(() => {
flush()
})

const api: Transport = {
...events,
Expand Down
84 changes: 84 additions & 0 deletions test/unit/utils/UWebSocketTransport.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import mitt, { Emitter } from 'mitt'
import { UWebSocketTransport, IUWebSocket, IUWebSocketEventMap } from '../../../src/utils/UWebSocketTransport'
import { Transport } from '@dcl/rpc'

describe('UWebSocketTransport', () => {
let mockSocket: jest.Mocked<IUWebSocket<{ isConnected: boolean }>>
let mockEmitter: Emitter<IUWebSocketEventMap>
let transport: Transport

beforeEach(() => {
mockSocket = {
send: jest.fn(),
close: jest.fn(),
end: jest.fn(),
getUserData: jest.fn(() => ({ isConnected: true }))
} as jest.Mocked<IUWebSocket<{ isConnected: boolean }>>

mockEmitter = mitt<IUWebSocketEventMap>()
transport = UWebSocketTransport(mockSocket, mockEmitter)
})

describe('Transport Initialization', () => {
it('should initialize transport and emit connect event', async () => {
const connectListener = jest.fn()
transport.on('connect', connectListener)

await new Promise(setImmediate)

expect(connectListener).toHaveBeenCalled()
})
})

describe('sendMessage', () => {
it('should send a Uint8Array message', () => {
const message = new Uint8Array([1, 2, 3])

transport.sendMessage(message)
expect(mockSocket.send).toHaveBeenCalledWith(message, true)
})
})

describe('close', () => {
it('should call socket.close()', () => {
transport.close()
expect(mockSocket.close).toHaveBeenCalled()
})
})

describe('Event Handling', () => {
it('should emit a close event when the WebSocket is closed', () => {
const closeListener = jest.fn()
transport.on('close', closeListener)

mockEmitter.emit('close', undefined)
expect(closeListener).toHaveBeenCalledWith({})
})

it('should emit a message event when a valid message is received', () => {
const messageListener = jest.fn()
transport.on('message', messageListener)

const message = new ArrayBuffer(4)
mockEmitter.emit('message', message)

expect(messageListener).toHaveBeenCalledWith(new Uint8Array(message))
})

it('should throw an error for unsupported message types', () => {
const invalidMessage = 'Invalid message'
expect(() => mockEmitter.emit('message', invalidMessage)).toThrow(
'WebSocketTransport: Received unknown type of message, expecting Uint8Array'
)
})
})

describe('isConnected', () => {
it('should reflect the socket connection state', () => {
expect(transport.isConnected).toBe(true)

mockSocket.getUserData.mockReturnValueOnce({ isConnected: false })
expect(transport.isConnected).toBe(false)
})
})
})

0 comments on commit 96d0432

Please sign in to comment.