github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/rolling-array-test.js (about)

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