diff --git a/src/instantiation/Ref.test.ts b/src/instantiation/Ref.test.ts index 9dfcdde..1fecdec 100644 --- a/src/instantiation/Ref.test.ts +++ b/src/instantiation/Ref.test.ts @@ -1,3 +1,4 @@ +import { omitReadonly } from '../manipulation/omitReadonly'; import { isRefByPrimary } from '../reference/isRefByPrimary'; import { isRefByUnique } from '../reference/isRefByUnique'; import { DomainEntity } from './DomainEntity'; @@ -268,5 +269,19 @@ describe('Ref', () => { expect(ref.turtle).not.toHaveProperty('uuid'); }); }); + + it('should not omit anything when omitReadonly is called, since refs have no metadata', () => { + const ref = new Ref({ + seawaterSecurityNumber: '821', + }); + + const refWithoutReadonly = omitReadonly(ref); + + // all properties should be preserved since Ref has metadata = [] + if (!isRefByUnique({ of: SeaTurtle })(refWithoutReadonly)) + throw new Error('expected ref to be by unique'); + expect(refWithoutReadonly.seawaterSecurityNumber).toBe('821'); + expect(refWithoutReadonly).toBeInstanceOf(Ref); + }); }); }); diff --git a/src/instantiation/Ref.ts b/src/instantiation/Ref.ts index eadd6b5..a68541a 100644 --- a/src/instantiation/Ref.ts +++ b/src/instantiation/Ref.ts @@ -34,6 +34,8 @@ export type Ref< // extend the domain literal into a custom class, so that we can rename it into Ref, without mutating the global DomainLiteral class class RefBase extends DomainLiteral { + public static metadata = [] as const; + constructor(props: T) { // if props itself is a domain entity or domain event, extract its reference (using unique keys as the default) if (isOfDomainEntity(props) || isOfDomainEvent(props)) { diff --git a/src/instantiation/RefByPrimary.test.ts b/src/instantiation/RefByPrimary.test.ts index bde2334..a5f0e1c 100644 --- a/src/instantiation/RefByPrimary.test.ts +++ b/src/instantiation/RefByPrimary.test.ts @@ -1,5 +1,6 @@ import type { HasMetadata, OmitMetadata } from 'type-fns'; +import { omitReadonly } from '../manipulation/omitReadonly'; import { DomainEntity } from './DomainEntity'; import { DomainLiteral } from './DomainLiteral'; import { RefByPrimary } from './RefByPrimary'; @@ -273,5 +274,19 @@ describe('DomainRefByPrimary', () => { expect(home.turtle).not.toHaveProperty('seawaterSecurityNumber'); }); }); + + it('should not omit anything when omitReadonly is called, since refs have no metadata', () => { + const ref = new RefByPrimary({ + uuid: '123e4567-e89b-12d3-a456-426614174000', + }); + + const refWithoutReadonly = omitReadonly(ref); + + // all properties should be preserved since RefByPrimary has metadata = [] + expect(refWithoutReadonly.uuid).toBe( + '123e4567-e89b-12d3-a456-426614174000', + ); + expect(refWithoutReadonly).toBeInstanceOf(RefByPrimary); + }); }); }); diff --git a/src/instantiation/RefByPrimary.ts b/src/instantiation/RefByPrimary.ts index 4f095a2..572f148 100644 --- a/src/instantiation/RefByPrimary.ts +++ b/src/instantiation/RefByPrimary.ts @@ -33,6 +33,8 @@ export type RefByPrimary< // extend the domain literal into a custom class, so that we can rename it into RefByUnique, without mutating the global DomainLiteral class class RefByPrimaryBase extends DomainLiteral { + public static metadata = [] as const; + constructor(props: T) { // if props itself is a domain entity or domain event, extract its reference if ( diff --git a/src/instantiation/RefByUnique.test.ts b/src/instantiation/RefByUnique.test.ts index 0ebc992..6de912d 100644 --- a/src/instantiation/RefByUnique.test.ts +++ b/src/instantiation/RefByUnique.test.ts @@ -1,3 +1,4 @@ +import { omitReadonly } from '../manipulation/omitReadonly'; import { DomainEntity } from './DomainEntity'; import { DomainLiteral } from './DomainLiteral'; import { Ref } from './Ref'; @@ -229,5 +230,17 @@ describe('DomainRefByUnique', () => { ); }); }); + + it('should not omit anything when omitReadonly is called, since refs have no metadata', () => { + const ref = new RefByUnique({ + seawaterSecurityNumber: '821', + }); + + const refWithoutReadonly = omitReadonly(ref); + + // all properties should be preserved since RefByUnique has metadata = [] + expect(refWithoutReadonly.seawaterSecurityNumber).toBe('821'); + expect(refWithoutReadonly).toBeInstanceOf(RefByUnique); + }); }); }); diff --git a/src/instantiation/RefByUnique.ts b/src/instantiation/RefByUnique.ts index b139fb7..8a8d364 100644 --- a/src/instantiation/RefByUnique.ts +++ b/src/instantiation/RefByUnique.ts @@ -34,6 +34,8 @@ export type RefByUnique< // extend the domain literal into a custom class, so that we can rename it into RefByUnique, without mutating the global DomainLiteral class class RefByUniqueBase extends DomainLiteral { + public static metadata = [] as const; + constructor(props: T) { // if props itself is a domain entity or domain event, extract its reference if (isOfDomainEntity(props) || isOfDomainEvent(props)) {