Skip to content

Commit

Permalink
testing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfrase3 committed Mar 8, 2024
1 parent 2ac1519 commit b0af9ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ const getServiceOptions = service => {
return {};
};

const constructContext = (app, service, event, data, context = {}) => {
const path = Object.entries(app.services).find(([_, value]) => value === service)[0];
const constructContext = (app, path, event, data, context = {}) => {
const id = (typeof data === 'object' && !Array.isArray(data))
? (data._id || data.id)
: undefined;
return Object.assign({
path,
id: data._id || data.id,
id,
method: eventMap[event],
params: {},
result: data,
event
}, context);
}
};

module.exports = app => {
if (app[SYNC]) {
Expand All @@ -42,6 +45,7 @@ module.exports = app => {

app.on('sync-in', (rawData) => {
const { event, path, data, context } = app.sync.deserialize(rawData);
if (!path) return;
const service = app.service(path);
const hook = context
? Object.assign({ app, service }, context)
Expand Down Expand Up @@ -70,7 +74,7 @@ module.exports = app => {
}

const serializedContext = ctx && typeof ctx.toJSON === 'function' ? ctx.toJSON() : ctx;
const constructedContext = constructContext(app, service, event, data, serializedContext);
const constructedContext = constructContext(app, path, event, data, serializedContext);
const context = ctx && (ctx.app === app || ctx.service === service)
? _.omit(constructedContext, 'app', 'service', 'self')
: constructedContext;
Expand Down
37 changes: 36 additions & 1 deletion test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('feathers-sync core tests', () => {
path: 'todo',
data: message,
context: {
id: undefined,
arguments: [message, {}],
data: message,
params: {},
Expand Down Expand Up @@ -80,13 +81,47 @@ describe('feathers-sync core tests', () => {
app.service('todo').create({ message });
});

it('sends sync-out for manual emits', done => {
const message = { message: 'This is a test', id: 1 };

app.once('sync-out', data => {
try {
assert.deepStrictEqual(data, {
event: 'created',
path: 'todo',
data: message,
context: {
id: 1,
params: {},
method: 'create',
event: 'created',
path: 'todo',
result: message
}
});
done();
} catch (error) {
done(error);
}
});

app.service('todo').emit('created', 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
context: {
path: 'todo',
id: undefined,
method: undefined,
params: {},
result: 'testing',
event: 'custom'
}
});
done();
});
Expand Down

0 comments on commit b0af9ca

Please sign in to comment.