ArrayStorage.spec.ts 6.2 KB

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