ArrayStorage.spec.ts 5.4 KB

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