domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
44 lines • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const DomainEntity_1 = require("../instantiation/DomainEntity");
const isPrimaryKeyRef_1 = require("./isPrimaryKeyRef");
describe('isPrimaryKeyRef', () => {
class SeaTurtle extends DomainEntity_1.DomainEntity {
}
SeaTurtle.primary = ['uuid'];
SeaTurtle.unique = ['seawaterSecurityNumber'];
describe('assess', () => {
it('should correctly identify that an object with the same shape as the primary key is a primary key ref', () => {
const ref = { uuid: 'uuid' };
const decision = (0, isPrimaryKeyRef_1.isPrimaryKeyRef)({ of: SeaTurtle })(ref);
expect(decision).toEqual(true);
});
it('should correctly identify that an object with the missing keys from the primary key is not a primary key ref', () => {
const ref = { seawaterSecurityNumber: '821' };
const decision = (0, isPrimaryKeyRef_1.isPrimaryKeyRef)({ of: SeaTurtle })(ref);
expect(decision).toEqual(false);
});
});
describe('guard', () => {
it('should narrow types ', () => {
const ref = { uuid: 'uuid' };
// if within the type guard, true
if ((0, isPrimaryKeyRef_1.isPrimaryKeyRef)({ of: SeaTurtle })(ref)) {
// should be able to access the uuid
const uuid = ref.uuid;
// should be able to assign to the primary key shape
const pk = ref;
}
// if within the type guard, false
if (!(0, isPrimaryKeyRef_1.isPrimaryKeyRef)({ of: SeaTurtle })(ref)) {
// @ts-expect-error: Property 'uuid' does not exist on type 'DomainUniqueKeyShape<typeof SeaTurtle>'.ts(2339)
const uuid = ref.uuid;
// @ts-expect-error: Property 'uuid' is missing in type 'DomainUniqueKeyShape<typeof SeaTurtle>' but required in type 'Required<Pick<SeaTurtle, "uuid">>'.ts(2741)
const pk = ref;
// should be able to assign to the unique key shape, since we know that it is one or the other as input
const uk = ref;
}
});
});
});
//# sourceMappingURL=isPrimaryKeyRef.test.js.map