github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/utils/rolling-array-test.js (about) 1 import { isArray } from '@ember/array'; 2 import { module, test } from 'qunit'; 3 import RollingArray from 'nomad-ui/utils/classes/rolling-array'; 4 5 module('Unit | Util | RollingArray', function() { 6 test('has a maxLength property that gets set in the constructor', function(assert) { 7 const array = RollingArray(10, 'a', 'b', 'c'); 8 assert.equal(array.maxLength, 10, 'maxLength is set in the constructor'); 9 assert.deepEqual( 10 array, 11 ['a', 'b', 'c'], 12 'additional arguments to the constructor become elements' 13 ); 14 }); 15 16 test('push works like Array#push', function(assert) { 17 const array = RollingArray(10); 18 const pushReturn = array.push('a'); 19 assert.equal( 20 pushReturn, 21 array.length, 22 'the return value from push is equal to the return value of Array#push' 23 ); 24 assert.equal(array[0], 'a', 'the arguments passed to push are appended to the array'); 25 26 array.push('b', 'c', 'd'); 27 assert.deepEqual( 28 array, 29 ['a', 'b', 'c', 'd'], 30 'the elements already in the array are left in tact and new elements are appended' 31 ); 32 }); 33 34 test('when pushing past maxLength, items are removed from the head of the array', function(assert) { 35 const array = RollingArray(3); 36 const pushReturn = array.push(1, 2, 3, 4); 37 assert.deepEqual( 38 array, 39 [2, 3, 4], 40 'The first argument to push is not in the array, but the following three are' 41 ); 42 assert.equal( 43 pushReturn, 44 array.length, 45 'The return value of push is still the array length despite more arguments than possible were provided to push' 46 ); 47 }); 48 49 test('when splicing past maxLength, items are removed from the head of the array', function(assert) { 50 const array = RollingArray(3, 'a', 'b', 'c'); 51 52 array.splice(1, 0, 'z'); 53 assert.deepEqual( 54 array, 55 ['z', 'b', 'c'], 56 'The new element is inserted as the second element in the array and the first element is removed due to maxLength restrictions' 57 ); 58 59 array.splice(0, 0, 'pickme'); 60 assert.deepEqual( 61 array, 62 ['z', 'b', 'c'], 63 'The new element never makes it into the array since it was added at the head of the array and immediately removed' 64 ); 65 66 array.splice(0, 1, 'pickme'); 67 assert.deepEqual( 68 array, 69 ['pickme', 'b', 'c'], 70 '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' 71 ); 72 }); 73 74 test('unshift throws instead of prepending elements', function(assert) { 75 const array = RollingArray(5); 76 77 assert.throws( 78 () => { 79 array.unshift(1); 80 }, 81 /Cannot unshift/, 82 'unshift is not supported, but is not undefined' 83 ); 84 }); 85 86 test('RollingArray is an instance of Array', function(assert) { 87 const array = RollingArray(5); 88 assert.ok(array.constructor === Array, 'The constructor is Array'); 89 assert.ok(array instanceof Array, 'The instanceof check is true'); 90 assert.ok(isArray(array), 'The ember isArray helper works'); 91 }); 92 });