ArrayStorage.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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('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. foo: 'bar'
  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. foo: 'bar'
  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('set', () => {
  132. let value: string;
  133. before(() => {
  134. storage = getStorageInstance<string>();
  135. storage.set(storageKey, storageValue);
  136. value = storage.get(storageKey);
  137. });
  138. it('should set value to the storage', () => {
  139. assert.equal(value, storageValue);
  140. });
  141. });
  142. describe('mergeWith', () => {
  143. const secondStorageKey: number = 1;
  144. const secondStorageValue: string = 'bar';
  145. const expectedArray: string[] = [storageValue, secondStorageValue];
  146. let array: string[];
  147. before(() => {
  148. storage = getStorageInstance<string>();
  149. storage.set(storageKey, storageValue);
  150. const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
  151. secondStorage.set(secondStorageKey, secondStorageValue);
  152. storage.mergeWith(secondStorage, false);
  153. array = storage.getStorage();
  154. });
  155. it('should merge two storages', () => {
  156. assert.deepEqual(array, expectedArray);
  157. });
  158. });
  159. });