MapStorage.spec.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { assert } from 'chai';
  2. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  3. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  4. import { IRandomGenerator } from '../../../src/interfaces/utils/IRandomGenerator';
  5. import { IStorage } from '../../../src/interfaces/storages/IStorage';
  6. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  7. import { MapStorage } from '../../../src/storages/MapStorage';
  8. class ConcreteStorage extends MapStorage <string> {
  9. constructor () {
  10. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  11. inversifyContainerFacade.load({});
  12. super(inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator));
  13. }
  14. }
  15. /**
  16. * @type {IStorage<any>}
  17. */
  18. const getStorageInstance = (): IStorage <any> => {
  19. const storage: IStorage<any> = new ConcreteStorage();
  20. storage.initialize();
  21. return storage;
  22. };
  23. describe('MapStorage', () => {
  24. const storageKey: string = 'foo';
  25. const storageValue: string = 'bar';
  26. let storage: IStorage <any>;
  27. describe('initialize (...args: any[]): void', () => {
  28. const expectedError: ErrorConstructor = Error;
  29. let testFunc: () => void;
  30. before(() => {
  31. storage = new ConcreteStorage();
  32. testFunc = () => storage.set(storageKey, storageValue);
  33. });
  34. it('should throws an error when storage isn\'t initialized', () => {
  35. assert.throws(testFunc, expectedError);
  36. });
  37. });
  38. describe('getStorage (): Map <string | number, T>', () => {
  39. const expectedInstanceOf: MapConstructor = Map;
  40. let mapStorage: string[];
  41. before(() => {
  42. storage = getStorageInstance();
  43. mapStorage = storage.getStorage();
  44. });
  45. it('should return storage', () => {
  46. assert.instanceOf(mapStorage, expectedInstanceOf);
  47. });
  48. });
  49. describe('get (key: string | number): T', () => {
  50. describe('variant #1: value exist', () => {
  51. const expectedValue: string = storageValue;
  52. let value: string;
  53. before(() => {
  54. storage = getStorageInstance();
  55. storage.set(storageKey, storageValue);
  56. value = storage.get(storageKey);
  57. });
  58. it('should return value from storage by key', () => {
  59. assert.equal(value, expectedValue);
  60. });
  61. });
  62. describe('variant #2: value isn\'t exist', () => {
  63. const expectedError: ErrorConstructor = Error;
  64. let testFunc: () => void;
  65. before(() => {
  66. storage = getStorageInstance();
  67. testFunc = () => storage.get(storageKey);
  68. });
  69. it('should throw an error', () => {
  70. assert.throws(testFunc, expectedError);
  71. });
  72. });
  73. });
  74. describe('getLength (): number', () => {
  75. const expectedStorageLength: number = 1;
  76. let storageLength: number;
  77. before(() => {
  78. storage = getStorageInstance();
  79. storage.set(storageKey, storageValue);
  80. storageLength = storage.getLength();
  81. });
  82. it('should return length of storage', () => {
  83. assert.equal(storageLength, expectedStorageLength);
  84. });
  85. });
  86. describe('getKeyOf (value: T): string | number | null', () => {
  87. let key: string | number | null;
  88. describe('variant #1', () => {
  89. before(() => {
  90. storage = getStorageInstance();
  91. storage.set(storageKey, storageValue);
  92. key = storage.getKeyOf(storageValue);
  93. });
  94. it('should return key of string value', () => {
  95. assert.equal(key, storageKey);
  96. });
  97. });
  98. describe('variant #2', () => {
  99. const object: Object = {
  100. bar: 'baz'
  101. };
  102. before(() => {
  103. storage = getStorageInstance();
  104. storage.set(storageKey, object);
  105. key = storage.getKeyOf(object);
  106. });
  107. it('should return key of object if objects in `set` and `get` are two same objects', () => {
  108. assert.equal(key, storageKey);
  109. });
  110. });
  111. describe('variant #3', () => {
  112. const expectedKey: null = null;
  113. const object: Object = {
  114. bar: 'baz'
  115. };
  116. before(() => {
  117. storage = getStorageInstance();
  118. storage.set(storageKey, object);
  119. key = storage.getKeyOf({...object});
  120. });
  121. it('should return `null` if objects in `set` and `get` are two different objects', () => {
  122. assert.equal(key, expectedKey);
  123. });
  124. });
  125. });
  126. describe('set (key: string | number, value: T): void', () => {
  127. let value: string;
  128. before(() => {
  129. storage = getStorageInstance();
  130. storage.set(storageKey, storageValue);
  131. value = storage.get(storageKey);
  132. });
  133. it('should set value to the storage', () => {
  134. assert.equal(value, storageValue);
  135. });
  136. });
  137. describe('mergeWith (storage: this, mergeId: boolean = false): void', () => {
  138. const secondStorageKey: string = 'baz';
  139. const secondStorageValue: string = 'quux';
  140. const expectedArray: string[][] = [
  141. [storageKey, storageValue],
  142. [secondStorageKey, secondStorageValue]
  143. ];
  144. let array: string[][];
  145. before(() => {
  146. storage = getStorageInstance();
  147. storage.set(storageKey, storageValue);
  148. const secondStorage: IStorage <string> = getStorageInstance();
  149. secondStorage.set(secondStorageKey, secondStorageValue);
  150. storage.mergeWith(secondStorage, false);
  151. array = <any>Array.from(storage.getStorage());
  152. });
  153. it('should merge two storages', () => {
  154. assert.deepEqual(array, expectedArray);
  155. });
  156. });
  157. });