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