MapStorage.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 expectedError: ErrorConstructor = Error;
  69. let testFunc: () => void;
  70. before(() => {
  71. storage = getStorageInstance<string>();
  72. testFunc = () => storage.get(storageKey);
  73. });
  74. it('should throw an error', () => {
  75. assert.throws(testFunc, expectedError);
  76. });
  77. });
  78. });
  79. describe('getLength', () => {
  80. const expectedStorageLength: number = 1;
  81. let storageLength: number;
  82. before(() => {
  83. storage = getStorageInstance<string>();
  84. storage.set(storageKey, storageValue);
  85. storageLength = storage.getLength();
  86. });
  87. it('should return length of storage', () => {
  88. assert.equal(storageLength, expectedStorageLength);
  89. });
  90. });
  91. describe('getKeyOf', () => {
  92. let key: string | number | null;
  93. describe('Variant #1', () => {
  94. before(() => {
  95. storage = getStorageInstance<string>();
  96. storage.set(storageKey, storageValue);
  97. key = storage.getKeyOf(storageValue);
  98. });
  99. it('should return key of string value', () => {
  100. assert.equal(key, storageKey);
  101. });
  102. });
  103. describe('Variant #2', () => {
  104. const object: Object = {
  105. bar: 'baz'
  106. };
  107. before(() => {
  108. storage = getStorageInstance<string>();
  109. storage.set(storageKey, object);
  110. key = storage.getKeyOf(object);
  111. });
  112. it('should return key of object if objects in `set` and `get` are two same objects', () => {
  113. assert.equal(key, storageKey);
  114. });
  115. });
  116. describe('Variant #3', () => {
  117. const expectedKey: null = null;
  118. const object: Object = {
  119. bar: 'baz'
  120. };
  121. before(() => {
  122. storage = getStorageInstance<string>();
  123. storage.set(storageKey, object);
  124. key = storage.getKeyOf({...object});
  125. });
  126. it('should return `null` if objects in `set` and `get` are two different objects', () => {
  127. assert.equal(key, expectedKey);
  128. });
  129. });
  130. });
  131. describe('has', () => {
  132. describe('Variant #1: item is presenting in storage', () => {
  133. const expectedItemExistence: boolean = true;
  134. let itemExistence: boolean;
  135. before(() => {
  136. storage = getStorageInstance<string>();
  137. storage.set(storageKey, storageValue);
  138. itemExistence = storage.has(storageKey);
  139. });
  140. it('should return `true` if item is presenting in storage', () => {
  141. assert.equal(itemExistence, expectedItemExistence);
  142. });
  143. });
  144. describe('Variant #2: item isn\'t presenting in storage', () => {
  145. const expectedItemExistence: boolean = false;
  146. let itemExistence: boolean;
  147. before(() => {
  148. storage = getStorageInstance<string>();
  149. itemExistence = storage.has(storageKey);
  150. });
  151. it('should return `false` if item isn\'t presenting in storage', () => {
  152. assert.equal(itemExistence, expectedItemExistence);
  153. });
  154. });
  155. });
  156. describe('set', () => {
  157. let value: string;
  158. before(() => {
  159. storage = getStorageInstance<string>();
  160. storage.set(storageKey, storageValue);
  161. value = storage.get(storageKey);
  162. });
  163. it('should set value to the storage', () => {
  164. assert.equal(value, storageValue);
  165. });
  166. });
  167. describe('mergeWith', () => {
  168. const secondStorageKey: string = 'baz';
  169. const secondStorageValue: string = 'quux';
  170. const expectedArray: string[][] = [
  171. [storageKey, storageValue],
  172. [secondStorageKey, secondStorageValue]
  173. ];
  174. let array: string[][];
  175. before(() => {
  176. storage = getStorageInstance<string>();
  177. storage.set(storageKey, storageValue);
  178. const secondStorage: IMapStorage <string, string> = getStorageInstance<string>();
  179. secondStorage.set(secondStorageKey, secondStorageValue);
  180. storage.mergeWith(secondStorage, false);
  181. array = Array.from(storage.getStorage());
  182. });
  183. it('should merge two storages', () => {
  184. assert.deepEqual(array, expectedArray);
  185. });
  186. });
  187. });