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

127 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-11-06 20:52:08 +01:00
'use strict';
2017-06-28 10:17:14 +02:00
const { test } = require('ava');
2016-11-06 20:52:08 +01:00
const List = require('./list');
2017-06-28 10:17:14 +02:00
function getList() {
2016-11-06 20:52:08 +01:00
const list = new List();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
return list;
}
2017-06-28 10:17:14 +02:00
test('should emit "evicted" events for objects leaving list', t => {
2016-11-06 20:52:08 +01:00
const list = getList();
const evictedList = [];
2017-06-28 10:17:14 +02:00
list.on('evicted', value => {
2016-11-06 20:52:08 +01:00
evictedList.push(value);
});
t.true(evictedList.length === 0);
list.reverseRemoveUntilTrue(({ value }) => {
if (value === 4) {
return true;
}
return false;
});
t.true(evictedList.length === 3);
list.reverseRemoveUntilTrue(() => false);
t.true(evictedList.length === 7);
list.add(1);
list.reverseRemoveUntilTrue(() => false);
t.true(evictedList.length === 8);
});
2017-06-28 10:17:14 +02:00
test('list should be able remove until given value', t => {
2016-11-06 20:52:08 +01:00
const list = getList();
t.true(list.toArray().length === 7);
list.reverseRemoveUntilTrue(({ value }) => value === 4);
t.true(list.toArray().length === 4);
2016-11-07 08:44:46 +01:00
list.reverseRemoveUntilTrue(({ value }) => value === 5);
2016-11-06 20:52:08 +01:00
t.true(list.toArray().length === 3);
2016-11-07 08:44:46 +01:00
list.reverseRemoveUntilTrue(({ value }) => value === 5);
2016-11-06 20:52:08 +01:00
t.true(list.toArray().length === 3);
});
2017-06-28 10:17:14 +02:00
test('list can be cleared and re-add entries', t => {
2016-11-06 20:52:08 +01:00
const list = getList();
list.add(8);
list.add(9);
t.true(list.toArray().length === 9);
list.reverseRemoveUntilTrue(() => false);
t.true(list.toArray().length === 0);
list.add(1);
list.add(2);
list.add(3);
t.true(list.toArray().length === 3);
});
2017-06-28 10:17:14 +02:00
test('should not iterate empty list ', t => {
const list = new List();
let iterateCount = 0;
list.iterate(() => {
iterateCount++;
});
t.true(iterateCount === 0);
});
2017-06-28 10:17:14 +02:00
test('should iterate', t => {
2016-11-06 20:52:08 +01:00
const list = getList();
let iterateCount = 0;
list.iterate(({ value }) => {
iterateCount++;
if (value === 4) {
return false;
}
return true;
});
t.true(iterateCount === 4);
});
2017-06-28 10:17:14 +02:00
test('should reverse iterate', t => {
2016-11-06 20:52:08 +01:00
const list = getList();
let iterateCount = 0;
list.iterateReverse(({ value }) => {
iterateCount++;
2016-11-07 08:44:46 +01:00
if (value === 5) {
2016-11-06 20:52:08 +01:00
return false;
}
return true;
});
t.true(iterateCount === 5);
});
2017-06-28 10:17:14 +02:00
test('should not reverse iterate empty list', t => {
const list = new List();
let iterateCount = 0;
list.iterateReverse(() => {
iterateCount++;
});
t.true(iterateCount === 0);
});