Skip to content

Commit cf232ff

Browse files
authored
add more tests (#976)
1 parent 8c42223 commit cf232ff

3 files changed

Lines changed: 196 additions & 2 deletions

File tree

meta_configurator/src/schema/__tests__/schemaManipulationUtils.test.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import {beforeEach, describe, expect, it, vi} from 'vitest';
22
import {shallowRef} from 'vue';
33
import {ManagedData} from '@/data/managedData';
44
import {SessionMode} from '@/store/sessionMode';
5-
import {extractInlinedSchemaElement} from '@/schema/schemaManipulationUtils';
5+
import {
6+
extractAllInlinedSchemaElements,
7+
extractInlinedSchemaElement,
8+
} from '@/schema/schemaManipulationUtils';
69

710
vi.mock('@/dataformats/formatRegistry', () => ({
811
useDataConverter: () => ({
@@ -158,4 +161,69 @@ describe('schemaManipulationUtils', () => {
158161
},
159162
});
160163
});
164+
165+
it('extracts inlined subschemas that are nested inside an existing $defs entry', () => {
166+
schemaData = new ManagedData(
167+
shallowRef({
168+
type: 'object',
169+
properties: {
170+
vehicle: {
171+
$ref: '#/$defs/vehicle',
172+
},
173+
},
174+
$defs: {
175+
vehicle: {
176+
type: 'object',
177+
properties: {
178+
path: {
179+
type: 'object',
180+
properties: {
181+
waypoint: {
182+
type: 'string',
183+
},
184+
},
185+
},
186+
pathCopy: {
187+
$ref: '#/$defs/vehicle/properties/path',
188+
},
189+
},
190+
},
191+
},
192+
}),
193+
SessionMode.SchemaEditor
194+
);
195+
196+
const extractedCount = extractAllInlinedSchemaElements(schemaData, false, true);
197+
198+
expect(extractedCount).toBe(1);
199+
expect(schemaData.data.value).toEqual({
200+
type: 'object',
201+
properties: {
202+
vehicle: {
203+
$ref: '#/$defs/vehicle',
204+
},
205+
},
206+
$defs: {
207+
vehicle: {
208+
type: 'object',
209+
properties: {
210+
path: {
211+
$ref: '#/$defs/path',
212+
},
213+
pathCopy: {
214+
$ref: '#/$defs/path',
215+
},
216+
},
217+
},
218+
path: {
219+
type: 'object',
220+
properties: {
221+
waypoint: {
222+
type: 'string',
223+
},
224+
},
225+
},
226+
},
227+
});
228+
});
161229
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {beforeEach, describe, expect, it, vi} from 'vitest';
2+
import {shallowRef} from 'vue';
3+
import {ManagedData} from '@/data/managedData';
4+
import {SessionMode} from '@/store/sessionMode';
5+
import {deleteSchemaElement} from '@/utility/deleteUtils';
6+
7+
vi.mock('@/dataformats/formatRegistry', () => ({
8+
useDataConverter: () => ({
9+
stringify: (data: any) => JSON.stringify(data),
10+
parse: (data: string) => JSON.parse(data),
11+
}),
12+
}));
13+
14+
describe('deleteUtils', () => {
15+
let schemaData: ManagedData;
16+
17+
beforeEach(() => {
18+
schemaData = new ManagedData(
19+
shallowRef({
20+
type: 'object',
21+
required: ['name', 'age'],
22+
properties: {
23+
name: {
24+
type: 'string',
25+
},
26+
age: {
27+
type: 'number',
28+
},
29+
nickname: {
30+
type: 'string',
31+
},
32+
},
33+
}),
34+
SessionMode.SchemaEditor
35+
);
36+
});
37+
38+
it('deletes a schema property and removes it from the parent required list', () => {
39+
deleteSchemaElement(schemaData, ['properties', 'name']);
40+
41+
expect(schemaData.data.value).toEqual({
42+
type: 'object',
43+
required: ['age'],
44+
properties: {
45+
age: {
46+
type: 'number',
47+
},
48+
nickname: {
49+
type: 'string',
50+
},
51+
},
52+
});
53+
});
54+
55+
it('deletes a non-required schema property without changing the required list', () => {
56+
deleteSchemaElement(schemaData, ['properties', 'nickname']);
57+
58+
expect(schemaData.data.value).toEqual({
59+
type: 'object',
60+
required: ['name', 'age'],
61+
properties: {
62+
name: {
63+
type: 'string',
64+
},
65+
age: {
66+
type: 'number',
67+
},
68+
},
69+
});
70+
});
71+
});

meta_configurator/src/utility/__tests__/renameUtils.test.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, expect, it, vi} from 'vitest';
2-
import {replacePropertyNameUtils} from '../renameUtils';
2+
import {replacePropertyNameUtils, updateReferences} from '../renameUtils';
33
import {META_SCHEMA_SIMPLIFIED} from '../../packaged-schemas/metaSchemaSimplified';
44
import {type Path} from '../path';
55
import _ from 'lodash';
@@ -208,4 +208,59 @@ describe('test renameUtils', () => {
208208
},
209209
});
210210
});
211+
212+
it('updates only matching $ref targets when references are rewritten directly', () => {
213+
const currentData = {
214+
properties: {
215+
source: {
216+
type: 'object',
217+
},
218+
consumer: {
219+
$ref: '#/properties/source',
220+
},
221+
nestedConsumer: {
222+
allOf: [
223+
{
224+
$ref: '#/properties/source/child',
225+
},
226+
],
227+
},
228+
unrelated: {
229+
$ref: '#/properties/sourceExtra',
230+
},
231+
},
232+
};
233+
234+
const updateDataFct = (subPath: Path, newValue: any) => {
235+
_.set(currentData, subPath, newValue);
236+
};
237+
238+
updateReferences(
239+
['properties', 'source'],
240+
['properties', 'renamedSource'],
241+
currentData,
242+
updateDataFct
243+
);
244+
245+
expect(currentData).toEqual({
246+
properties: {
247+
source: {
248+
type: 'object',
249+
},
250+
consumer: {
251+
$ref: '#/properties/renamedSource',
252+
},
253+
nestedConsumer: {
254+
allOf: [
255+
{
256+
$ref: '#/properties/renamedSource/child',
257+
},
258+
],
259+
},
260+
unrelated: {
261+
$ref: '#/properties/sourceExtra',
262+
},
263+
},
264+
});
265+
});
211266
});

0 commit comments

Comments
 (0)