UNPKG

domain-objects

Version:

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

87 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const test_fns_1 = require("test-fns"); const DomainEntity_1 = require("../../instantiation/DomainEntity"); const DomainLiteral_1 = require("../../instantiation/DomainLiteral"); const dedupe_1 = require("./dedupe"); describe('dedupe', () => { class SeaTurtle extends DomainEntity_1.DomainEntity { } SeaTurtle.unique = ['saltwaterSecurityNumber']; class Region extends DomainLiteral_1.DomainLiteral { } // some entities with a dupe const turtleOne = new SeaTurtle({ saltwaterSecurityNumber: '821', name: 'Shelly C.', }); const turtleTwo = new SeaTurtle({ saltwaterSecurityNumber: '921', name: 'Shellbert', }); const turtleOneWithId = new SeaTurtle(Object.assign(Object.assign({}, turtleOne), { id: 821 })); // some literals with a dupe const regionOne = new Region({ name: 'Great Barrier Reef', }); const regionTwo = new Region({ name: 'Puerto Rico Trench', }); const regionThree = new Region({ name: 'East Pacific', }); it('should keep only the one instance per distinct identity', () => { const deduped = (0, dedupe_1.dedupe)([ // entities turtleOne, turtleTwo, turtleTwo, // exact dupe turtleOneWithId, // identity dupe turtleTwo, // exact dupe regionOne, regionThree, regionTwo, regionTwo, regionOne, ]); expect(deduped).toHaveLength(5); }); it('should keep only the first instance per distinct identity', () => { const deduped = (0, dedupe_1.dedupe)([ turtleOne, turtleOneWithId, // identity dupe turtleOne, // exact dupe turtleOneWithId, // identity dupe ]); expect(deduped).toHaveLength(1); expect(deduped[0].id).toEqual(undefined); }); it('should fail fast with a helpful error if there are multiple versions of the same entity, by default', () => { const turtleOneWithDiffName = new SeaTurtle(Object.assign(Object.assign({}, turtleOne), { name: 'Shellina C.' })); const error = (0, test_fns_1.getError)(() => (0, dedupe_1.dedupe)([turtleOne, turtleOneWithDiffName])); expect(error.message).toContain('Two different versions of the same entity were asked to be deduped.'); expect(error.message).toMatchSnapshot(); }); it('should choose the first version if are multiple versions of the same entity and the caller specified this mode', () => { const turtleOneWithDiffName = new SeaTurtle(Object.assign(Object.assign({}, turtleOne), { name: 'Shellina C.' })); const deduped = (0, dedupe_1.dedupe)([ turtleOne, turtleOneWithDiffName, // identity dupe turtleOne, // exact dupe turtleOneWithDiffName, // identity dupe ], { onMultipleEntityVersions: 'CHOOSE_FIRST_OCCURRENCE' }); expect(deduped).toHaveLength(1); expect(deduped[0].name).toEqual('Shelly C.'); }); it('should work on literals', () => { const namesUnique = (0, dedupe_1.dedupe)([ 'aurora', 'shelbert', 'donahue', 'aurora', 'shelbert', ]); expect(namesUnique).toEqual(['aurora', 'shelbert', 'donahue']); }); }); //# sourceMappingURL=dedupe.test.js.map