UNPKG

@tucmc/hazel

Version:
204 lines (203 loc) 8.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const globals_1 = require("@jest/globals"); const DMap_1 = require("./DMap"); const standardData = { first: { name: 'first', count: 1, sleep: false }, second: { name: 'first', count: 0, sleep: false }, third: { name: 'first', count: 2, sleep: true } }; const dataType1 = standardData; const dataType2 = [ { first: { name: 'first', count: 1, sleep: false } }, { second: { name: 'first', count: 0, sleep: false } }, { third: { name: 'first', count: 2, sleep: true } } ]; const dataType3 = [ ['first', { name: 'first', count: 1, sleep: false }], ['second', { name: 'first', count: 0, sleep: false }], ['third', { name: 'first', count: 2, sleep: true }] ]; const expectedKVIO = [ { k: 'first', v: { name: 'first', count: 1, sleep: false }, i: 0, o: { first: { name: 'first', count: 1, sleep: false } } }, { k: 'second', v: { name: 'first', count: 0, sleep: false }, i: 1, o: { second: { name: 'first', count: 0, sleep: false } } }, { k: 'third', v: { name: 'first', count: 2, sleep: true }, i: 2, o: { third: { name: 'first', count: 2, sleep: true } } } ]; (0, globals_1.describe)('DMap', () => { (0, globals_1.test)('constructor', () => { const dMapType1 = new DMap_1.DMap(dataType1); const dMapType2 = new DMap_1.DMap(dataType2); const dMapType3 = new DMap_1.DMap(dataType3); (0, globals_1.expect)(dMapType1.size()).toBe(3); (0, globals_1.expect)(dMapType2.size()).toBe(3); (0, globals_1.expect)(dMapType3.size()).toBe(3); (0, globals_1.expect)(dMapType1.getRecord()).toMatchObject(standardData); (0, globals_1.expect)(dMapType2.getRecord()).toMatchObject(standardData); (0, globals_1.expect)(dMapType3.getRecord()).toMatchObject(standardData); }); const testSubject = new DMap_1.DMap(standardData); (0, globals_1.test)('method::size', () => { (0, globals_1.expect)(testSubject.size()).toBe(3); }); (0, globals_1.test)('method::keys', () => { (0, globals_1.expect)(testSubject.keys()).toEqual(['first', 'second', 'third']); }); (0, globals_1.test)('method::values', () => { (0, globals_1.expect)(testSubject.values()).toMatchObject([ { name: 'first', count: 1, sleep: false }, { name: 'first', count: 0, sleep: false }, { name: 'first', count: 2, sleep: true } ]); }); (0, globals_1.test)('method::iterable', () => { (0, globals_1.expect)(testSubject.iterable()).toEqual([ ['first', { name: 'first', count: 1, sleep: false }], ['second', { name: 'first', count: 0, sleep: false }], ['third', { name: 'first', count: 2, sleep: true }] ]); }); (0, globals_1.test)('method::map', () => { const mapResult = testSubject.map((k, v, i, o) => { return { k, v, i, o }; }); (0, globals_1.expect)(mapResult).toMatchObject(expectedKVIO); }); (0, globals_1.test)('method::sort', () => { const sorted = testSubject.sort((a, b) => a[1].count - b[1].count); (0, globals_1.expect)(sorted.keys()).toStrictEqual(['second', 'first', 'third']); }); (0, globals_1.test)('method::iterateSync', () => { testSubject.iterateSync((k, v, i, o) => { const ref = expectedKVIO[i]; if (!ref) { throw 'error'; } (0, globals_1.expect)({ k, v, i, o }).toMatchObject(ref); }); }); (0, globals_1.test)('method::iterate', async () => { const KVIOList = []; const delay = (ms) => new Promise((res) => setTimeout(res, ms)); await testSubject.iterate(async (k, v, i, o) => { if (i === 1) { await delay(1000); } KVIOList.push({ k, v, i, o }); }); (0, globals_1.expect)(KVIOList).toEqual(expectedKVIO); }); (0, globals_1.test)('method::hasKey', () => { (0, globals_1.expect)(testSubject.hasKey('first')).toBe(true); (0, globals_1.expect)(testSubject.hasKey('firste')).toBe(false); }); (0, globals_1.test)('method::filter', () => { const filtered = testSubject.filter((k, v) => !v.sleep); (0, globals_1.expect)(filtered.getRecord()).toMatchObject({ first: { name: 'first', count: 1, sleep: false }, second: { name: 'first', count: 0, sleep: false } }); }); (0, globals_1.test)('method::groupBy', () => { const grouped = testSubject.groupBy((v) => (v.sleep ? 'yes' : 'no')); (0, globals_1.expect)(grouped.keys()).toEqual(globals_1.expect.arrayContaining(['yes', 'no'])); (0, globals_1.expect)(grouped.get('yes')).toMatchObject([ { third: { name: 'first', count: 2, sleep: true } } ]); (0, globals_1.expect)(grouped.get('no')).toMatchObject([ { first: { name: 'first', count: 1, sleep: false } }, { second: { name: 'first', count: 0, sleep: false } } ]); }); (0, globals_1.test)('method::findKeys', () => { const keys = testSubject.findKeys((v) => !v.sleep); (0, globals_1.expect)(keys).toEqual(globals_1.expect.arrayContaining(['first', 'second'])); const keys2 = testSubject.findKeys((v) => v.sleep); (0, globals_1.expect)(keys2).toEqual(globals_1.expect.arrayContaining(['third'])); }); (0, globals_1.test)('method::findValues', () => { const vals = testSubject.findValues((v) => !v.sleep); (0, globals_1.expect)(vals).toMatchObject([ { name: 'first', count: 1, sleep: false }, { name: 'first', count: 0, sleep: false } ]); const vals2 = testSubject.findValues((v) => v.sleep); (0, globals_1.expect)(vals2).toMatchObject([{ name: 'first', count: 2, sleep: true }]); }); (0, globals_1.test)('method::hasValue', () => { (0, globals_1.expect)(testSubject.hasValue({ name: 'first', count: 1, sleep: false })).toBe(true); (0, globals_1.expect)(testSubject.hasValue({ count: 1, name: 'first', sleep: false })).toBe(true); }); (0, globals_1.test)('method::keyMatch', () => { const matched = testSubject.keyMatch(['first', 'third']); (0, globals_1.expect)(matched).toEqual(globals_1.expect.arrayContaining(['first', 'third'])); }); (0, globals_1.test)('method::keyDiff', () => { const diff = testSubject.keyDiff(['first', 'third']); (0, globals_1.expect)(diff).toEqual(globals_1.expect.arrayContaining(['second'])); const diff2 = testSubject.keyDiff([]); (0, globals_1.expect)(diff2).toEqual(globals_1.expect.arrayContaining(['first', 'second', 'third'])); }); (0, globals_1.test)('method::get', () => { (0, globals_1.expect)(testSubject.get('first')).toMatchObject({ name: 'first', count: 1, sleep: false }); }); (0, globals_1.test)('method::set', () => { const mutableTestSubject = new DMap_1.DMap(standardData); const testSetData = { name: 'test-set', count: 3, sleep: false }; mutableTestSubject.set('newKey', testSetData); (0, globals_1.expect)(mutableTestSubject.keys()).toContain('newKey'); (0, globals_1.expect)(mutableTestSubject.size()).toBe(4); (0, globals_1.expect)(mutableTestSubject.get('newKey')).toMatchObject(testSetData); }); (0, globals_1.test)('method::insert', () => { const mutableTestSubject = new DMap_1.DMap(standardData); const testInsertData = { name: 'test-insert', count: 4, sleep: true }; const inserted_key = mutableTestSubject.insert(testInsertData); (0, globals_1.expect)(mutableTestSubject.size()).toBe(4); (0, globals_1.expect)(mutableTestSubject.get(inserted_key)).toMatchObject(testInsertData); }); (0, globals_1.test)('method::getRecord', () => { (0, globals_1.expect)(testSubject.getRecord()).toMatchObject(standardData); }); (0, globals_1.test)('method::isLive', () => { (0, globals_1.expect)(testSubject.isLive()).toBe(false); }); (0, globals_1.test)('method::remove', () => { const mutableTestSubject = new DMap_1.DMap(standardData); mutableTestSubject.remove('first'); (0, globals_1.expect)(mutableTestSubject.getRecord()).toMatchObject({ second: { name: 'first', count: 0, sleep: false }, third: { name: 'first', count: 2, sleep: true } }); mutableTestSubject.remove('second'); (0, globals_1.expect)(mutableTestSubject.getRecord()).toMatchObject({ third: { name: 'first', count: 2, sleep: true } }); }); });