UNPKG

domain-objects

Version:

A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.

95 lines 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const DomainEntity_1 = require("../instantiation/DomainEntity"); const DomainLiteral_1 = require("../instantiation/DomainLiteral"); const DomainEntityPrimaryKeysMustBeDefinedError_1 = require("./DomainEntityPrimaryKeysMustBeDefinedError"); const getPrimaryIdentifier_1 = require("./getPrimaryIdentifier"); describe('getPrimaryIdentifier', () => { describe('literal', () => { it('should be able to get primary identifier accurately', () => { class Address extends DomainLiteral_1.DomainLiteral { } Address.primary = ['uuid']; const address = new Address({ uuid: '__uuid__', street: '123 Elm Street', city: 'Austin', state: 'TX', postal: '78704', }); const primary = (0, getPrimaryIdentifier_1.getPrimaryIdentifier)(address); expect(primary).toEqual({ uuid: '__uuid__', }); }); it('should return empty object, if primary keys are not defined on the instance', () => { class Address extends DomainLiteral_1.DomainLiteral { } Address.primary = ['uuid']; const address = new Address({ street: '123 Elm Street', city: 'Austin', state: 'TX', postal: '78704', }); const primary = (0, getPrimaryIdentifier_1.getPrimaryIdentifier)(address); expect(primary).toEqual(undefined); }); it('should throw an error if .primary is not defined on the literal', () => { class Address extends DomainLiteral_1.DomainLiteral { } const address = new Address({ uuid: '__uuid__', street: '123 Elm Street', city: 'Austin', state: 'TX', postal: '78704', }); try { (0, getPrimaryIdentifier_1.getPrimaryIdentifier)(address); throw new Error('should not reach here'); } catch (error) { if (!(error instanceof Error)) throw error; expect(error.message).toContain('`Address.primary` must be defined, to be able to `getPrimaryIdentifier`'); expect(error).toBeInstanceOf(DomainEntityPrimaryKeysMustBeDefinedError_1.DomainEntityPrimaryKeysMustBeDefinedError); } }); }); describe('entity', () => { it('should be able to get unique identifier accurately', () => { class ShmoogleAdsCampaign extends DomainEntity_1.DomainEntity { } ShmoogleAdsCampaign.primary = ['resourceName']; ShmoogleAdsCampaign.unique = ['name']; const campaign = new ShmoogleAdsCampaign({ resourceName: '__resource_name__', name: 'best campaign ever', }); const primary = (0, getPrimaryIdentifier_1.getPrimaryIdentifier)(campaign); expect(primary).toEqual({ resourceName: '__resource_name__', }); }); it('should throw an error if .primary is not defined on the entity', () => { class ShmoogleAdsCampaign extends DomainEntity_1.DomainEntity { } const campaign = new ShmoogleAdsCampaign({ resourceName: '__resource_name__', name: 'best campaign ever', }); try { (0, getPrimaryIdentifier_1.getPrimaryIdentifier)(campaign); throw new Error('should not reach here'); } catch (error) { if (!(error instanceof Error)) throw error; expect(error.message).toContain('`ShmoogleAdsCampaign.primary` must be defined, to be able to `getPrimaryIdentifier`'); expect(error).toBeInstanceOf(DomainEntityPrimaryKeysMustBeDefinedError_1.DomainEntityPrimaryKeysMustBeDefinedError); } }); }); }); //# sourceMappingURL=getPrimaryIdentifier.test.js.map