Skip to content

Commit

Permalink
#add test cases for default namespace attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstyles committed Jan 18, 2016
1 parent b12cab2 commit 165ff41
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
##

* _update_ attach raw socket.io instance
* _update_ attach IO instance for default namespace
* _fix_ use new socket.io identifier type
* _update_ dependencies
* _fix_ attach IO instance and not the raw socket for namespaced instances
Expand Down
8 changes: 4 additions & 4 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ socket.use( co.wrap( function *( ctx, next ) {
*/
socket.on( 'connection', ctx => {
console.log( 'Join event', ctx.socket.id )
// socket.broadcast( 'connections', {
// numConnections: socket.connections.size
// })
app.io.broadcast( 'connections', {
socket.broadcast( 'connections', {
numConnections: socket.connections.size
})
// app.io.broadcast( 'connections', {
// numConnections: socket.connections.size
// })
})

socket.on( 'disconnect', ctx => {
Expand Down
27 changes: 23 additions & 4 deletions spec/attach.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

'use strict';

const http = require( 'http' )

const tape = require( 'tape' )
const Koa = require( 'koa' )
const ioc = require( 'socket.io-client' )
const socketIO = require( 'socket.io' )
const IO = require( '../' )

const application = require( './helpers/utils' ).application
Expand All @@ -20,16 +23,16 @@ tape( 'socket.start alters the app to include socket.io', t => {
t.ok( app.server, 'server created linking socket and the koa callback' )
})

tape( 'should not alter a koa app that already has .io unless called with a namespace', t => {
tape( 'should not alter a koa app that already has ._io unless called with a namespace', t => {
t.plan( 1 )

const app = new Koa()
const socket = new IO()
app.io = {}
app._io = {}

t.throws( () => {
socket.attach( app )
}, null, 'calling .attach throws an error when .io already exists without a namespace' )
}, null, 'calling .attach throws an error when ._io already exists without a namespace' )
})

tape( 'should not alter a koa app that already has .server', t => {
Expand Down Expand Up @@ -70,10 +73,26 @@ tape( 'Attaching a namespace to a \'clean\' koa app is fine', t => {
chat.attach( app )

t.ok( app.chat, 'the chat namespace has been attached to the app' )
t.ok( app.io, 'io will be attached, it just isnt listening' )
t.ok( app._io, 'io will be attached, it just isnt listening' )
}, null, 'Attaching only a namespace is fine' )
})

tape( 'Manually creating the socketIO instance and attaching namespaces without a default is fine', t => {
t.plan( 1 )

const app = new Koa()
const chat = new IO( 'chat' )

const server = http.createServer( app.callback() )
const io = socketIO( server )
app._io = io

t.doesNotThrow( () => {
chat.attach( app )
}, null, 'Attaching a namespace is fine' )

})

tape( 'Attaching a namespace should be done via an options object', t => {
t.plan( 2 )

Expand Down
8 changes: 2 additions & 6 deletions spec/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tape( 'Client connects to server', t => {
client.on( 'connect', () => {
client.disconnect()
})
socket.on( 'disconnect', socket => {
socket.on( 'disconnect', ctx => {
t.pass( 'connect-disconnect cleanly' )
})
})
Expand Down Expand Up @@ -76,10 +76,6 @@ tape( 'A specific connection can be picked from the list of active connections',
const app = application( socket )

app._io.on( 'connection', sock => {
for ( var key of socket.connections.keys() ) {
console.log( key )
}
console.log( sock.id )
t.equal( socket.connections.has( sock.id ), true, 'The socket ID is contained in the connections map' )
sock.disconnect()
})
Expand All @@ -93,7 +89,7 @@ tape( 'The connection list can be used to boot a client', t => {
const socket = new IO()
const app = application( socket )

app.io.on( 'connection', sock => {
app._io.on( 'connection', sock => {
t.equal( socket.connections.size, 1, 'The connected client is registered' )
})

Expand Down

0 comments on commit 165ff41

Please sign in to comment.