-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemDefToSchema.test.ts
More file actions
137 lines (125 loc) · 4.84 KB
/
itemDefToSchema.test.ts
File metadata and controls
137 lines (125 loc) · 4.84 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
import { describe, it, expect } from 'vitest';
import { jsonFormForItemDef } from '../src/schema/itemDefToSchema';
import type { ItemDef } from '../src/schema/itemDefToSchema';
function makeItemDef (data: Partial<ItemDef['data']>, template?: Record<string, unknown>): ItemDef {
return {
data: {
type: 'text',
label: { en: 'Test' },
...data
} as any,
eventTemplate: () => (template || { streamIds: ['s1'], type: 'note/txt' })
};
}
describe('jsonFormForItemDef', () => {
it('standard case: wraps content in object', () => {
const itemDef = makeItemDef({ type: 'text', label: { en: 'Name' } });
const result = jsonFormForItemDef(itemDef);
const schema = result.schema as any;
expect(schema.type).toBe('object');
expect(schema.properties.content).toBeDefined();
expect(schema.properties.content.type).toBe('string');
expect(schema.required).toEqual(['content']);
});
it('eventDataForFormData merges with template', () => {
const itemDef = makeItemDef(
{ type: 'number', label: { en: 'Temp' } },
{ streamIds: ['s1'], type: 'temperature/c' }
);
const result = jsonFormForItemDef(itemDef);
const eventData = result.eventDataForFormData({ content: 37.5 });
expect(eventData).toEqual({
streamIds: ['s1'],
type: 'temperature/c',
content: 37.5
});
});
describe('activity/plain checkbox', () => {
it('returns schema directly (not wrapped)', () => {
const itemDef = makeItemDef({
type: 'checkbox',
label: { en: 'Walk' },
eventType: 'activity/plain'
});
const result = jsonFormForItemDef(itemDef);
const schema = result.schema as any;
expect(schema.type).toBe('boolean');
expect(schema.properties).toBeUndefined();
});
it('processData filters false → null', () => {
const itemDef = makeItemDef(
{ type: 'checkbox', label: { en: 'Walk' }, eventType: 'activity/plain' },
{ streamIds: ['s1'], type: 'activity/plain' }
);
const result = jsonFormForItemDef(itemDef);
expect(result.eventDataForFormData(false as any)).toBeNull();
});
it('processData passes true → event created', () => {
const itemDef = makeItemDef(
{ type: 'checkbox', label: { en: 'Walk' }, eventType: 'activity/plain' },
{ streamIds: ['s1'], type: 'activity/plain' }
);
const result = jsonFormForItemDef(itemDef);
const eventData = result.eventDataForFormData(true as any);
expect(eventData).not.toBeNull();
});
});
describe('ratio/generic select', () => {
// ratio/generic itemDefs have options both as array (for schemaFor/SelectData)
// and as Record keys (for relativeTo computation via Object.keys)
const selectOptions = [
{ value: 0, label: { en: '0' } },
{ value: 5, label: { en: '5' } },
{ value: 10, label: { en: '10' } }
];
function makeRatioItemDef (template?: Record<string, unknown>) {
const itemDef = makeItemDef(
{ type: 'select', label: { en: 'Pain' }, eventType: 'ratio/generic' },
template
);
// schemaFor needs array format (SelectData.options)
(itemDef.data as any).options = selectOptions;
return itemDef;
}
it('wraps with nested content.value structure', () => {
const result = jsonFormForItemDef(makeRatioItemDef());
const schema = result.schema as any;
expect(schema.properties.content.properties.value).toBeDefined();
});
it('processData injects relativeTo', () => {
const result = jsonFormForItemDef(makeRatioItemDef({ streamIds: ['s1'], type: 'ratio/generic' }));
const eventData = result.eventDataForFormData({ content: { value: 5 } }) as any;
// With the fix, relativeTo is the max option value (10), not max array index
expect(eventData.content.relativeTo).toBe(10);
});
it('returns null when content is null', () => {
const result = jsonFormForItemDef(makeRatioItemDef({ streamIds: ['s1'], type: 'ratio/generic' }));
const eventData = result.eventDataForFormData({ content: null } as any);
expect(eventData).toBeNull();
});
});
describe('variations', () => {
it('adds type selector to schema', () => {
const itemDef = makeItemDef({
type: 'text',
label: { en: 'Symptom' },
variations: {
eventType: {
label: { en: 'Category' },
options: [
{ value: 'mild', label: { en: 'Mild' } },
{ value: 'severe', label: { en: 'Severe' } }
]
}
}
});
const result = jsonFormForItemDef(itemDef);
const schema = result.schema as any;
expect(schema.properties.type).toBeDefined();
expect(schema.properties.type.oneOf).toEqual([
{ const: 'mild', title: 'Mild' },
{ const: 'severe', title: 'Severe' }
]);
});
});
});