UNPKG

domain-objects

Version:

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

62 lines 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const uuid_1 = require("uuid"); const assertDomainObjectIsSafeToManipulate_1 = require("../constraints/assertDomainObjectIsSafeToManipulate"); const DomainEntity_1 = require("../instantiation/DomainEntity"); const DomainEntityUpdatablePropertiesMustBeDefinedError_1 = require("./DomainEntityUpdatablePropertiesMustBeDefinedError"); const getUpdatableProperties_1 = require("./getUpdatableProperties"); describe('getUpdatableProperties', () => { describe('entity', () => { it('should be able to get updatable properties accurately', () => { class RocketShip extends DomainEntity_1.DomainEntity { } RocketShip.updatable = ['fuelQuantity', 'passengers']; const ship = new RocketShip({ serialNumber: 'SN5', fuelQuantity: 9001, passengers: 21, }); const updatableProps = (0, getUpdatableProperties_1.getUpdatableProperties)(ship); expect(updatableProps).toEqual({ fuelQuantity: 9001, passengers: 21 }); }); it('should throw an error if .updatable is not defined on the entity', () => { class RocketShip extends DomainEntity_1.DomainEntity { } const ship = new RocketShip({ serialNumber: 'SN5', fuelQuantity: 9001, passengers: 21, }); try { (0, getUpdatableProperties_1.getUpdatableProperties)(ship); throw new Error('should not reach here'); } catch (error) { if (!(error instanceof Error)) throw error; expect(error.message).toContain('`RocketShip.updatable` must be defined, to be able to `getUpdatableProperties`'); expect(error).toBeInstanceOf(DomainEntityUpdatablePropertiesMustBeDefinedError_1.DomainEntityUpdatablePropertiesMustBeDefinedError); } }); }); describe('safety', () => { it('should throw an error if domain object is not safe to manipulate', () => { class Plane extends DomainEntity_1.DomainEntity { } Plane.unique = ['uuid']; Plane.updatable = ['passengerLimit']; const phone = new Plane({ uuid: (0, uuid_1.v4)(), manufacturer: { name: 'boeing' }, passengerLimit: 821, }); try { (0, getUpdatableProperties_1.getUpdatableProperties)(phone); } catch (error) { expect(error).toBeInstanceOf(assertDomainObjectIsSafeToManipulate_1.DomainObjectNotSafeToManipulateError); } }); }); }); //# sourceMappingURL=getUpdatableProperties.test.js.map