ArrayStorage.spec.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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('mergeWith', () => {
  180. describe('Base merge', () => {
  181. const secondStorageKey: number = 1;
  182. const secondStorageValue: string = 'bar';
  183. const expectedArray: string[] = [storageValue, secondStorageValue];
  184. let array: string[];
  185. before(() => {
  186. storage = getStorageInstance<string>();
  187. storage.set(storageKey, storageValue);
  188. const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
  189. secondStorage.set(secondStorageKey, secondStorageValue);
  190. storage.mergeWith(secondStorage, false);
  191. array = storage.getStorage();
  192. });
  193. it('should merge two storages', () => {
  194. assert.deepEqual(array, expectedArray);
  195. });
  196. });
  197. describe('Merge with storage id', () => {
  198. const secondStorageKey: number = 1;
  199. const secondStorageValue: string = 'bar';
  200. const expectedArray: string[] = [storageValue, secondStorageValue];
  201. let array: string[];
  202. let storageId: string;
  203. let expectedStorageId: string;
  204. before(() => {
  205. storage = getStorageInstance<string>();
  206. storage.set(storageKey, storageValue);
  207. const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
  208. expectedStorageId = secondStorage.getStorageId();
  209. secondStorage.set(secondStorageKey, secondStorageValue);
  210. storage.mergeWith(secondStorage, true);
  211. storageId = storage.getStorageId();
  212. array = storage.getStorage();
  213. });
  214. it('should update storage id', () => {
  215. assert.deepEqual(storageId, expectedStorageId);
  216. });
  217. it('should merge two storages', () => {
  218. assert.deepEqual(array, expectedArray);
  219. });
  220. });
  221. });
  222. });