 rewire = require 'rewire'
 
 class WebSocket
-  @sockets: {}
+  @instance = null
 
   CONNECTING: 0
   OPEN: 1

  constructor: (@url, @protocols)  ->
    @readyState = @CONNECTING
    @sent = []
    WebSocket.sockets[@url] = this

  open: ->
    @readyState = @OPEN
    @onopen?()

  close: (code, reason) ->
    unless @readyState in [@CLOSING, @CLOSED]
      @readyState = @CLOSED
      delete WebSocket.sockets[@url]
      @onclose? code: code, reason: reason

  error: -> @onerror?()

  send: (data) ->
    unless @readyState is @OPEN
      throw new Error 'WebSocket is not open'
    @sent.push JSON.parse data

  receive: (data) -> @onmessage? data: JSON.stringify data

Client = rewire '../client'
Client.__set__ 'WebSocket', WebSocket


  beforeEach ->
    client = new Client url, protocols
    socket = WebSocket.instance

  it 'should open a new connection', ->
    assert.equal socket.url, url
