|
| 1 | +import { |
| 2 | + createMockArraySchema, |
| 3 | + createMockNumberSchema, |
| 4 | + createMockObjectSchema, |
| 5 | + createMockStringSchema, |
| 6 | +} from '../../../__tests__/helpers.test'; |
| 7 | +import { |
| 8 | + getSchemaByInstancePath, |
| 9 | + getSchemaBySchemaPath, |
| 10 | + getValuePaths, |
| 11 | + parseInstancePath, |
| 12 | + parseSchemaPath, |
| 13 | +} from '../utils'; |
| 14 | + |
| 15 | +const nameSchema = createMockStringSchema(); |
| 16 | +const streetSchema = createMockStringSchema(); |
| 17 | +const citySchema = createMockStringSchema(); |
| 18 | +const addressSchema = createMockObjectSchema({street: streetSchema, city: citySchema}); |
| 19 | +const typeStringSchema = createMockStringSchema(); |
| 20 | +const typeNumberSchema = createMockNumberSchema(); |
| 21 | +const tagsSchema = createMockArraySchema([typeStringSchema, typeNumberSchema]); |
| 22 | +const labelsSchema = createMockArraySchema(typeStringSchema); |
| 23 | +const specialFieldSchema = createMockNumberSchema(); |
| 24 | +const testSchema = createMockObjectSchema({ |
| 25 | + name: nameSchema, |
| 26 | + address: addressSchema, |
| 27 | + tags: tagsSchema, |
| 28 | + labels: labelsSchema, |
| 29 | + 'special/field': specialFieldSchema, |
| 30 | +}); |
| 31 | + |
| 32 | +describe('SchemaRendererServiceField/utils', () => { |
| 33 | + describe('parseSchemaPath', () => { |
| 34 | + it('should parse a simple schema path', () => { |
| 35 | + const result = parseSchemaPath('#/properties/name/minLength'); |
| 36 | + |
| 37 | + expect(result).toEqual(['properties', 'name']); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should parse a nested schema path', () => { |
| 41 | + const result = parseSchemaPath('#/properties/address/properties/street/maxLength'); |
| 42 | + |
| 43 | + expect(result).toEqual(['properties', 'address', 'properties', 'street']); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle URL encoded characters', () => { |
| 47 | + const result = parseSchemaPath('#/properties/special%20field/minimum'); |
| 48 | + |
| 49 | + expect(result).toEqual(['properties', 'special field']); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle schema path with only one segment', () => { |
| 53 | + const result = parseSchemaPath('#/type'); |
| 54 | + |
| 55 | + expect(result).toEqual([]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle path with tilde escaping for slash', () => { |
| 59 | + const result = parseSchemaPath('#/properties/path~1to~1file/maximum'); |
| 60 | + |
| 61 | + expect(result).toEqual(['properties', 'path/to/file']); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle path with tilde escaping for tilde', () => { |
| 65 | + const result = parseSchemaPath('#/properties/tilde~0character/not'); |
| 66 | + |
| 67 | + expect(result).toEqual(['properties', 'tilde~character']); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle complex paths with multiple escaped characters', () => { |
| 71 | + const result = parseSchemaPath( |
| 72 | + '#/properties/complex~1path~0with~1special%20chars/const', |
| 73 | + ); |
| 74 | + |
| 75 | + expect(result).toEqual(['properties', 'complex/path~with/special chars']); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle paths with array indexes', () => { |
| 79 | + const result = parseSchemaPath('#/properties/items/0/name/minLength'); |
| 80 | + |
| 81 | + expect(result).toEqual(['properties', 'items', '0', 'name']); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return an empty array for invalid schema path format', () => { |
| 85 | + const result = parseSchemaPath('invalid-path'); |
| 86 | + |
| 87 | + expect(result).toEqual([]); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('parseInstancePath', () => { |
| 92 | + it('should return an empty array for empty instance path', () => { |
| 93 | + const result = parseInstancePath(''); |
| 94 | + |
| 95 | + expect(result).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should parse a simple instance path', () => { |
| 99 | + const result = parseInstancePath('/name'); |
| 100 | + |
| 101 | + expect(result).toEqual(['name']); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should parse a nested instance path', () => { |
| 105 | + const result = parseInstancePath('/address/street'); |
| 106 | + |
| 107 | + expect(result).toEqual(['address', 'street']); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should handle path with tilde escaping for slash', () => { |
| 111 | + const result = parseInstancePath('/path~1to~1file'); |
| 112 | + |
| 113 | + expect(result).toEqual(['path/to/file']); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should handle path with tilde escaping for tilde', () => { |
| 117 | + const result = parseInstancePath('/tilde~0character'); |
| 118 | + |
| 119 | + expect(result).toEqual(['tilde~character']); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should handle complex paths with multiple escaped characters', () => { |
| 123 | + const result = parseInstancePath('/complex~1path~0with~1special'); |
| 124 | + |
| 125 | + expect(result).toEqual(['complex/path~with/special']); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should handle paths with array indexes', () => { |
| 129 | + const result = parseInstancePath('/items/0/name'); |
| 130 | + |
| 131 | + expect(result).toEqual(['items', '0', 'name']); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle deep nested paths', () => { |
| 135 | + const result = parseInstancePath('/users/5/addresses/2/street'); |
| 136 | + |
| 137 | + expect(result).toEqual(['users', '5', 'addresses', '2', 'street']); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle paths with consecutive slashes', () => { |
| 141 | + const result = parseInstancePath('/path//with//double/slashes'); |
| 142 | + |
| 143 | + expect(result).toEqual(['path', '', 'with', '', 'double', 'slashes']); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('getSchemaBySchemaPath', () => { |
| 148 | + it('should return the main schema for empty path array', () => { |
| 149 | + const result = getSchemaBySchemaPath('#/', testSchema); |
| 150 | + |
| 151 | + expect(result).toBe(testSchema); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should return the schema for a simple path', () => { |
| 155 | + const result = getSchemaBySchemaPath('#/properties/name/minLength', testSchema); |
| 156 | + |
| 157 | + expect(result).toBe(nameSchema); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should return the schema for a nested path', () => { |
| 161 | + const result = getSchemaBySchemaPath( |
| 162 | + '#/properties/address/properties/street/minLength', |
| 163 | + testSchema, |
| 164 | + ); |
| 165 | + |
| 166 | + expect(result).toBe(streetSchema); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should return the schema for a path with array indexes', () => { |
| 170 | + const result = getSchemaBySchemaPath('#/properties/tags/items/1/maximum', testSchema); |
| 171 | + |
| 172 | + expect(result).toBe(typeNumberSchema); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should handle array with single schema for items', () => { |
| 176 | + const result = getSchemaBySchemaPath('#/properties/labels/items/minLength', testSchema); |
| 177 | + |
| 178 | + expect(result).toBe(typeStringSchema); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should return undefined for a path that does not exist', () => { |
| 182 | + const result = getSchemaBySchemaPath('#/properties/nonexistent/minLength', testSchema); |
| 183 | + |
| 184 | + expect(result).toBeUndefined(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should handle paths with special characters', () => { |
| 188 | + const result = getSchemaBySchemaPath( |
| 189 | + '#/properties/special~1field/maxLength', |
| 190 | + testSchema, |
| 191 | + ); |
| 192 | + |
| 193 | + expect(result).toBe(specialFieldSchema); |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + describe('getSchemaByInstancePath', () => { |
| 198 | + it('should return the main schema for empty instance path', () => { |
| 199 | + const result = getSchemaByInstancePath('', testSchema); |
| 200 | + |
| 201 | + expect(result).toBe(testSchema); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should return the schema for a simple object property path', () => { |
| 205 | + const result = getSchemaByInstancePath('/name', testSchema); |
| 206 | + |
| 207 | + expect(result).toBe(nameSchema); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should return the schema for a nested object property path', () => { |
| 211 | + const result = getSchemaByInstancePath('/address/street', testSchema); |
| 212 | + |
| 213 | + expect(result).toBe(streetSchema); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should return the schema for an array item path with specific index', () => { |
| 217 | + const result = getSchemaByInstancePath('/tags/1', testSchema); |
| 218 | + |
| 219 | + expect(result).toBe(typeNumberSchema); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should handle array with single schema for items', () => { |
| 223 | + const result = getSchemaByInstancePath('/labels/0', testSchema); |
| 224 | + |
| 225 | + expect(result).toBe(typeStringSchema); |
| 226 | + }); |
| 227 | + |
| 228 | + it('should return undefined for a path that does not exist', () => { |
| 229 | + const result = getSchemaByInstancePath('/nonexistent', testSchema); |
| 230 | + |
| 231 | + expect(result).toBeUndefined(); |
| 232 | + }); |
| 233 | + |
| 234 | + it('should handle paths with special characters', () => { |
| 235 | + const result = getSchemaByInstancePath('/special~1field', testSchema); |
| 236 | + |
| 237 | + expect(result).toBe(specialFieldSchema); |
| 238 | + }); |
| 239 | + |
| 240 | + it('should return undefined for a path that starts valid but ends invalid', () => { |
| 241 | + const result = getSchemaByInstancePath('/address/nonexistent', testSchema); |
| 242 | + |
| 243 | + expect(result).toBeUndefined(); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should return undefined when traversing non-object and non-array schemas', () => { |
| 247 | + const result = getSchemaByInstancePath('/name/invalid', testSchema); |
| 248 | + |
| 249 | + expect(result).toBeUndefined(); |
| 250 | + }); |
| 251 | + }); |
| 252 | + |
| 253 | + describe('getValuePaths', () => { |
| 254 | + it('should return a path for a primitive value', () => { |
| 255 | + const result = getValuePaths('test'); |
| 256 | + expect(result).toEqual([]); |
| 257 | + }); |
| 258 | + |
| 259 | + it('should return a path for a number value', () => { |
| 260 | + const result = getValuePaths(42); |
| 261 | + expect(result).toEqual([]); |
| 262 | + }); |
| 263 | + |
| 264 | + it('should return a path for a boolean value', () => { |
| 265 | + const result = getValuePaths(true); |
| 266 | + expect(result).toEqual([]); |
| 267 | + }); |
| 268 | + |
| 269 | + it('should return a path for null', () => { |
| 270 | + const result = getValuePaths(null); |
| 271 | + expect(result).toEqual([]); |
| 272 | + }); |
| 273 | + |
| 274 | + it('should return a path for undefined', () => { |
| 275 | + const result = getValuePaths(undefined); |
| 276 | + expect(result).toEqual([]); |
| 277 | + }); |
| 278 | + |
| 279 | + it('should return an empty array for an empty array', () => { |
| 280 | + const result = getValuePaths([]); |
| 281 | + |
| 282 | + expect(result).toEqual([]); |
| 283 | + }); |
| 284 | + |
| 285 | + it('should return an empty array for an empty object', () => { |
| 286 | + const result = getValuePaths({}); |
| 287 | + |
| 288 | + expect(result).toEqual([]); |
| 289 | + }); |
| 290 | + |
| 291 | + it('should return paths for an array of primitive values', () => { |
| 292 | + const result = getValuePaths(['a', 'b', 'c']); |
| 293 | + |
| 294 | + expect(result).toEqual([['0'], ['1'], ['2']]); |
| 295 | + }); |
| 296 | + |
| 297 | + it('should return paths for an object with primitive values', () => { |
| 298 | + const result = getValuePaths({a: 1, b: 2, c: 3}); |
| 299 | + |
| 300 | + expect(result).toEqual([['a'], ['b'], ['c']]); |
| 301 | + }); |
| 302 | + |
| 303 | + it('should return paths for a nested array', () => { |
| 304 | + const result = getValuePaths([ |
| 305 | + ['a', 'b'], |
| 306 | + ['c', 'd'], |
| 307 | + ]); |
| 308 | + |
| 309 | + expect(result).toEqual([ |
| 310 | + ['0', '0'], |
| 311 | + ['0', '1'], |
| 312 | + ['1', '0'], |
| 313 | + ['1', '1'], |
| 314 | + ]); |
| 315 | + }); |
| 316 | + |
| 317 | + it('should return paths for a nested object', () => { |
| 318 | + const result = getValuePaths({a: {b: 1}, c: {d: 2}}); |
| 319 | + |
| 320 | + expect(result).toEqual([ |
| 321 | + ['a', 'b'], |
| 322 | + ['c', 'd'], |
| 323 | + ]); |
| 324 | + }); |
| 325 | + |
| 326 | + it('should return paths for a mixed nested array and object', () => { |
| 327 | + const result = getValuePaths([{a: 1}, {b: 2}]); |
| 328 | + |
| 329 | + expect(result).toEqual([ |
| 330 | + ['0', 'a'], |
| 331 | + ['1', 'b'], |
| 332 | + ]); |
| 333 | + }); |
| 334 | + |
| 335 | + it('should return paths for a complex nested structure', () => { |
| 336 | + const result = getValuePaths({ |
| 337 | + a: [1, 2], |
| 338 | + b: {c: 3, d: [4, {e: 5}]}, |
| 339 | + }); |
| 340 | + |
| 341 | + expect(result).toEqual([ |
| 342 | + ['a', '0'], |
| 343 | + ['a', '1'], |
| 344 | + ['b', 'c'], |
| 345 | + ['b', 'd', '0'], |
| 346 | + ['b', 'd', '1', 'e'], |
| 347 | + ]); |
| 348 | + }); |
| 349 | + |
| 350 | + it('should handle initial path parameter', () => { |
| 351 | + const result = getValuePaths( |
| 352 | + { |
| 353 | + a: [1, 2], |
| 354 | + b: {c: 3, d: [4, {e: 5}]}, |
| 355 | + }, |
| 356 | + ['parent'], |
| 357 | + ); |
| 358 | + |
| 359 | + expect(result).toEqual([ |
| 360 | + ['parent', 'a', '0'], |
| 361 | + ['parent', 'a', '1'], |
| 362 | + ['parent', 'b', 'c'], |
| 363 | + ['parent', 'b', 'd', '0'], |
| 364 | + ['parent', 'b', 'd', '1', 'e'], |
| 365 | + ]); |
| 366 | + }); |
| 367 | + }); |
| 368 | +}); |
0 commit comments