-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemLabels.test.js
More file actions
147 lines (136 loc) · 5.63 KB
/
itemLabels.test.js
File metadata and controls
147 lines (136 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { assert } from './test-utils/deps-node.js';
import {
getSectionItemLabels,
collectItemLabelsFromSections
} from '../ts/appTemplates/itemLabels.ts';
describe('[ILBL] itemLabels', function () {
describe('getSectionItemLabels', function () {
it('returns labels for an itemKey present in section', function () {
const section = {
key: 's1',
type: 'permanent',
name: { en: 'Section 1' },
itemKeys: ['function-mobility'],
itemCustomizations: {
'function-mobility': {
labels: { question: { en: 'MOBILITY' } }
}
}
};
const labels = getSectionItemLabels(section, 'function-mobility');
assert.deepEqual(labels, { question: { en: 'MOBILITY' } });
});
it('returns undefined when itemKey not in section', function () {
const section = { key: 's', type: 'permanent', name: { en: '' }, itemKeys: ['x'] };
assert.equal(getSectionItemLabels(section, 'function-mobility'), undefined);
});
it('returns undefined when no customizations', function () {
const section = { key: 's', type: 'permanent', name: { en: '' }, itemKeys: ['function-mobility'] };
assert.equal(getSectionItemLabels(section, 'function-mobility'), undefined);
});
});
describe('collectItemLabelsFromSections', function () {
const mkSection = (key, itemKeys, customs) => ({
key, type: 'permanent', name: { en: key }, itemKeys, itemCustomizations: customs
});
it('returns empty when no section uses the item', function () {
const sources = [{
section: mkSection('s', ['x'], {}),
source: { contactName: 'A' }
}];
assert.deepEqual(collectItemLabelsFromSections('function-mobility', sources), []);
});
it('returns one entry per section with non-empty labels and source attribution', function () {
const sources = [
{
section: mkSection('s1', ['function-mobility'], {
'function-mobility': { labels: { question: { en: 'MOBILITY' } } }
}),
source: { contactName: 'Dr. A', formTitle: { en: 'Form A' } }
},
{
section: mkSection('s2', ['function-mobility'], {
'function-mobility': { labels: { question: { en: 'Walking' } } }
}),
source: { contactName: 'Dr. B', formTitle: { en: 'Form B' } }
}
];
const result = collectItemLabelsFromSections('function-mobility', sources);
assert.equal(result.length, 2);
assert.deepEqual(result[0].question, { en: 'MOBILITY' });
assert.equal(result[0].source.contactName, 'Dr. A');
assert.deepEqual(result[1].question, { en: 'Walking' });
assert.equal(result[1].source.contactName, 'Dr. B');
});
it('skips sections with no labels when requireLabels=true (default)', function () {
const sources = [
{
section: mkSection('s1', ['function-mobility'], {}),
source: { contactName: 'A' }
},
{
section: mkSection('s2', ['function-mobility'], {
'function-mobility': { labels: { question: { en: 'M' } } }
}),
source: { contactName: 'B' }
}
];
const result = collectItemLabelsFromSections('function-mobility', sources);
assert.equal(result.length, 1);
assert.equal(result[0].source.contactName, 'B');
});
it('includes empty-label sections when requireLabels=false', function () {
const sources = [
{
section: mkSection('s', ['function-mobility'], {}),
source: { contactName: 'A' }
}
];
const result = collectItemLabelsFromSections('function-mobility', sources, { requireLabels: false });
assert.equal(result.length, 1);
assert.equal(result[0].source.contactName, 'A');
});
it('deduplicates identical label sets (default)', function () {
const sameLabels = { labels: { question: { en: 'MOBILITY' } } };
const sources = [
{
section: mkSection('s1', ['function-mobility'], { 'function-mobility': sameLabels }),
source: { contactName: 'Dr. A' }
},
{
section: mkSection('s2', ['function-mobility'], { 'function-mobility': sameLabels }),
source: { contactName: 'Dr. B' }
}
];
const result = collectItemLabelsFromSections('function-mobility', sources);
assert.equal(result.length, 1, 'identical labels collapse to one entry');
assert.equal(result[0].source.contactName, 'Dr. A', 'first occurrence wins');
});
it('keeps duplicates when deduplicate=false', function () {
const same = { labels: { question: { en: 'M' } } };
const sources = [
{ section: mkSection('s1', ['m'], { m: same }), source: { contactName: 'A' } },
{ section: mkSection('s2', ['m'], { m: same }), source: { contactName: 'B' } }
];
const result = collectItemLabelsFromSections('m', sources, { deduplicate: false });
assert.equal(result.length, 2);
});
it('preserves the options override map', function () {
const sources = [{
section: mkSection('s', ['function-mobility'], {
'function-mobility': {
labels: {
question: { en: 'MOBILITY' },
options: { 0: { en: 'No problems walking' }, 1: { en: 'Unable to walk' } }
}
}
}),
source: { contactName: 'Dr. A' }
}];
const result = collectItemLabelsFromSections('function-mobility', sources);
assert.equal(result.length, 1);
assert.deepEqual(result[0].options[0], { en: 'No problems walking' });
assert.deepEqual(result[0].options[1], { en: 'Unable to walk' });
});
});
});