MapStorage.spec.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  4. import { TDictionary } from '../../../src/types/TDictionary';
  5. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { IMapStorage } from '../../../src/interfaces/storages/IMapStorage';
  7. import { IOptions } from '../../../src/interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../../src/interfaces/utils/IRandomGenerator';
  9. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  10. import { MapStorage } from '../../../src/storages/MapStorage';
  11. class ConcreteStorage <V> extends MapStorage <string, V> {
  12. constructor () {
  13. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  14. inversifyContainerFacade.load('', '', {});
  15. super(
  16. inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator),
  17. inversifyContainerFacade.get<IOptions>(ServiceIdentifiers.IOptions)
  18. );
  19. }
  20. }
  21. /**
  22. * @returns {IMapStorage<string, V>}
  23. */
  24. const getStorageInstance = <V>(): IMapStorage <string, V> => {
  25. const storage: IMapStorage <string, V> = new ConcreteStorage <V> ();
  26. storage.initialize();
  27. return storage;
  28. };
  29. describe('MapStorage', () => {
  30. const storageKey: string = 'foo';
  31. const storageValue: string = 'bar';
  32. let storage: IMapStorage <string, any>;
  33. describe('initialize', () => {
  34. const expectedError: ErrorConstructor = Error;
  35. let testFunc: () => void;
  36. before(() => {
  37. storage = new ConcreteStorage();
  38. testFunc = () => storage.set(storageKey, storageValue);
  39. });
  40. it('should throws an error when storage isn\'t initialized', () => {
  41. assert.throws(testFunc, expectedError);
  42. });
  43. });
  44. describe('getStorage', () => {
  45. const expectedInstanceOf: MapConstructor = Map;
  46. let mapStorage: Map <string, string>;
  47. before(() => {
  48. storage = getStorageInstance<string>();
  49. mapStorage = storage.getStorage();
  50. });
  51. it('should return storage', () => {
  52. assert.instanceOf(mapStorage, expectedInstanceOf);
  53. });
  54. });
  55. describe('get', () => {
  56. describe('Variant #1: value exist', () => {
  57. const expectedValue: string = storageValue;
  58. let value: string;
  59. before(() => {
  60. storage = getStorageInstance<string>();
  61. storage.set(storageKey, storageValue);
  62. value = storage.get(storageKey);
  63. });
  64. it('should return value from storage by key', () => {
  65. assert.equal(value, expectedValue);
  66. });
  67. });
  68. describe('Variant #2: value isn\'t exist', () => {
  69. const expectedValue: undefined = undefined;
  70. let value: string;
  71. before(() => {
  72. storage = getStorageInstance<string>();
  73. value = storage.get(storageKey);
  74. });
  75. it('should return undefined', () => {
  76. assert.equal(value, expectedValue);
  77. });
  78. });
  79. });
  80. describe('getOrThrow', () => {
  81. describe('Variant #1: value exist', () => {
  82. const expectedValue: string = storageValue;
  83. let value: string;
  84. before(() => {
  85. storage = getStorageInstance<string>();
  86. storage.set(storageKey, storageValue);
  87. value = storage.getOrThrow(storageKey);
  88. });
  89. it('should return value from storage by key', () => {
  90. assert.equal(value, expectedValue);
  91. });
  92. });
  93. describe('Variant #2: value isn\'t exist', () => {
  94. const expectedError: ErrorConstructor = Error;
  95. let testFunc: () => void;
  96. before(() => {
  97. storage = getStorageInstance<string>();
  98. testFunc = () => storage.getOrThrow(storageKey);
  99. });
  100. it('should throw an error', () => {
  101. assert.throws(testFunc, expectedError);
  102. });
  103. });
  104. });
  105. describe('getLength', () => {
  106. const expectedStorageLength: number = 1;
  107. let storageLength: number;
  108. before(() => {
  109. storage = getStorageInstance<string>();
  110. storage.set(storageKey, storageValue);
  111. storageLength = storage.getLength();
  112. });
  113. it('should return length of storage', () => {
  114. assert.equal(storageLength, expectedStorageLength);
  115. });
  116. });
  117. describe('getKeyOf', () => {
  118. let key: string | number | null;
  119. describe('Variant #1', () => {
  120. before(() => {
  121. storage = getStorageInstance<string>();
  122. storage.set(storageKey, storageValue);
  123. key = storage.getKeyOf(storageValue);
  124. });
  125. it('should return key of string value', () => {
  126. assert.equal(key, storageKey);
  127. });
  128. });
  129. describe('Variant #2', () => {
  130. const object: Object = {
  131. bar: 'baz'
  132. };
  133. before(() => {
  134. storage = getStorageInstance<string>();
  135. storage.set(storageKey, object);
  136. key = storage.getKeyOf(object);
  137. });
  138. it('should return key of object if objects in `set` and `get` are two same objects', () => {
  139. assert.equal(key, storageKey);
  140. });
  141. });
  142. describe('Variant #3', () => {
  143. const expectedKey: null = null;
  144. const object: Object = {
  145. bar: 'baz'
  146. };
  147. before(() => {
  148. storage = getStorageInstance<string>();
  149. storage.set(storageKey, object);
  150. key = storage.getKeyOf({...object});
  151. });
  152. it('should return `null` if objects in `set` and `get` are two different objects', () => {
  153. assert.equal(key, expectedKey);
  154. });
  155. });
  156. });
  157. describe('getStorageAsDictionary', () => {
  158. const expectedDictionary: TDictionary<string> = {
  159. [storageKey]: storageValue
  160. };
  161. let storageAsDictionary: TDictionary<string>;
  162. before(() => {
  163. storage = getStorageInstance<string>();
  164. storage.set(storageKey, storageValue);
  165. storageAsDictionary = storage.getStorageAsDictionary();
  166. });
  167. it('should return storage as dictionary', () => {
  168. assert.deepEqual(storageAsDictionary, expectedDictionary);
  169. });
  170. });
  171. describe('has', () => {
  172. describe('Variant #1: item is presenting in storage', () => {
  173. const expectedItemExistence: boolean = true;
  174. let itemExistence: boolean;
  175. before(() => {
  176. storage = getStorageInstance<string>();
  177. storage.set(storageKey, storageValue);
  178. itemExistence = storage.has(storageKey);
  179. });
  180. it('should return `true` if item is presenting in storage', () => {
  181. assert.equal(itemExistence, expectedItemExistence);
  182. });
  183. });
  184. describe('Variant #2: item isn\'t presenting in storage', () => {
  185. const expectedItemExistence: boolean = false;
  186. let itemExistence: boolean;
  187. before(() => {
  188. storage = getStorageInstance<string>();
  189. itemExistence = storage.has(storageKey);
  190. });
  191. it('should return `false` if item isn\'t presenting in storage', () => {
  192. assert.equal(itemExistence, expectedItemExistence);
  193. });
  194. });
  195. });
  196. describe('set', () => {
  197. let value: string;
  198. before(() => {
  199. storage = getStorageInstance<string>();
  200. storage.set(storageKey, storageValue);
  201. value = storage.getOrThrow(storageKey);
  202. });
  203. it('should set value to the storage', () => {
  204. assert.equal(value, storageValue);
  205. });
  206. });
  207. describe('mergeWith', () => {
  208. const secondStorageKey: string = 'baz';
  209. const secondStorageValue: string = 'quux';
  210. const expectedArray: string[][] = [
  211. [storageKey, storageValue],
  212. [secondStorageKey, secondStorageValue]
  213. ];
  214. let array: string[][];
  215. before(() => {
  216. storage = getStorageInstance<string>();
  217. storage.set(storageKey, storageValue);
  218. const secondStorage: IMapStorage <string, string> = getStorageInstance<string>();
  219. secondStorage.set(secondStorageKey, secondStorageValue);
  220. storage.mergeWith(secondStorage, false);
  221. array = Array.from(storage.getStorage());
  222. });
  223. it('should merge two storages', () => {
  224. assert.deepEqual(array, expectedArray);
  225. });
  226. });
  227. });