|
1 | 1 | import test from 'ava';
|
2 | 2 | import { ControlElement } from '../../src/models';
|
3 |
| -import { createCombinatorRenderInfos } from '../../src/mappers'; |
| 3 | +import { |
| 4 | + createCombinatorRenderInfos, |
| 5 | + getCombinatorIndexOfFittingSchema, |
| 6 | +} from '../../src/mappers'; |
4 | 7 |
|
5 | 8 | const rootSchema = {
|
6 | 9 | type: 'object',
|
@@ -111,3 +114,244 @@ test('createCombinatorRenderInfos - uses keyword + index when no labels provided
|
111 | 114 | t.deepEqual(duaRenderInfo.label, 'anyOf-0');
|
112 | 115 | t.deepEqual(lipaRenderInfo.label, 'anyOf-1');
|
113 | 116 | });
|
| 117 | + |
| 118 | +const schemaWithCustomIdProperty = { |
| 119 | + properties: { |
| 120 | + customId: { const: '123' }, |
| 121 | + }, |
| 122 | +}; |
| 123 | + |
| 124 | +const schemaWithId = { |
| 125 | + properties: { |
| 126 | + id: { const: '123' }, |
| 127 | + }, |
| 128 | +}; |
| 129 | + |
| 130 | +const schemaWithIdWithoutConst = { |
| 131 | + properties: { |
| 132 | + type: { type: 'string' }, |
| 133 | + }, |
| 134 | +}; |
| 135 | + |
| 136 | +const schemaWithType = { |
| 137 | + properties: { |
| 138 | + type: { const: 'typeValue' }, |
| 139 | + }, |
| 140 | +}; |
| 141 | + |
| 142 | +const schemaWithKind = { |
| 143 | + properties: { |
| 144 | + kind: { const: 'kindValue' }, |
| 145 | + }, |
| 146 | +}; |
| 147 | + |
| 148 | +const schemaWithFirstString = { |
| 149 | + properties: { |
| 150 | + obj: { type: 'object' }, |
| 151 | + name: { const: 'John' }, |
| 152 | + }, |
| 153 | +}; |
| 154 | + |
| 155 | +const schemaWithFirstNumber = { |
| 156 | + properties: { |
| 157 | + obj: { type: 'object' }, |
| 158 | + identity: { const: 123 }, |
| 159 | + }, |
| 160 | +}; |
| 161 | + |
| 162 | +const schemaWithFirstNumberWithoutConst = { |
| 163 | + properties: { |
| 164 | + obj: { type: 'object' }, |
| 165 | + identity: { type: 'number' }, |
| 166 | + }, |
| 167 | +}; |
| 168 | + |
| 169 | +const indexRootSchema = { |
| 170 | + definitions: { |
| 171 | + schemaWithCustomIdProperty, |
| 172 | + schemaWithId, |
| 173 | + schemaWithIdWithoutConst, |
| 174 | + schemaWithType, |
| 175 | + schemaWithKind, |
| 176 | + schemaWithFirstString, |
| 177 | + schemaWithFirstNumber, |
| 178 | + schemaWithFirstNumberWithoutConst, |
| 179 | + }, |
| 180 | +}; |
| 181 | + |
| 182 | +test('getCombinatorIndexOfFittingSchema - schema with x-jsf-type-property', (t) => { |
| 183 | + const data = { customId: '123' }; |
| 184 | + const keyword = 'anyOf'; |
| 185 | + const schema = { |
| 186 | + anyOf: [schemaWithId, schemaWithCustomIdProperty], |
| 187 | + 'x-jsf-type-property': 'customId', |
| 188 | + }; |
| 189 | + |
| 190 | + const result = getCombinatorIndexOfFittingSchema( |
| 191 | + data, |
| 192 | + keyword, |
| 193 | + schema, |
| 194 | + indexRootSchema |
| 195 | + ); |
| 196 | + t.is(result, 1); |
| 197 | +}); |
| 198 | + |
| 199 | +test('getCombinatorIndexOfFittingSchema - data with id property', (t) => { |
| 200 | + const data = { id: '123' }; |
| 201 | + const keyword = 'anyOf'; |
| 202 | + const schema = { anyOf: [schemaWithId, schemaWithKind] }; |
| 203 | + |
| 204 | + const result = getCombinatorIndexOfFittingSchema( |
| 205 | + data, |
| 206 | + keyword, |
| 207 | + schema, |
| 208 | + indexRootSchema |
| 209 | + ); |
| 210 | + t.is(result, 0); |
| 211 | +}); |
| 212 | + |
| 213 | +test('getCombinatorIndexOfFittingSchema - data with id property without const', (t) => { |
| 214 | + const data = { id: '123', type: 'typeValue' }; |
| 215 | + const keyword = 'anyOf'; |
| 216 | + const schema = { anyOf: [schemaWithIdWithoutConst, schemaWithKind] }; |
| 217 | + |
| 218 | + const result = getCombinatorIndexOfFittingSchema( |
| 219 | + data, |
| 220 | + keyword, |
| 221 | + schema, |
| 222 | + indexRootSchema |
| 223 | + ); |
| 224 | + // First schema does not have a const and, thus, cannot match |
| 225 | + t.is(result, -1); |
| 226 | +}); |
| 227 | + |
| 228 | +test('getCombinatorIndexOfFittingSchema - data with unfitting id property value', (t) => { |
| 229 | + const data = { id: '321' }; |
| 230 | + const keyword = 'anyOf'; |
| 231 | + const schema = { anyOf: [schemaWithId, schemaWithKind] }; |
| 232 | + |
| 233 | + const result = getCombinatorIndexOfFittingSchema( |
| 234 | + data, |
| 235 | + keyword, |
| 236 | + schema, |
| 237 | + indexRootSchema |
| 238 | + ); |
| 239 | + t.is(result, -1); |
| 240 | +}); |
| 241 | + |
| 242 | +test('getCombinatorIndexOfFittingSchema - data with type property', (t) => { |
| 243 | + const data = { type: 'typeValue' }; |
| 244 | + const keyword = 'anyOf'; |
| 245 | + const schema = { anyOf: [schemaWithId, schemaWithType] }; |
| 246 | + |
| 247 | + const result = getCombinatorIndexOfFittingSchema( |
| 248 | + data, |
| 249 | + keyword, |
| 250 | + schema, |
| 251 | + indexRootSchema |
| 252 | + ); |
| 253 | + t.is(result, 1); |
| 254 | +}); |
| 255 | + |
| 256 | +test('getCombinatorIndexOfFittingSchema - data with unfitting type property value', (t) => { |
| 257 | + const data = { type: 'wrongTypeValue' }; |
| 258 | + const keyword = 'anyOf'; |
| 259 | + const schema = { anyOf: [schemaWithId, schemaWithType] }; |
| 260 | + |
| 261 | + const result = getCombinatorIndexOfFittingSchema( |
| 262 | + data, |
| 263 | + keyword, |
| 264 | + schema, |
| 265 | + indexRootSchema |
| 266 | + ); |
| 267 | + t.is(result, -1); |
| 268 | +}); |
| 269 | + |
| 270 | +test('getCombinatorIndexOfFittingSchema - schema with refs and data with type property', (t) => { |
| 271 | + const data = { type: 'typeValue' }; |
| 272 | + const keyword = 'anyOf'; |
| 273 | + const schema = { |
| 274 | + anyOf: [ |
| 275 | + { $ref: '#/definitions/schemaWithId' }, |
| 276 | + { $ref: '#/definitions/schemaWithType' }, |
| 277 | + ], |
| 278 | + }; |
| 279 | + |
| 280 | + const result = getCombinatorIndexOfFittingSchema( |
| 281 | + data, |
| 282 | + keyword, |
| 283 | + schema, |
| 284 | + indexRootSchema |
| 285 | + ); |
| 286 | + t.is(result, 1); |
| 287 | +}); |
| 288 | + |
| 289 | +test('getCombinatorIndexOfFittingSchema - data with kind property', (t) => { |
| 290 | + const data = { kind: 'kindValue' }; |
| 291 | + const keyword = 'anyOf'; |
| 292 | + const schema = { anyOf: [schemaWithKind] }; |
| 293 | + |
| 294 | + const result = getCombinatorIndexOfFittingSchema( |
| 295 | + data, |
| 296 | + keyword, |
| 297 | + schema, |
| 298 | + indexRootSchema |
| 299 | + ); |
| 300 | + t.is(result, 0); |
| 301 | +}); |
| 302 | + |
| 303 | +test('getCombinatorIndexOfFittingSchema - data with unfitting kind property value', (t) => { |
| 304 | + const data = { kind: 'wrongKindValue' }; |
| 305 | + const keyword = 'anyOf'; |
| 306 | + const schema = { anyOf: [schemaWithKind] }; |
| 307 | + |
| 308 | + const result = getCombinatorIndexOfFittingSchema( |
| 309 | + data, |
| 310 | + keyword, |
| 311 | + schema, |
| 312 | + indexRootSchema |
| 313 | + ); |
| 314 | + t.is(result, -1); |
| 315 | +}); |
| 316 | + |
| 317 | +test('getCombinatorIndexOfFittingSchema - data with first string property', (t) => { |
| 318 | + const data = { obj: {}, name: 'John' }; |
| 319 | + const keyword = 'anyOf'; |
| 320 | + const schema = { anyOf: [{}, schemaWithFirstString] }; |
| 321 | + |
| 322 | + const result = getCombinatorIndexOfFittingSchema( |
| 323 | + data, |
| 324 | + keyword, |
| 325 | + schema, |
| 326 | + indexRootSchema |
| 327 | + ); |
| 328 | + t.is(result, 1); |
| 329 | +}); |
| 330 | + |
| 331 | +test('getCombinatorIndexOfFittingSchema - data with first number property', (t) => { |
| 332 | + const data = { obj: {}, identity: 123 }; |
| 333 | + const keyword = 'anyOf'; |
| 334 | + const schema = { anyOf: [schemaWithFirstNumber] }; |
| 335 | + |
| 336 | + const result = getCombinatorIndexOfFittingSchema( |
| 337 | + data, |
| 338 | + keyword, |
| 339 | + schema, |
| 340 | + indexRootSchema |
| 341 | + ); |
| 342 | + t.is(result, 0); |
| 343 | +}); |
| 344 | + |
| 345 | +test('getCombinatorIndexOfFittingSchema - no matching schema', (t) => { |
| 346 | + const data = { name: 'Doe' }; |
| 347 | + const keyword = 'anyOf'; |
| 348 | + const schema = { anyOf: [schemaWithFirstString] }; |
| 349 | + |
| 350 | + const result = getCombinatorIndexOfFittingSchema( |
| 351 | + data, |
| 352 | + keyword, |
| 353 | + schema, |
| 354 | + indexRootSchema |
| 355 | + ); |
| 356 | + t.is(result, -1); |
| 357 | +}); |
0 commit comments