ArrayStorage.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  4. import { IArrayStorage } from '../../../src/interfaces/storages/IArrayStorage';
  5. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { IOptions } from '../../../src/interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../../../src/interfaces/utils/IRandomGenerator';
  8. import { ArrayStorage } from '../../../src/storages/ArrayStorage';
  9. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  10. class ConcreteStorage <V> extends ArrayStorage <V> {
  11. constructor () {
  12. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  13. inversifyContainerFacade.load('', '', {});
  14. super(
  15. inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator),
  16. inversifyContainerFacade.get<IOptions>(ServiceIdentifiers.IOptions)
  17. );
  18. }
  19. }
  20. /**
  21. * @returns {IArrayStorage<V>}
  22. */
  23. const getStorageInstance = <V> (): IArrayStorage <V> => {
  24. const storage: IArrayStorage <V> = new ConcreteStorage <V> ();
  25. storage.initialize();
  26. return storage;
  27. };
  28. describe('ArrayStorage', () => {
  29. const storageKey: number = 0;
  30. const storageValue: string = 'foo';
  31. let storage: IArrayStorage <any>;
  32. describe('initialize', () => {
  33. const expectedError: ErrorConstructor = Error;
  34. let testFunc: () => void;
  35. before(() => {
  36. storage = new ConcreteStorage<string>();
  37. testFunc = () => storage.set(storageKey, storageValue);
  38. });
  39. it('should throws an error when storage isn\'t initialized', () => {
  40. assert.throws(testFunc, expectedError);
  41. });
  42. });
  43. describe('getStorage', () => {
  44. const expectedInstanceOf: ArrayConstructor = Array;
  45. let arrayStorage: string[];
  46. before(() => {
  47. storage = getStorageInstance<string>();
  48. arrayStorage = storage.getStorage();
  49. });
  50. it('should return storage', () => {
  51. assert.instanceOf(arrayStorage, expectedInstanceOf);
  52. });
  53. });
  54. describe('getStorageId', () => {
  55. const storageIdRegExp: RegExp = /^[a-zA-Z0-9]{6}$/;
  56. let storageId: string;
  57. before(() => {
  58. storage = getStorageInstance<string>();
  59. storage.set(storageKey, storageValue);
  60. storageId = storage.getStorageId();
  61. });
  62. it('should return storage id', () => {
  63. assert.match(storageId, storageIdRegExp);
  64. });
  65. });
  66. describe('get', () => {
  67. describe('Variant #1: value exist', () => {
  68. const expectedValue: string = storageValue;
  69. let value: string;
  70. before(() => {
  71. storage = getStorageInstance<string>();
  72. storage.set(storageKey, storageValue);
  73. value = storage.get(storageKey);
  74. });
  75. it('should return value from storage by key', () => {
  76. assert.equal(value, expectedValue);
  77. });
  78. });
  79. describe('Variant #2: value isn\'t exist', () => {
  80. const expectedValue: undefined = undefined;
  81. let value: string;
  82. before(() => {
  83. storage = getStorageInstance<string>();
  84. value = storage.get(storageKey);
  85. });
  86. it('should return undefined if value does not exist in the storage', () => {
  87. assert.equal(value, expectedValue);
  88. });
  89. });
  90. });
  91. describe('getOrThrow', () => {
  92. describe('Variant #1: value exist', () => {
  93. const expectedValue: string = storageValue;
  94. let value: string;
  95. before(() => {
  96. storage = getStorageInstance<string>();
  97. storage.set(storageKey, storageValue);
  98. value = storage.getOrThrow(storageKey);
  99. });
  100. it('should return value from storage by key', () => {
  101. assert.equal(value, expectedValue);
  102. });
  103. });
  104. describe('Variant #2: value isn\'t exist', () => {
  105. const expectedError: ErrorConstructor = Error;
  106. let testFunc: () => void;
  107. before(() => {
  108. storage = getStorageInstance<string>();
  109. testFunc = () => storage.getOrThrow(storageKey);
  110. });
  111. it('should throw an error', () => {
  112. assert.throws(testFunc, expectedError);
  113. });
  114. });
  115. });
  116. describe('getLength', () => {
  117. const expectedStorageLength: number = 1;
  118. let storageLength: number;
  119. before(() => {
  120. storage = getStorageInstance<string>();
  121. storage.set(storageKey, storageValue);
  122. storageLength = storage.getLength();
  123. });
  124. it('should return length of storage', () => {
  125. assert.equal(storageLength, expectedStorageLength);
  126. });
  127. });
  128. describe('getKeyOf', () => {
  129. let key: string | number | null;
  130. describe('Variant #1', () => {
  131. before(() => {
  132. storage = getStorageInstance<string>();
  133. storage.set(storageKey, storageValue);
  134. key = storage.getKeyOf(storageValue);
  135. });
  136. it('should return key of string value', () => {
  137. assert.equal(key, storageKey);
  138. });
  139. });
  140. describe('Variant #2', () => {
  141. const object: Object = {
  142. foo: 'bar'
  143. };
  144. before(() => {
  145. storage = getStorageInstance<string>();
  146. storage.set(storageKey, object);
  147. key = storage.getKeyOf(object);
  148. });
  149. it('should return key of object if objects in `set` and `get` are two same objects', () => {
  150. assert.equal(key, storageKey);
  151. });
  152. });
  153. describe('Variant #3', () => {
  154. const expectedKey: null = null;
  155. const object: Object = {
  156. foo: 'bar'
  157. };
  158. before(() => {
  159. storage = getStorageInstance<string>();
  160. storage.set(storageKey, object);
  161. key = storage.getKeyOf({...object});
  162. });
  163. it('should return `null` if objects in `set` and `get` are two different objects', () => {
  164. assert.equal(key, expectedKey);
  165. });
  166. });
  167. });
  168. describe('set', () => {
  169. let value: string;
  170. before(() => {
  171. storage = getStorageInstance<string>();
  172. storage.set(storageKey, storageValue);
  173. value = storage.get(storageKey);
  174. });
  175. it('should set value to the storage', () => {
  176. assert.equal(value, storageValue);
  177. });
  178. });
  179. describe('delete', () => {
  180. describe('Variant #1: value exist', () => {
  181. const expectedUpdatedStorage: string[] = [
  182. 'foo',
  183. 'baz'
  184. ];
  185. const expectedUpdatedStorageLength: number = 2;
  186. const expectedDeletedValue: string = 'bar';
  187. let updatedStorage: string[];
  188. let updatedStorageLength: number;
  189. let deletedValue: string;
  190. before(() => {
  191. storage = getStorageInstance<string>();
  192. storage.set(0, 'foo');
  193. storage.set(1, 'bar');
  194. storage.set(2, 'baz');
  195. deletedValue = storage.delete(1);
  196. updatedStorage = storage.getStorage();
  197. updatedStorageLength = storage.getLength();
  198. });
  199. it('should delete value from the storage by index', () => {
  200. assert.deepEqual(updatedStorage, expectedUpdatedStorage);
  201. });
  202. it('should update storage length', () => {
  203. assert.deepEqual(updatedStorageLength, expectedUpdatedStorageLength);
  204. });
  205. it('should return deleted value', () => {
  206. assert.equal(deletedValue, expectedDeletedValue);
  207. });
  208. });
  209. describe('Variant #2: value isn\'t exist', () => {
  210. const expectedUpdatedStorage: string[] = [
  211. 'foo',
  212. 'bar',
  213. 'baz'
  214. ];
  215. const expectedUpdatedStorageLength: number = 3;
  216. const expectedDeletedValue: undefined = undefined;
  217. let updatedStorage: string[];
  218. let updatedStorageLength: number;
  219. let deletedValue: string;
  220. before(() => {
  221. storage = getStorageInstance<string>();
  222. storage.set(0, 'foo');
  223. storage.set(1, 'bar');
  224. storage.set(2, 'baz');
  225. deletedValue = storage.delete(3);
  226. updatedStorage = storage.getStorage();
  227. updatedStorageLength = storage.getLength();
  228. });
  229. it('should keep storage the same', () => {
  230. assert.deepEqual(updatedStorage, expectedUpdatedStorage);
  231. });
  232. it('should keep storage length', () => {
  233. assert.deepEqual(updatedStorageLength, expectedUpdatedStorageLength);
  234. });
  235. it('should return undefined', () => {
  236. assert.equal(deletedValue, expectedDeletedValue);
  237. });
  238. });
  239. });
  240. describe('mergeWith', () => {
  241. describe('Base merge', () => {
  242. const secondStorageKey: number = 1;
  243. const secondStorageValue: string = 'bar';
  244. const expectedArray: string[] = [storageValue, secondStorageValue];
  245. let array: string[];
  246. before(() => {
  247. storage = getStorageInstance<string>();
  248. storage.set(storageKey, storageValue);
  249. const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
  250. secondStorage.set(secondStorageKey, secondStorageValue);
  251. storage.mergeWith(secondStorage, false);
  252. array = storage.getStorage();
  253. });
  254. it('should merge two storages', () => {
  255. assert.deepEqual(array, expectedArray);
  256. });
  257. });
  258. describe('Merge with storage id', () => {
  259. const secondStorageKey: number = 1;
  260. const secondStorageValue: string = 'bar';
  261. const expectedArray: string[] = [storageValue, secondStorageValue];
  262. let array: string[];
  263. let storageId: string;
  264. let expectedStorageId: string;
  265. before(() => {
  266. storage = getStorageInstance<string>();
  267. storage.set(storageKey, storageValue);
  268. const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
  269. expectedStorageId = secondStorage.getStorageId();
  270. secondStorage.set(secondStorageKey, secondStorageValue);
  271. storage.mergeWith(secondStorage, true);
  272. storageId = storage.getStorageId();
  273. array = storage.getStorage();
  274. });
  275. it('should update storage id', () => {
  276. assert.deepEqual(storageId, expectedStorageId);
  277. });
  278. it('should merge two storages', () => {
  279. assert.deepEqual(array, expectedArray);
  280. });
  281. });
  282. });
  283. });