-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcore.test.js
More file actions
141 lines (123 loc) · 3.27 KB
/
core.test.js
File metadata and controls
141 lines (123 loc) · 3.27 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const assert = require('assert')
const feathers = require('@feathersjs/feathers')
const sync = require('../lib/core')
describe('feathers-sync core tests', () => {
const app = feathers()
.configure(sync)
.use('/todo', {
events: ['custom'],
async create (data) {
return data
}
})
app.sync = {
serialize: data => data,
deserialize: data => data
}
it('configuring twice does nothing', () => {
app.configure(sync)
})
it('sends sync-out for service events', done => {
const message = { message: 'This is a test' }
app.once('sync-out', data => {
try {
assert.deepStrictEqual(data, {
event: 'created',
path: 'todo',
data: message,
context: {
arguments: [message, {}],
data: message,
params: {},
type: 'around',
statusCode: undefined,
method: 'create',
event: 'created',
path: 'todo',
result: message
}
})
done()
} catch (error) {
done(error)
}
})
app.service('todo').create(message)
})
it('can skip sending sync event', done => {
const message = 'This is a test'
const handler = () => {
done(new Error('Should never get here'))
}
app.service('todo').once('created', todo => {
assert.strictEqual(todo.message, message)
app.removeListener('sync-out', handler)
done()
})
app.once('sync-out', handler)
let synced = false
app.service('todo').hooks({
before (context) {
if (!synced) {
context[sync.SYNC] = false
synced = true
}
return context
}
})
app.service('todo').create({ message })
})
it('sends sync-out for custom events', done => {
app.once('sync-out', data => {
assert.deepStrictEqual(data, {
event: 'custom',
path: 'todo',
data: 'testing',
context: undefined
})
done()
})
app.service('todo').emit('custom', 'testing')
})
it('passes non-service events through', done => {
const todo = app.service('todo')
todo.once('something', data => {
assert.strictEqual(data, 'test')
done()
})
todo.emit('something', 'test')
})
it('sync-in event gets turned into service event', done => {
app.service('todo').once('created', (data, context) => {
assert.deepStrictEqual(data, { message: 'This is a test' })
assert.strictEqual(context.app, app)
assert.strictEqual(context.service, app.service('todo'))
assert.strictEqual(context.method, 'create')
assert.strictEqual(context.type, 'after')
done()
})
app.emit('sync-in', {
event: 'created',
path: 'todo',
data: { message: 'This is a test' },
context: {
data: { message: 'This is a test' },
params: {},
type: 'after',
method: 'create',
path: 'todo',
result: { message: 'This is a test' }
}
})
})
it('sync-in fails for invalid event (path)', () => {
try {
app.emit('sync-in', {
event: 'something',
path: 'todos'
})
} catch (error) {
assert.strictEqual(error.message, 'Can not find service \'todos\'')
}
})
})