|
10 | 10 | import { expect, test } from '@jest/globals';
|
11 | 11 | import { float, float32, int8, integer, PrimaryKey, Reference } from '../src/reflection/type.js';
|
12 | 12 | import { is } from '../src/typeguard.js';
|
| 13 | +import { Serializer } from '../src/serializer.js'; |
| 14 | +import { cast } from '../src/serializer-facade.js'; |
| 15 | +import { isReferenceInstance } from '../src/reference.js'; |
13 | 16 |
|
14 | 17 | test('primitive string', () => {
|
15 | 18 | expect(is<string>('a')).toEqual(true);
|
@@ -198,10 +201,10 @@ test('array any', () => {
|
198 | 201 | expect(is<any[]>(true)).toEqual(false);
|
199 | 202 | expect(is<any[]>({})).toEqual(false);
|
200 | 203 |
|
201 |
| - expect(is<any[]>({length:1})).toEqual(false); |
202 |
| - expect(is<any[]>({length:0})).toEqual(false); |
203 |
| - expect(is<any[]>({length:null})).toEqual(false); |
204 |
| - expect(is<any[]>({length:undefined})).toEqual(false); |
| 204 | + expect(is<any[]>({ length: 1 })).toEqual(false); |
| 205 | + expect(is<any[]>({ length: 0 })).toEqual(false); |
| 206 | + expect(is<any[]>({ length: null })).toEqual(false); |
| 207 | + expect(is<any[]>({ length: undefined })).toEqual(false); |
205 | 208 | });
|
206 | 209 |
|
207 | 210 | test('union', () => {
|
@@ -397,15 +400,50 @@ test('class with literal and default', () => {
|
397 | 400 | readConcernLevel: 'local' = 'local';
|
398 | 401 | }
|
399 | 402 |
|
400 |
| - expect(is<ConnectionOptions>({readConcernLevel: 'local'})).toBe(true); |
401 |
| - expect(is<ConnectionOptions>({readConcernLevel: 'local2'})).toBe(false); |
| 403 | + expect(is<ConnectionOptions>({ readConcernLevel: 'local' })).toBe(true); |
| 404 | + expect(is<ConnectionOptions>({ readConcernLevel: 'local2' })).toBe(false); |
402 | 405 | });
|
403 | 406 |
|
404 | 407 | test('union literal', () => {
|
405 | 408 | class ConnectionOptions {
|
406 | 409 | readConcernLevel: 'local' | 'majority' | 'linearizable' | 'available' = 'majority';
|
407 | 410 | }
|
408 | 411 |
|
409 |
| - expect(is<ConnectionOptions>({readConcernLevel: 'majority'})).toBe(true); |
410 |
| - expect(is<ConnectionOptions>({readConcernLevel: 'majority2'})).toBe(false); |
| 412 | + expect(is<ConnectionOptions>({ readConcernLevel: 'majority' })).toBe(true); |
| 413 | + expect(is<ConnectionOptions>({ readConcernLevel: 'majority2' })).toBe(false); |
| 414 | +}); |
| 415 | + |
| 416 | +test('union classes with generic', () => { |
| 417 | + class Group { |
| 418 | + id: number & PrimaryKey = 0; |
| 419 | + second: string = ''; |
| 420 | + } |
| 421 | + |
| 422 | + class User { |
| 423 | + id: number = 0; |
| 424 | + groups: (Group & Reference)[] = []; |
| 425 | + } |
| 426 | + |
| 427 | + const serializer = new Serializer(); |
| 428 | + |
| 429 | + const newGroup = cast<Group>({ id: 1, second: 'a' }); |
| 430 | + |
| 431 | + const a = cast<User>({ |
| 432 | + id: 1, |
| 433 | + groups: [newGroup], |
| 434 | + }, undefined, serializer); |
| 435 | + |
| 436 | + expect(a.groups[0]).toBeInstanceOf(Group); |
| 437 | + expect(isReferenceInstance(a.groups[0])).toBe(false); |
| 438 | + |
| 439 | + const b = cast<User>({ |
| 440 | + id: 1, |
| 441 | + groups: [1], |
| 442 | + }, undefined, serializer); |
| 443 | + |
| 444 | + expect(b.groups[0]).toBeInstanceOf(Group); |
| 445 | + expect(isReferenceInstance(b.groups[0])).toBe(true); |
| 446 | + if (isReferenceInstance(b.groups[0])) { |
| 447 | + //do something with this instance and fully load it |
| 448 | + } |
411 | 449 | });
|
0 commit comments