MapStorage.spec.ts 8.3 KB

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