github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/redux/localities.spec.ts (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 import { assert } from "chai"; 12 13 import { selectLocalityTree, LocalityTier } from "./localities"; 14 15 function makeStateWithLocalities(localities: LocalityTier[][]) { 16 const nodes = localities.map((locality, i) => { 17 return { 18 desc: { 19 node_id: i, 20 locality: { tiers: locality }, 21 }, 22 }; 23 }); 24 25 return { 26 cachedData: { 27 nodes: { 28 data: nodes, 29 inFlight: false, 30 valid: true, 31 }, 32 liveness: {}, 33 }, 34 }; 35 } 36 37 describe("selectLocalityTree", function() { 38 it("puts nodes without locality at the top-level", function() { 39 const state = makeStateWithLocalities([ 40 [], 41 ]); 42 43 const tree = selectLocalityTree(state); 44 45 assert.isEmpty(tree.tiers); 46 assert.isEmpty(tree.localities); 47 48 assert.lengthOf(tree.nodes, 1); 49 }); 50 51 it("organizes nodes by locality", function() { 52 const state = makeStateWithLocalities([ 53 [{ key: "region", value: "us-east-1" }], 54 [{ key: "region", value: "us-east-2" }], 55 ]); 56 57 const tree = selectLocalityTree(state); 58 59 assert.isEmpty(tree.tiers); 60 assert.isEmpty(tree.nodes); 61 62 assert.hasAllKeys(tree.localities, ["region"] ); 63 const regions = tree.localities.region; 64 65 assert.hasAllKeys(regions, ["us-east-1", "us-east-2"]); 66 67 const usEast1 = regions["us-east-1"]; 68 69 assert.isEmpty(usEast1.localities); 70 assert.deepEqual(usEast1.tiers, [{ key: "region", value: "us-east-1" }]); 71 72 assert.lengthOf(usEast1.nodes, 1); 73 74 const usEast2 = regions["us-east-2"]; 75 76 assert.isEmpty(usEast2.localities); 77 assert.deepEqual(usEast2.tiers, [{ key: "region", value: "us-east-2" }]); 78 79 assert.lengthOf(usEast2.nodes, 1); 80 }); 81 });