Skip to content

Emitting with object as payload converts the object to a string #297

@gCardinal

Description

@gCardinal

In a test, if I call server.emit('an-event', {foo: 'bar'}), the socket in my test will receive [Object object] as the payload. The exact code is below.

I am using the Socket.IO implementation and it allows objects to be given as arguments to emit() as can be seen here. I would assume this library should also respect that.


it(`dispatches setCurrentPlayer() when Events.ChangeCurrentPlayer event is received`, () => {
    const currentPlayer: Player = { id: '1', color: 'red' }
    const playerList: Player[] = [currentPlayer, { id: '2', color: 'blue' }]

    const server = new Server(configuration.server.uri)
    const store = createMockedStore({}, SocketIO)
    store.dispatch(joinAction)
    const action = switchCurrentPlayer(currentPlayer, playerList)
    server.emit(Events.ChangeCurrentPlayer, action.payload) // This is the relevant bit

    // Next line will fail, error after this. 
    expect(store.getActions()).toContainObject(setCurrentPlayer(action.payload))
})

Notice the object with type: setCurrentPlayer in the expected and received.

Error: expected [{"payload": {"player": {"id": "0987"}, "settings": {"gameNumber": "1234"}}, "type": "JOIN_GAME_SUCCESS"}, {"meta": {"dispatchedByWsMiddleware": true}, "payload": "[object Object]", "type": "setCurrentPlayer"}] to contain object {"payload": {"color": "blue", "id": "2"}, "type": "setCurrentPlayer"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions