-
Notifications
You must be signed in to change notification settings - Fork 16
/
connect-test.js
93 lines (77 loc) · 2.33 KB
/
connect-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var simple = require('simple-mock')
var test = require('tape')
var connect = require('../../lib/connect')
test('connect', function (t) {
var syncApi = {
on: simple.stub()
}
var state = {
emitter: {
emit: simple.stub()
},
remote: 'remoteDb',
db: {
sync: simple.stub()
}
}
simple.mock(state.db, 'sync').returnWith(syncApi)
connect(state)
.then(function () {
t.is(state.db.sync.callCount, 1, 'calls sync once')
t.is(state.db.sync.lastCall.args[0], 'remoteDb', 'calls api.sync with "remoteDb"')
t.is(state.db.sync.lastCall.args[1].live, true, 'calls api.sync with live: true')
t.is(state.db.sync.lastCall.args[1].retry, true, 'calls api.sync with retry: true')
t.is(state.db.sync.lastCall.args[1].create_target, true, 'calls api.sync with create_target: true')
t.is(syncApi.on.callCount, 2, 'listens to two events')
t.is(syncApi.on.calls[0].arg, 'error', 'listens "error" event')
t.is(syncApi.on.calls[1].arg, 'change', 'listens "change" event')
t.is(state.emitter.emit.callCount, 1, 'emits 1 event')
t.is(state.emitter.emit.lastCall.arg, 'connect', 'emits "connect" event')
})
t.end()
})
test('connect with remote being promise', function (t) {
var syncApi = {
on: simple.stub()
}
var state = {
emitter: {
emit: simple.stub()
},
remote: Promise.resolve('remoteDb'),
db: {
sync: simple.stub()
}
}
simple.mock(state.db, 'sync').returnWith(syncApi)
connect(state)
.then(function () {
t.is(state.db.sync.lastCall.arg, 'remoteDb', 'calls api.sync with value of resolved promise')
})
t.end()
})
test('connect replication error', function (t) {
t.plan(3)
var syncApi = {
on: simple.stub()
}
var state = {
emitter: {
emit: simple.stub()
},
remote: 'remoteDb',
db: {
sync: simple.stub()
}
}
simple.mock(state.db, 'sync').returnWith(syncApi)
connect(state)
.then(function () {
t.is(syncApi.on.calls[0].args[0], 'error', 'listens "error" event')
var errorCallback = syncApi.on.calls[0].args[1]
var error = new Error('Replication error')
errorCallback(error)
t.is(state.emitter.emit.lastCall.args[0], 'error', 'last event emitted was an error event')
t.is(state.emitter.emit.lastCall.args[1], error, 'error event was called with error object')
})
})