Merge branch 'master' into 16848-token-handling-improvements
[arvados-workbench2.git] / src / models / vocabulary.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as Vocabulary from './vocabulary';
6
7 describe('Vocabulary', () => {
8     let vocabulary: Vocabulary.Vocabulary;
9
10     beforeEach(() => {
11         vocabulary = {
12             strict_tags: false,
13             tags: {
14                 IDKEYCOMMENT: {
15                     labels: []
16                 },
17                 IDKEYANIMALS: {
18                     strict: false,
19                     labels: [
20                         {label: "Animal" },
21                         {label: "Creature"}
22                     ],
23                     values: {
24                         IDVALANIMALS1: {
25                             labels: [
26                                 {label: "Human"},
27                                 {label: "Homo sapiens"}
28                             ]
29                         },
30                         IDVALANIMALS2: {
31                             labels: [
32                                 {label: "Dog"},
33                                 {label: "Canis lupus familiaris"}
34                             ]
35                         },
36                     }
37                 },
38                 IDKEYSIZES: {
39                     labels: [{label: "Sizes"}],
40                     values: {
41                         IDVALSIZES1: {
42                             labels: [{label: "Small"}]
43                         },
44                         IDVALSIZES2: {
45                             labels: [{label: "Medium"}]
46                         },
47                         IDVALSIZES3: {
48                             labels: [{label: "Large"}]
49                         },
50                         IDVALSIZES4: {
51                             labels: []
52                         }
53                     }
54                 }
55             }
56         }
57     });
58
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"},
67         ]);
68     });
69
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"},
78         ])
79     });
80
81     it('returns an empty list of values for an non-existent key', () => {
82         const tagValues = Vocabulary.getTagValues('IDNONSENSE', vocabulary);
83         expect(tagValues).toEqual([]);
84     });
85
86     it('returns a key id for a given key label', () => {
87         const testCases = [
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: ''},
93         ]
94         testCases.forEach(tc => {
95             const tagValueID = Vocabulary.getTagKeyID(tc.keyLabel, vocabulary);
96             expect(tagValueID).toEqual(tc.expected);
97         });
98     });
99
100     it('returns an key label for a given key id', () => {
101         const testCases = [
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'},
108         ]
109         testCases.forEach(tc => {
110             const tagValueID = Vocabulary.getTagKeyLabel(tc.keyID, vocabulary);
111             expect(tagValueID).toEqual(tc.expected);
112         });
113     });
114
115     it('returns a value id for a given key id and value label', () => {
116         const testCases = [
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: ''},
122             // Key ID unknown
123             {keyID: 'IDNONSENSE', valueLabel: 'Does not matter', expected: ''},
124         ]
125         testCases.forEach(tc => {
126             const tagValueID = Vocabulary.getTagValueID(tc.keyID, tc.valueLabel, vocabulary);
127             expect(tagValueID).toEqual(tc.expected);
128         });
129     });
130
131     it('returns a value label for a given key & value id pair', () => {
132         const testCases = [
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'},
141         ]
142         testCases.forEach(tc => {
143             const tagValueLabel = Vocabulary.getTagValueLabel(tc.keyId, tc.valueId, vocabulary);
144             expect(tagValueLabel).toEqual(tc.expected);
145         });
146     });
147 });