2016-04-24 22:41:37 +02:00
|
|
|
'use strict';
|
2016-08-23 01:29:47 +02:00
|
|
|
const eventDiffer = require('../../lib/eventDiffer');
|
|
|
|
const eventType = require('../../lib/eventType');
|
2016-06-18 21:53:18 +02:00
|
|
|
const assert = require('assert');
|
2015-01-26 16:54:50 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
describe('eventDiffer', () => {
|
|
|
|
it('fails if events include an unknown event type', () => {
|
|
|
|
const events = [
|
2016-04-24 22:41:37 +02:00
|
|
|
{ type: eventType.featureCreated, data: {} },
|
2016-06-18 21:55:46 +02:00
|
|
|
{ type: 'unknown-type', data: {} },
|
2015-01-26 16:54:50 +01:00
|
|
|
];
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
assert.throws(() => {
|
2015-01-26 16:54:50 +01:00
|
|
|
eventDiffer.addDiffs(events);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
it('diffs a feature-update event', () => {
|
2016-08-22 15:12:05 +02:00
|
|
|
const feature = 'foo';
|
2016-06-18 21:53:18 +02:00
|
|
|
const desc = 'bar';
|
2015-01-26 16:54:50 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const events = [
|
2015-01-26 16:54:50 +01:00
|
|
|
{
|
|
|
|
type: eventType.featureUpdated,
|
2016-08-22 15:12:05 +02:00
|
|
|
data: { name: feature, description: desc, strategy: 'default', enabled: true, parameters: { value: 2 } },
|
2015-01-26 16:54:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: eventType.featureCreated,
|
2016-08-22 15:12:05 +02:00
|
|
|
data: { name: feature, description: desc, strategy: 'default', enabled: false, parameters: { value: 1 } },
|
2016-06-18 21:55:46 +02:00
|
|
|
},
|
2015-01-26 16:54:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
eventDiffer.addDiffs(events);
|
|
|
|
|
|
|
|
assert.deepEqual(events[0].diffs, [
|
2016-06-18 21:55:46 +02:00
|
|
|
{ kind: 'E', path: ['enabled'], lhs: false, rhs: true },
|
|
|
|
{ kind: 'E', path: ['parameters', 'value'], lhs: 1, rhs: 2 },
|
2015-01-26 16:54:50 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert.strictEqual(events[1].diffs, null);
|
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
it('diffs only against features with the same name', () => {
|
|
|
|
const events = [
|
2015-01-26 16:54:50 +01:00
|
|
|
{
|
|
|
|
type: eventType.featureUpdated,
|
2016-06-18 21:55:46 +02:00
|
|
|
data: { name: 'bar', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
|
2015-01-26 16:54:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: eventType.featureUpdated,
|
2016-06-18 21:55:46 +02:00
|
|
|
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: false, parameters: {} },
|
2015-01-26 16:54:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: eventType.featureCreated,
|
2016-06-18 21:55:46 +02:00
|
|
|
data: { name: 'bar', description: 'desc', strategy: 'default', enabled: false, parameters: {} },
|
2015-01-26 16:54:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: eventType.featureCreated,
|
2016-06-18 21:55:46 +02:00
|
|
|
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
|
|
|
|
},
|
2015-01-26 16:54:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
eventDiffer.addDiffs(events);
|
|
|
|
|
|
|
|
assert.strictEqual(events[0].diffs[0].rhs, true);
|
|
|
|
assert.strictEqual(events[1].diffs[0].rhs, false);
|
|
|
|
assert.strictEqual(events[2].diffs, null);
|
|
|
|
assert.strictEqual(events[3].diffs, null);
|
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
it('sets an empty array of diffs if nothing was changed', () => {
|
|
|
|
const events = [
|
2015-01-26 16:54:50 +01:00
|
|
|
{
|
|
|
|
type: eventType.featureUpdated,
|
2016-06-18 21:55:46 +02:00
|
|
|
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
|
2015-01-26 16:54:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: eventType.featureCreated,
|
2016-06-18 21:55:46 +02:00
|
|
|
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
|
|
|
|
},
|
2015-01-26 16:54:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
eventDiffer.addDiffs(events);
|
|
|
|
assert.deepEqual(events[0].diffs, []);
|
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
it('sets diffs to null if there was nothing to diff against', () => {
|
|
|
|
const events = [
|
2015-01-26 16:54:50 +01:00
|
|
|
{
|
|
|
|
type: eventType.featureUpdated,
|
2016-06-18 21:55:46 +02:00
|
|
|
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
|
|
|
|
},
|
2015-01-26 16:54:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
eventDiffer.addDiffs(events);
|
|
|
|
assert.strictEqual(events[0].diffs, null);
|
|
|
|
});
|
2016-04-24 22:41:37 +02:00
|
|
|
});
|