|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | | -import { attributeValueToTypedAttributeValue, isAttributeObject } from '../../src/attributes'; |
| 2 | +import { attributeValueToTypedAttributeValue, isAttributeObject, serializeAttributes } from '../../src/attributes'; |
3 | 3 |
|
4 | 4 | describe('attributeValueToTypedAttributeValue', () => { |
5 | 5 | describe('without fallback (default behavior)', () => { |
@@ -267,8 +267,43 @@ describe('attributeValueToTypedAttributeValue', () => { |
267 | 267 | type: 'string', |
268 | 268 | }); |
269 | 269 | }); |
| 270 | + |
| 271 | + it.each([null, { value: null }, { value: null, unit: 'byte' }])('stringifies %s values', value => { |
| 272 | + const result = attributeValueToTypedAttributeValue(value, true); |
| 273 | + expect(result).toMatchObject({ |
| 274 | + value: 'null', |
| 275 | + type: 'string', |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + it.each([undefined, { value: undefined }])('stringifies %s values to ""', value => { |
| 280 | + const result = attributeValueToTypedAttributeValue(value, true); |
| 281 | + expect(result).toEqual({ |
| 282 | + value: '', |
| 283 | + type: 'string', |
| 284 | + }); |
| 285 | + }); |
| 286 | + |
| 287 | + it('stringifies undefined values with unit to ""', () => { |
| 288 | + const result = attributeValueToTypedAttributeValue({ value: undefined, unit: 'byte' }, true); |
| 289 | + expect(result).toEqual({ |
| 290 | + value: '', |
| 291 | + unit: 'byte', |
| 292 | + type: 'string', |
| 293 | + }); |
| 294 | + }); |
270 | 295 | }); |
271 | 296 | }); |
| 297 | + |
| 298 | + describe('with fallback="skip-undefined"', () => { |
| 299 | + it.each([undefined, { value: undefined }, { value: undefined, unit: 'byte' }])( |
| 300 | + 'ignores undefined values (%s)', |
| 301 | + value => { |
| 302 | + const result = attributeValueToTypedAttributeValue(value, 'skip-undefined'); |
| 303 | + expect(result).toBeUndefined(); |
| 304 | + }, |
| 305 | + ); |
| 306 | + }); |
272 | 307 | }); |
273 | 308 |
|
274 | 309 | describe('isAttributeObject', () => { |
@@ -297,3 +332,118 @@ describe('isAttributeObject', () => { |
297 | 332 | }, |
298 | 333 | ); |
299 | 334 | }); |
| 335 | + |
| 336 | +describe('serializeAttributes', () => { |
| 337 | + it('returns an empty object for undefined attributes', () => { |
| 338 | + const result = serializeAttributes(undefined); |
| 339 | + expect(result).toStrictEqual({}); |
| 340 | + }); |
| 341 | + |
| 342 | + it('returns an empty object for an empty object', () => { |
| 343 | + const result = serializeAttributes({}); |
| 344 | + expect(result).toStrictEqual({}); |
| 345 | + }); |
| 346 | + |
| 347 | + it('serializes valid, non-primitive values', () => { |
| 348 | + const result = serializeAttributes({ foo: 'bar', bar: { value: 123 }, baz: { value: 456, unit: 'byte' } }); |
| 349 | + expect(result).toStrictEqual({ |
| 350 | + bar: { |
| 351 | + type: 'integer', |
| 352 | + value: 123, |
| 353 | + }, |
| 354 | + baz: { |
| 355 | + type: 'integer', |
| 356 | + unit: 'byte', |
| 357 | + value: 456, |
| 358 | + }, |
| 359 | + foo: { |
| 360 | + type: 'string', |
| 361 | + value: 'bar', |
| 362 | + }, |
| 363 | + }); |
| 364 | + }); |
| 365 | + |
| 366 | + it('ignores undefined values if fallback is false', () => { |
| 367 | + const result = serializeAttributes( |
| 368 | + { foo: undefined, bar: { value: undefined }, baz: { value: undefined, unit: 'byte' } }, |
| 369 | + false, |
| 370 | + ); |
| 371 | + expect(result).toStrictEqual({}); |
| 372 | + }); |
| 373 | + |
| 374 | + it('ignores undefined values if fallback is "skip-undefined"', () => { |
| 375 | + const result = serializeAttributes( |
| 376 | + { foo: undefined, bar: { value: undefined }, baz: { value: undefined, unit: 'byte' } }, |
| 377 | + 'skip-undefined', |
| 378 | + ); |
| 379 | + expect(result).toStrictEqual({}); |
| 380 | + }); |
| 381 | + |
| 382 | + it('stringifies undefined values to "" if fallback is true', () => { |
| 383 | + const result = serializeAttributes( |
| 384 | + { foo: undefined, bar: { value: undefined }, baz: { value: undefined, unit: 'byte' } }, |
| 385 | + true, |
| 386 | + ); |
| 387 | + expect(result).toStrictEqual({ |
| 388 | + bar: { |
| 389 | + type: 'string', |
| 390 | + value: '', |
| 391 | + }, |
| 392 | + baz: { |
| 393 | + type: 'string', |
| 394 | + unit: 'byte', |
| 395 | + value: '', |
| 396 | + }, |
| 397 | + foo: { type: 'string', value: '' }, |
| 398 | + }); |
| 399 | + }); |
| 400 | + |
| 401 | + it('ignores null values by default', () => { |
| 402 | + const result = serializeAttributes({ foo: null, bar: { value: null }, baz: { value: null, unit: 'byte' } }); |
| 403 | + expect(result).toStrictEqual({}); |
| 404 | + }); |
| 405 | + |
| 406 | + it('stringifies to `"null"` if fallback is true', () => { |
| 407 | + const result = serializeAttributes({ foo: null, bar: { value: null }, baz: { value: null, unit: 'byte' } }, true); |
| 408 | + expect(result).toStrictEqual({ |
| 409 | + foo: { |
| 410 | + type: 'string', |
| 411 | + value: 'null', |
| 412 | + }, |
| 413 | + bar: { |
| 414 | + type: 'string', |
| 415 | + value: 'null', |
| 416 | + }, |
| 417 | + baz: { |
| 418 | + type: 'string', |
| 419 | + unit: 'byte', |
| 420 | + value: 'null', |
| 421 | + }, |
| 422 | + }); |
| 423 | + }); |
| 424 | + |
| 425 | + describe('invalid (non-primitive) values', () => { |
| 426 | + it("doesn't fall back to stringification by default", () => { |
| 427 | + const result = serializeAttributes({ foo: { some: 'object' }, bar: [1, 2, 3], baz: () => {} }); |
| 428 | + expect(result).toStrictEqual({}); |
| 429 | + }); |
| 430 | + |
| 431 | + it('falls back to stringification of unsupported non-primitive values if fallback is true', () => { |
| 432 | + const result = serializeAttributes({ foo: { some: 'object' }, bar: [1, 2, 3], baz: () => {} }, true); |
| 433 | + expect(result).toStrictEqual({ |
| 434 | + bar: { |
| 435 | + type: 'string', |
| 436 | + value: '[1,2,3]', |
| 437 | + }, |
| 438 | + baz: { |
| 439 | + type: 'string', |
| 440 | + value: '', |
| 441 | + }, |
| 442 | + foo: { |
| 443 | + type: 'string', |
| 444 | + value: '{"some":"object"}', |
| 445 | + }, |
| 446 | + }); |
| 447 | + }); |
| 448 | + }); |
| 449 | +}); |
0 commit comments