1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as Vocabulary from './vocabulary';
7 describe('Vocabulary', () => {
8 let vocabulary: Vocabulary.Vocabulary;
27 {label: "Homo sapiens"}
33 {label: "Canis lupus familiaris"}
39 labels: [{label: "Sizes"}],
42 labels: [{label: "Small"}]
45 labels: [{label: "Medium"}]
48 labels: [{label: "Large"}]
59 it('returns the list of tag keys', () => {
60 const tagKeys = Vocabulary.getTags(vocabulary);
61 // Alphabetically ordered by label
62 expect(tagKeys).toEqual([
63 {id: "IDKEYANIMALS", label: "Animal"},
64 {id: "IDKEYANIMALS", label: "Creature"},
65 {id: "IDKEYCOMMENT", label: "IDKEYCOMMENT"},
66 {id: "IDKEYSIZES", label: "Sizes"},
70 it('returns the tag values for a given key', () => {
71 const tagValues = Vocabulary.getTagValues('IDKEYSIZES', vocabulary);
72 // Alphabetically ordered by label
73 expect(tagValues).toEqual([
74 {id: "IDVALSIZES4", label: "IDVALSIZES4"},
75 {id: "IDVALSIZES3", label: "Large"},
76 {id: "IDVALSIZES2", label: "Medium"},
77 {id: "IDVALSIZES1", label: "Small"},
81 it('returns an empty list of values for an non-existent key', () => {
82 const tagValues = Vocabulary.getTagValues('IDNONSENSE', vocabulary);
83 expect(tagValues).toEqual([]);
86 it('returns a key id for a given key label', () => {
88 // Two labels belonging to the same ID
89 {keyLabel: 'Animal', expected: 'IDKEYANIMALS'},
90 {keyLabel: 'Creature', expected: 'IDKEYANIMALS'},
91 // Non-existent label returns empty string
92 {keyLabel: 'ThisKeyLabelDoesntExist', expected: ''},
94 testCases.forEach(tc => {
95 const tagValueID = Vocabulary.getTagKeyID(tc.keyLabel, vocabulary);
96 expect(tagValueID).toEqual(tc.expected);
100 it('returns an key label for a given key id', () => {
102 // ID with many labels return the first one
103 {keyID: 'IDKEYANIMALS', expected: 'Animal'},
104 // Key IDs without any labels or unknown keys should return the literal
105 // key from the API's response (that is, the key 'id')
106 {keyID: 'IDKEYCOMMENT', expected: 'IDKEYCOMMENT'},
107 {keyID: 'FOO', expected: 'FOO'},
109 testCases.forEach(tc => {
110 const tagValueID = Vocabulary.getTagKeyLabel(tc.keyID, vocabulary);
111 expect(tagValueID).toEqual(tc.expected);
115 it('returns a value id for a given key id and value label', () => {
117 // Key ID and value label known
118 {keyID: 'IDKEYANIMALS', valueLabel: 'Human', expected: 'IDVALANIMALS1'},
119 {keyID: 'IDKEYANIMALS', valueLabel: 'Homo sapiens', expected: 'IDVALANIMALS1'},
120 // Key ID known, value label unknown
121 {keyID: 'IDKEYANIMALS', valueLabel: 'Dinosaur', expected: ''},
123 {keyID: 'IDNONSENSE', valueLabel: 'Does not matter', expected: ''},
125 testCases.forEach(tc => {
126 const tagValueID = Vocabulary.getTagValueID(tc.keyID, tc.valueLabel, vocabulary);
127 expect(tagValueID).toEqual(tc.expected);
131 it('returns a value label for a given key & value id pair', () => {
133 // Known key & value ids with multiple value labels: returns the first label
134 {keyId: 'IDKEYANIMALS', valueId: 'IDVALANIMALS1', expected: 'Human'},
135 // Values without label or unknown values should return the literal value from
136 // the API's response (that is, the value 'id')
137 {keyId: 'IDKEYSIZES', valueId: 'IDVALSIZES4', expected: 'IDVALSIZES4'},
138 {keyId: 'IDKEYCOMMENT', valueId: 'FOO', expected: 'FOO'},
139 {keyId: 'IDKEYANIMALS', valueId: 'BAR', expected: 'BAR'},
140 {keyId: 'IDKEYNONSENSE', valueId: 'FOOBAR', expected: 'FOOBAR'},
142 testCases.forEach(tc => {
143 const tagValueLabel = Vocabulary.getTagValueLabel(tc.keyId, tc.valueId, vocabulary);
144 expect(tagValueLabel).toEqual(tc.expected);