UNPKG

domain-objects

Version:

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

109 lines 5.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const uuid_1 = require("uuid"); const DomainObject_1 = require("../instantiation/DomainObject"); const assertDomainObjectIsSafeToManipulate_1 = require("./assertDomainObjectIsSafeToManipulate"); describe('assertDomainObjectIsSafeToManipulate', () => { it('should not throw an error if DomainObject has no nested objects', () => { class PlantPot extends DomainObject_1.DomainObject { } // instantiate it const pot = new PlantPot({ diameterInInches: 7 }); // check if its safe (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(pot); // should be safe }); it('should not throw an error if DomainObject has a nested Date object', () => { class PlantPot extends DomainObject_1.DomainObject { } // instantiate it const pot = new PlantPot({ createdAt: new Date(), diameterInInches: 7 }); // check if its safe (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(pot); // should be safe }); it('should not throw an error if all nested objects on DomainObject are explicitly defined as nested', () => { class PlantPot extends DomainObject_1.DomainObject { } class Plant extends DomainObject_1.DomainObject { } Plant.nested = { pot: PlantPot }; // now show we still think its safe const plant = new Plant({ pot: { diameterInInches: 7 }, lastWatered: 'monday', }); (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(plant); }); it('should not throw an error if DomainObject has an array of strings', () => { class PlantPot extends DomainObject_1.DomainObject { } // instantiate it const pot = new PlantPot({ diameterInInches: 7, pastPlantUuids: [(0, uuid_1.v4)(), (0, uuid_1.v4)()], }); // check if its safe (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(pot); // should be safe }); it('should not throw an error if DomainObject has an array of DomainObjects explicitly defined as nested', () => { class PlantPot extends DomainObject_1.DomainObject { } class Plant extends DomainObject_1.DomainObject { } Plant.nested = { pastPots: PlantPot }; // now show we still think its safe const plant = new Plant({ pastPots: [{ diameterInInches: 7 }], lastWatered: 'monday', }); (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(plant); }); it('should throw a helpful error if any of the properties are a nested object not explicitly defined', () => { class PlantPot extends DomainObject_1.DomainObject { } class Plant extends DomainObject_1.DomainObject { } // now show we do not think its safe const plant = new Plant({ pot: { diameterInInches: 7 }, lastWatered: 'monday', }); try { (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(plant); throw new Error('should not reach here'); } catch (error) { if (!(error instanceof Error)) throw error; expect(error.message).toContain(`DomainObject 'Plant' is not safe to manipulate.`); expect(error.message).toContain(`["pot"]`); // should identify the culprits expect(error.message).toContain(`Please make sure all nested objects are DomainObjects and are explicitly defined on the class definition, using the 'nested' static property.`); // should explain how to fix expect(error.message).toContain('For example'); // should include an example expect(error.message).toMatchSnapshot(); // should look nice - visually } }); it('should throw a helpful error if any of the properties are an array with nested objects not explicitly defined', () => { class PlantPot extends DomainObject_1.DomainObject { } class Plant extends DomainObject_1.DomainObject { } // now show we do not think its safe const plant = new Plant({ pastPots: [{ diameterInInches: 7 }], lastWatered: 'monday', }); try { (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(plant); throw new Error('should not reach here'); } catch (error) { if (!(error instanceof Error)) throw error; expect(error.message).toContain(`DomainObject 'Plant' is not safe to manipulate.`); expect(error.message).toContain(`["pastPots"]`); // should identify the culprits expect(error.message).toContain(`Please make sure all nested objects are DomainObjects and are explicitly defined on the class definition, using the 'nested' static property.`); // should explain how to fix expect(error.message).toContain('For example'); // should include an example expect(error.message).toMatchSnapshot(); // should look nice - visually } }); }); //# sourceMappingURL=assertDomainObjectIsSafeToManipulate.test.js.map