github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/unit/utils/path-tree-test.js (about)

     1  import pathTree from 'nomad-ui/utils/path-tree';
     2  import { module, test } from 'qunit';
     3  
     4  const PATHSTRINGS = [
     5    { path: '/foo/bar/baz' },
     6    { path: '/foo/bar/bay' },
     7    { path: '/foo/bar/bax' },
     8    { path: '/a/b' },
     9    { path: '/a/b/c' },
    10    { path: '/a/b/canary' },
    11    { path: '/a/b/canine' },
    12    { path: '/a/b/chipmunk' },
    13    { path: '/a/b/c/d' },
    14    { path: '/a/b/c/dalmation/index' },
    15    { path: '/a/b/c/doberman/index' },
    16    { path: '/a/b/c/dachshund/index' },
    17    { path: '/a/b/c/dachshund/poppy' },
    18  ];
    19  
    20  module('Unit | Utility | path-tree', function () {
    21    test('it converts path strings to a Variable Path Object ', function (assert) {
    22      const tree = new pathTree(PATHSTRINGS);
    23      assert.ok(
    24        'root' in tree.paths,
    25        'Tree has a paths object that begins with a root'
    26      );
    27      assert.ok('children' in tree.paths.root, 'Root has children');
    28      assert.equal(
    29        Object.keys(tree.paths.root.children).length,
    30        2,
    31        'Root has 2 children (a[...] and foo[...])'
    32      );
    33    });
    34  
    35    test('it allows for node-based search and traversal', function (assert) {
    36      const tree = new pathTree(PATHSTRINGS);
    37      assert.deepEqual(
    38        tree.paths.root,
    39        tree.findPath(''),
    40        'Returns tree root on default findPath'
    41      );
    42      assert.ok(
    43        tree.findPath('foo'),
    44        'Path found at the first part of a concatenated folder'
    45      );
    46      assert.ok(
    47        tree.findPath('foo/bar'),
    48        'Finds a path at the concatenated folder path'
    49      );
    50      assert.ok(
    51        tree.findPath('a/b'),
    52        'Finds a path at the concatenated folder path with multiple subdirectories'
    53      );
    54  
    55      assert.equal(
    56        Object.keys(tree.findPath('a/b/c').children).length,
    57        3,
    58        'Multiple subdirectories are listed at a found compacted path with many child paths'
    59      );
    60  
    61      assert.equal(
    62        Object.keys(tree.findPath('a/b').files).length,
    63        4,
    64        'Multiple files are listed at a found non-terminal compacted path with many variables'
    65      );
    66      assert.equal(
    67        Object.keys(tree.findPath('a/b/c/doberman').files).length,
    68        1,
    69        'One file listed at a found compacted path with a single variable'
    70      );
    71      assert.equal(
    72        Object.keys(tree.findPath('a/b/c/dachshund').files).length,
    73        2,
    74        'Multiple files listed at a found terminal compacted path with many variables'
    75      );
    76    });
    77  });