open-nomad/ui/tests/unit/utils/rolling-array-test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3 KiB
JavaScript
Raw Normal View History

2018-08-31 00:26:41 +00:00
import { isArray } from '@ember/array';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
2018-08-31 00:26:41 +00:00
import RollingArray from 'nomad-ui/utils/classes/rolling-array';
2021-12-28 14:45:20 +00:00
module('Unit | Util | RollingArray', function () {
test('has a maxLength property that gets set in the constructor', function (assert) {
2019-03-13 00:04:16 +00:00
const array = RollingArray(10, 'a', 'b', 'c');
assert.equal(array.maxLength, 10, 'maxLength is set in the constructor');
assert.deepEqual(
array,
['a', 'b', 'c'],
'additional arguments to the constructor become elements'
);
});
2018-08-31 00:26:41 +00:00
2021-12-28 14:45:20 +00:00
test('push works like Array#push', function (assert) {
2019-03-13 00:04:16 +00:00
const array = RollingArray(10);
const pushReturn = array.push('a');
assert.equal(
pushReturn,
array.length,
'the return value from push is equal to the return value of Array#push'
);
2021-12-28 16:08:12 +00:00
assert.equal(
array[0],
'a',
'the arguments passed to push are appended to the array'
);
2018-08-31 00:26:41 +00:00
2019-03-13 00:04:16 +00:00
array.push('b', 'c', 'd');
assert.deepEqual(
array,
['a', 'b', 'c', 'd'],
'the elements already in the array are left in tact and new elements are appended'
);
});
2018-08-31 00:26:41 +00:00
2021-12-28 14:45:20 +00:00
test('when pushing past maxLength, items are removed from the head of the array', function (assert) {
2019-03-13 00:04:16 +00:00
const array = RollingArray(3);
const pushReturn = array.push(1, 2, 3, 4);
assert.deepEqual(
array,
[2, 3, 4],
'The first argument to push is not in the array, but the following three are'
);
assert.equal(
pushReturn,
array.length,
'The return value of push is still the array length despite more arguments than possible were provided to push'
);
});
2018-08-31 00:26:41 +00:00
2021-12-28 14:45:20 +00:00
test('when splicing past maxLength, items are removed from the head of the array', function (assert) {
2019-03-13 00:04:16 +00:00
const array = RollingArray(3, 'a', 'b', 'c');
2018-08-31 00:26:41 +00:00
2019-03-13 00:04:16 +00:00
array.splice(1, 0, 'z');
assert.deepEqual(
array,
['z', 'b', 'c'],
'The new element is inserted as the second element in the array and the first element is removed due to maxLength restrictions'
);
2018-08-31 00:26:41 +00:00
2019-03-13 00:04:16 +00:00
array.splice(0, 0, 'pickme');
assert.deepEqual(
array,
['z', 'b', 'c'],
'The new element never makes it into the array since it was added at the head of the array and immediately removed'
);
2018-08-31 00:26:41 +00:00
2019-03-13 00:04:16 +00:00
array.splice(0, 1, 'pickme');
assert.deepEqual(
array,
['pickme', 'b', 'c'],
'The new element makes it into the array since the previous element at the head of the array is first removed due to the second argument to splice'
);
});
2018-08-31 00:26:41 +00:00
2021-12-28 14:45:20 +00:00
test('unshift throws instead of prepending elements', function (assert) {
2019-03-13 00:04:16 +00:00
const array = RollingArray(5);
2018-08-31 00:26:41 +00:00
2019-03-13 00:04:16 +00:00
assert.throws(
() => {
array.unshift(1);
},
/Cannot unshift/,
'unshift is not supported, but is not undefined'
);
});
2018-08-31 00:26:41 +00:00
2021-12-28 14:45:20 +00:00
test('RollingArray is an instance of Array', function (assert) {
2019-03-13 00:04:16 +00:00
const array = RollingArray(5);
2021-12-28 16:08:12 +00:00
assert.strictEqual(array.constructor, Array, 'The constructor is Array');
2019-03-13 00:04:16 +00:00
assert.ok(array instanceof Array, 'The instanceof check is true');
assert.ok(isArray(array), 'The ember isArray helper works');
});
2018-08-31 00:26:41 +00:00
});