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