UNPKG

@code-o-mat/history-cache

Version:

A data object structure that logs all changes using the immutable library

366 lines (307 loc) 11.5 kB
import Bunyan from 'bunyan'; const log = Bunyan.createLogger({name: "CoreMethodOMatTest"}); import 'mocha'; import { expect } from 'chai'; import { Map, List } from 'immutable'; import { createMap } from 'immutable-map-symbol-preprocessor'; import { NONE, PROPS } from '@code-o-mat/globals/lib/constants/values'; import { CODE_O_MAT, CACHE, HISTORY_CACHE } from '@code-o-mat/globals/lib/constants/types'; const codePath = process.env.NODE_ENV === "PRODUCTION" ? 'lib' : 'src'; const METHODS = require(`../${codePath}/constants/methods`); const { APPEND_TO_HISTORY, FIND_INDEX, TRIM, GET_LOG_ITEMS, GET_LOG_ITEM_TEXT, PROCESS_LOG_ITEM, } = METHODS; const HistoryCache = require(`../${codePath}/history-cache`).default; const LOG = require(`../${codePath}/constants/types`).LOG; describe(`All tests for the History Cache`, () => { var historyCache; var FEELING = Symbol('FEELING'); var defaultData = { name: 'Robert LaMarca', age: 103, birthplace: 'Croatia', voice: 'Flat', eyes: 'sensitive', [FEELING]: 'groovy' }; var mergeData = { eyes: 'teal', height: 'short' } var mergedData = { ...defaultData, ...mergeData } it(`should have the appropriate meta for class HistoryCache`, () => { expect(HistoryCache.APP).to.equal(CODE_O_MAT); expect(HistoryCache.FAMILY).to.equal(CACHE); expect(HistoryCache.TYPE).to.equal(HISTORY_CACHE); } ); it(`should be able to instantiate a HistoryCache`, () => { historyCache = new HistoryCache(defaultData); expect(historyCache.APP).to.equal(CODE_O_MAT); expect(historyCache.FAMILY).to.equal(CACHE); expect(historyCache.TYPE).to.equal(HISTORY_CACHE); expect(typeof historyCache[PROPS].data).to.equal('object'); expect(List.isList(historyCache[PROPS].history)).to.equal(true); } ); it(`should be able to get the values in the current data set`, () => { const name = historyCache.get('name'); expect(name).to.equal('Robert LaMarca'); const age = historyCache.get('age'); expect(age).to.equal(103); const birthplace = historyCache.get('birthplace'); expect(birthplace).to.equal('Croatia'); const voice = historyCache.get('voice'); expect(voice).to.equal('Flat'); const eyes = historyCache.get('eyes'); expect(eyes).to.equal('sensitive'); const feeling = historyCache.get(FEELING); expect(feeling).to.equal('groovy'); } ); it(`should be able to use a default with get `, () => { const noName = historyCache.get('boo', 1066); expect(noName).to.equal(1066); } ); it(`should be able to get a value with getFrom using the current version`, () => { const name = historyCache.getFrom('name'); expect(name).to.equal('Robert LaMarca'); } ); it(`the index should be 1`, () => { expect(historyCache.index()).to.equal(1); } ); it(`the limit should be NONE`, () => { expect(historyCache.limit()).to.equal(NONE); } ); it(`should be able to set the limit to an integer`, () => { const stillHistoryCache = historyCache.limit(20); expect(stillHistoryCache.TYPE).to.equal(HISTORY_CACHE); expect(stillHistoryCache.uid()).to.equal(historyCache.uid()); expect(historyCache.limit()).to.equal(20); } ); it(`should be able to set no limit`, () => { historyCache.limit(-1); expect(historyCache.limit()).to.equal(NONE); } ); it(`should be not allow an improper limit setting`, () => { const limitB = () => historyCache.limit(true); const limitS = () => historyCache.limit('HI'); const limitF = () => historyCache.limit(3.2); const limitO = () => historyCache.limit({}); const limitFn = () => historyCache.limit(()=>true); expect(limitB).to.throw(Error); expect(limitS).to.throw(Error); expect(limitF).to.throw(Error); expect(limitO).to.throw(Error); expect(limitFn).to.throw(Error); } ); it(`should be able to set a new value for name. The value should set properly and it should also cause a new entry to be make to the _history structures. It should also show the index to be 2`, () => { historyCache.set('name', 'Edith Bunker'); expect(historyCache.get('name')).to.equal('Edith Bunker'); expect(historyCache.index()).to.equal(2); } ); it(`should be able to get a log API object`, () => { const log = historyCache.log(); expect(log.TYPE).to.equal(LOG); expect(typeof log.$).to.equal('function'); expect(typeof log.limit).to.equal('function'); expect(typeof log.log).to.equal('function'); expect(typeof log.json).to.equal('function'); expect(typeof log.text).to.equal('function'); expect(typeof log[GET_LOG_ITEMS]).to.equal('function'); expect(typeof log[GET_LOG_ITEM_TEXT]).to.equal('function'); expect(typeof log[PROCESS_LOG_ITEM]).to.equal('function'); } ); it(`should be able to get a log object`, () => { const logObject = historyCache.log().log(); const sameLogObject = historyCache.log().$(); const logAsArray = logObject.toJS(); const alsoLogAsArray = historyCache.log().js(); expect(List.isList(logObject)).to.equal(true); expect(List.isList(sameLogObject)).to.equal(true); expect(Array.isArray(logAsArray)).to.equal(true); expect(Array.isArray(alsoLogAsArray)).to.equal(true); } ); it(`should be able to get a json log object`, () => { const jsonLog = historyCache.log().json(); const jsLog = historyCache.log().js(); expect(jsonLog.UID).to.equal(jsLog.UID); expect(jsonLog.comment).to.equal(jsLog.comment); expect(jsonLog.event).to.equal(jsLog.event); } ); it(`should be able to get a text log object`, () => { const logText = historyCache.log().text(); expect(typeof logText).to.equal('string'); } ); // log count it(`Set some more values and see if the length of the log matches.`, () => { historyCache.set('voice', 'Sonorous') .set('eyes', 'cold') .set('mien', 'magnificent'); const log = historyCache.log().$(); const jsLog = historyCache.log().js(); expect(log.size).to.equal(5); expect(jsLog.length).to.equal(5); } ); it(`should be able to get a log with a limited count`, () => { const log = historyCache.log().log(2); const fullLog = historyCache.log().log(); const jsLog = historyCache.log().js(2); const jsonLog = historyCache.log().json(2); expect(fullLog.size).to.equal(5); expect(log.size).to.equal(2); expect(fullLog.get(4)).to.deep.equal(log.get(1)); expect(jsLog.length).to.equal(2); expect(JSON.parse(jsonLog).length).to.equal(2); } ); // Tag feature it(`should be able to set a tag to the current version. The size of the history should remain the same.`, () => { const stillHistoryCache = historyCache.tag('A tag'); expect(stillHistoryCache.uid()).to.equal(historyCache.uid()); expect(historyCache[PROPS].history.size).to.equal(5); expect(historyCache.index()).to.equal(5); expect(historyCache[PROPS].tagIndex.get('A tag')).to.equal(5); log.info(historyCache.log().text()) } ); // FIND features it(`should be able to get an index using private method FIND_INDEX`, () => { const index = historyCache[FIND_INDEX](0); expect(index).to.equal(0); expect(historyCache[FIND_INDEX](1)).to.equal(1); expect(historyCache[FIND_INDEX](3)).to.equal(3); } ); it(`should be able to get an version from an index`, () => { const historyItem = historyCache.version(2); expect(typeof historyItem).to.equal('object'); expect(Map.isMap(historyItem.data)).to.equal(true); expect(historyItem.data.get('voice')).to.equal('Sonorous'); } ); it(`should be able to get a version from a UID`, () => { const baseHistoryItem = historyCache.version(2); const historyItem = historyCache.version(baseHistoryItem.UID); expect(typeof historyItem).to.equal('object'); expect(Map.isMap(historyItem.data)).to.equal(true); expect(historyItem.UID).to.equal(baseHistoryItem.UID); expect(historyItem.data.get('voice')).to.equal('Sonorous'); } ); it(`should be able to get a version from an event`, () => { const baseHistoryItem = historyCache.version(2); const historyItem = historyCache.version('set voice = Sonorous'); expect(typeof historyItem).to.equal('object'); expect(Map.isMap(historyItem.data)).to.equal(true); expect(historyItem.UID).to.equal(baseHistoryItem.UID); expect(historyItem.data.get('voice')).to.equal('Sonorous'); } ); it(`should be able to check out a version from an index. The historyCache should add the checkout to the history.`, () => { historyCache.checkout(2); expect(historyCache.index()).to.equal(6); expect(historyCache.get('voice')).to.equal('Sonorous'); } ); it(`should be able to checkout a version from a UID`, () => { historyCache.checkout(5); const UID = historyCache.version(2).UID historyCache.checkout(UID); expect(historyCache.index()).to.equal(8); expect(historyCache.get('voice')).to.equal('Sonorous'); } ); it(`should be able to checkout a version from an event`, () => { historyCache.checkout(5); const UID = historyCache.version(2).UID historyCache.checkout('set voice = Sonorous'); expect(historyCache.index()).to.equal(10); expect(historyCache.get('voice')).to.equal('Sonorous'); } ); it(`should be able to get a value with getFrom using the a particular version`, () => { const voice2 = historyCache.getFrom('voice', 2); expect(voice2).to.equal('Sonorous'); expect(voice2).to.not.equal(historyCache.getFrom('voice', 0)); } ); it(`should be able to merge new data with a JavaScript Object`, () => { historyCache = new HistoryCache(defaultData); const stillHistoryCache = historyCache.merge(mergeData); expect(stillHistoryCache.uid()).to.equal(historyCache.uid()); expect(historyCache.data()).to.deep.equal(mergedData); } ); it(`should be able to merge new data with a Map`, () => { historyCache = new HistoryCache(defaultData); const mergeMap = createMap(mergeData); const stillHistoryCache = historyCache.merge(mergeMap); expect(stillHistoryCache.uid()).to.equal(historyCache.uid()); expect(historyCache.data()).to.deep.equal(mergedData); } ); } );