1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/event-differ.test.js

112 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
2016-10-26 10:43:11 +02:00
2016-11-13 15:41:35 +01:00
const test = require('ava');
2016-11-13 18:14:29 +01:00
const eventDiffer = require('./event-differ');
2016-12-09 14:50:30 +01:00
const { FEATURE_CREATED, FEATURE_UPDATED } = require('./event-type');
2016-11-13 18:14:29 +01:00
const logger = require('./logger');
2016-11-13 15:41:35 +01:00
2016-12-27 21:03:50 +01:00
test.beforeEach(() => {
2016-11-13 15:41:35 +01:00
logger.setLevel('FATAL');
});
2016-11-13 15:41:35 +01:00
test('fails if events include an unknown event type', t => {
const events = [
2016-12-09 14:50:30 +01:00
{ type: FEATURE_CREATED, data: {} },
2016-11-13 15:41:35 +01:00
{ type: 'unknown-type', data: {} },
];
2016-11-13 15:41:35 +01:00
t.throws(() => {
eventDiffer.addDiffs(events);
2016-11-13 15:41:35 +01:00
});
});
2016-11-13 15:41:35 +01:00
test('diffs a feature-update event', t => {
const feature = 'foo';
const desc = 'bar';
2016-11-13 15:41:35 +01:00
const events = [
{
2016-12-09 14:50:30 +01:00
type: FEATURE_UPDATED,
2016-11-13 15:41:35 +01:00
data: { name: feature, description: desc, strategy: 'default', enabled: true, parameters: { value: 2 } },
},
{
2016-12-09 14:50:30 +01:00
type: FEATURE_CREATED,
2016-11-13 15:41:35 +01:00
data: { name: feature, description: desc, strategy: 'default', enabled: false, parameters: { value: 1 } },
},
];
2016-11-13 15:41:35 +01:00
eventDiffer.addDiffs(events);
2016-12-01 00:42:14 +01:00
const diffs = events[0].diffs;
t.true(diffs[0].kind === 'E');
t.true(diffs[0].path[0] === 'enabled');
t.true(diffs[0].kind === 'E');
t.true(diffs[0].lhs === false);
t.true(diffs[0].rhs);
t.true(diffs[1].kind === 'E');
t.true(diffs[1].path[0] === 'parameters');
t.true(diffs[1].path[1] === 'value');
t.true(diffs[1].kind === 'E');
t.true(diffs[1].lhs === 1);
2016-11-13 15:41:35 +01:00
t.true(events[1].diffs === null);
});
2016-11-13 15:41:35 +01:00
test('diffs only against features with the same name', t => {
const events = [
{
2016-12-09 14:50:30 +01:00
type: FEATURE_UPDATED,
2016-11-13 15:41:35 +01:00
data: { name: 'bar', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
},
{
2016-12-09 14:50:30 +01:00
type: FEATURE_UPDATED,
2016-11-13 15:41:35 +01:00
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: false, parameters: {} },
},
{
2016-12-09 14:50:30 +01:00
type: FEATURE_CREATED,
2016-11-13 15:41:35 +01:00
data: { name: 'bar', description: 'desc', strategy: 'default', enabled: false, parameters: {} },
},
{
2016-12-09 14:50:30 +01:00
type: FEATURE_CREATED,
2016-11-13 15:41:35 +01:00
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
},
];
2016-11-13 15:41:35 +01:00
eventDiffer.addDiffs(events);
2016-11-13 15:41:35 +01:00
t.true(events[0].diffs[0].rhs === true);
t.true(events[1].diffs[0].rhs === false);
t.true(events[2].diffs === null);
t.true(events[3].diffs === null);
});
2016-11-13 15:41:35 +01:00
test('sets an empty array of diffs if nothing was changed', t => {
const events = [
{
2016-12-09 14:50:30 +01:00
type: FEATURE_UPDATED,
2016-11-13 15:41:35 +01:00
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
},
{
2016-12-09 14:50:30 +01:00
type: FEATURE_CREATED,
2016-11-13 15:41:35 +01:00
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
},
];
eventDiffer.addDiffs(events);
t.deepEqual(events[0].diffs, []);
});
2016-11-13 15:41:35 +01:00
test('sets diffs to null if there was nothing to diff against', t => {
const events = [
{
2016-12-09 14:50:30 +01:00
type: FEATURE_UPDATED,
2016-11-13 15:41:35 +01:00
data: { name: 'foo', description: 'desc', strategy: 'default', enabled: true, parameters: {} },
},
];
eventDiffer.addDiffs(events);
t.true(events[0].diffs === null);
});