UNPKG

@api3/contracts

Version:

Contracts through which API3 services are delivered

52 lines 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const arrays_1 = require("./arrays"); describe(arrays_1.hasUniqueEntries.name, () => { it('returns true for an empty array', () => { const items = []; expect((0, arrays_1.hasUniqueEntries)(items, 'id')).toBe(true); }); it('returns false for duplicates based on specified field', () => { const items = [ { id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }, ]; expect((0, arrays_1.hasUniqueEntries)(items, 'id')).toBe(false); }); it('returns true if no duplicates based on specified field', () => { const items = [ { id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 3, name: 'C' }, ]; expect((0, arrays_1.hasUniqueEntries)(items, 'id')).toBe(true); }); it('returns true for an array with a single element', () => { const items = [{ id: 1, name: 'A' }]; expect((0, arrays_1.hasUniqueEntries)(items, 'id')).toBe(true); }); it('throws an error for a non-existing field', () => { const items = [ { id: 1, name: 'A' }, { id: 2, name: 'B' }, ]; expect(() => (0, arrays_1.hasUniqueEntries)(items, 'nonExistingField')).toThrow('unknown field:nonExistingField on array item'); }); it('returns false for duplicates in non-primary field', () => { const items = [ { id: 1, name: 'A' }, { id: 2, name: 'A' }, ]; expect((0, arrays_1.hasUniqueEntries)(items, 'name')).toBe(false); }); it('throws an error given an array of primitives', () => { const items = [1, 2, 3, 1]; expect(() => (0, arrays_1.hasUniqueEntries)(items, 'id')).toThrow('unknown field:id on array item'); }); it('throws an error given an array of different object structures', () => { const items = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }, 3]; expect(() => (0, arrays_1.hasUniqueEntries)(items, 'id')).toThrow('unknown field:id on array item'); }); }); //# sourceMappingURL=arrays.test.js.map