github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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(
    25        array[0],
    26        'a',
    27        'the arguments passed to push are appended to the array'
    28      );
    29  
    30      array.push('b', 'c', 'd');
    31      assert.deepEqual(
    32        array,
    33        ['a', 'b', 'c', 'd'],
    34        'the elements already in the array are left in tact and new elements are appended'
    35      );
    36    });
    37  
    38    test('when pushing past maxLength, items are removed from the head of the array', function (assert) {
    39      const array = RollingArray(3);
    40      const pushReturn = array.push(1, 2, 3, 4);
    41      assert.deepEqual(
    42        array,
    43        [2, 3, 4],
    44        'The first argument to push is not in the array, but the following three are'
    45      );
    46      assert.equal(
    47        pushReturn,
    48        array.length,
    49        'The return value of push is still the array length despite more arguments than possible were provided to push'
    50      );
    51    });
    52  
    53    test('when splicing past maxLength, items are removed from the head of the array', function (assert) {
    54      const array = RollingArray(3, 'a', 'b', 'c');
    55  
    56      array.splice(1, 0, 'z');
    57      assert.deepEqual(
    58        array,
    59        ['z', 'b', 'c'],
    60        'The new element is inserted as the second element in the array and the first element is removed due to maxLength restrictions'
    61      );
    62  
    63      array.splice(0, 0, 'pickme');
    64      assert.deepEqual(
    65        array,
    66        ['z', 'b', 'c'],
    67        'The new element never makes it into the array since it was added at the head of the array and immediately removed'
    68      );
    69  
    70      array.splice(0, 1, 'pickme');
    71      assert.deepEqual(
    72        array,
    73        ['pickme', 'b', 'c'],
    74        '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'
    75      );
    76    });
    77  
    78    test('unshift throws instead of prepending elements', function (assert) {
    79      const array = RollingArray(5);
    80  
    81      assert.throws(
    82        () => {
    83          array.unshift(1);
    84        },
    85        /Cannot unshift/,
    86        'unshift is not supported, but is not undefined'
    87      );
    88    });
    89  
    90    test('RollingArray is an instance of Array', function (assert) {
    91      const array = RollingArray(5);
    92      assert.strictEqual(array.constructor, Array, 'The constructor is Array');
    93      assert.ok(array instanceof Array, 'The instanceof check is true');
    94      assert.ok(isArray(array), 'The ember isArray helper works');
    95    });
    96  });