hoodiehq / hoodie-connection-status

:dog: connection status API for the browser
Apache License 2.0
5 stars 10 forks source link

testing the disconnect/reconnect events #17

Closed gr2m closed 8 years ago

gr2m commented 8 years ago

@drspaniel

Problem seems to be nock. I'd make it a unit test without nock, by mocking the nets method directly

var test = require('tape')
var lolex = require('lolex')
var simple = require('simple-mock')
var check = require('../../lib/check')

test('Events', function (t) {
  t.plan(2)

  var clock = lolex.install()
  var EventEmitter = require('events').EventEmitter
  var emitter = new EventEmitter()

  function disconnected () {
    t.pass('Disconnected')
  }
  function reconnected () {
    t.pass('Reconnected')
  }

  emitter.on('disconnected', disconnected)
  emitter.on('reconnected', reconnected)

  simple.mock(check.internals, 'nets').callFn(function (options, callback) {
    callback(null, {})
  })
  check({
    emitter: emitter,
    cache: {}
  }, {interval: 1000})

  // emulate network error for "disconnected"
  simple.mock(check.internals, 'nets').callFn(function (options, callback) {
    callback(new Error('oops'))
  })
  clock.tick(1000)

  // emulate network success for "reconnected"
  simple.mock(check.internals, 'nets').callFn(function (options, callback) {
    callback(null, {})
  })
  clock.tick(1000)

  clock.uninstall()
  simple.restore()

})

Does that work for you?

courajs commented 8 years ago

A little busy this week with Thanksgiving plans, but I'll give it a shot when I can

gr2m commented 8 years ago

I found out that nock is ok, the problem is that the callback from the nets request does not get called immediately, so the clock.tick calls occur before the setTimeout call.

I don’t know how to work around that yet

gr2m commented 8 years ago

So yeah, I tried a few things, let’s use lolex in unit tests only, not in combination with nock.

For integration tests, let's set the interval to a low number, and then simply wait for the real timeouts, without faking timers

gr2m commented 8 years ago

closing in favor of https://github.com/hoodiehq/hoodie-client-connection-status/pull/32