ArrayStorage.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. describe('ArrayStorage', () => {
  10. describe('initialize (...args: any[]): void', () => {
  11. it('should throws an error when storage isn\'t initialized', () => {
  12. const storage: IStorage <string> = new ConcreteStorage();
  13. assert.throws(() => storage.set(0, 'foo'), Error);
  14. });
  15. });
  16. describe('getStorage (): T[]', () => {
  17. it('should returns storage', () => {
  18. const storage: IStorage <string> = new ConcreteStorage();
  19. storage.initialize();
  20. assert.instanceOf(storage.getStorage(), Array);
  21. });
  22. });
  23. describe('get (key: number): T', () => {
  24. it('should returns value from storage by key', () => {
  25. const storage: IStorage <string> = new ConcreteStorage();
  26. storage.initialize();
  27. storage.set(0, 'foo');
  28. assert.equal(storage.get(0), 'foo');
  29. });
  30. it('should throw an error if value isn\'t exist', () => {
  31. const storage: IStorage <string> = new ConcreteStorage();
  32. storage.initialize();
  33. assert.throws(() => storage.get(0), Error);
  34. });
  35. });
  36. describe('getLength (): number', () => {
  37. it('should returns length of storage', () => {
  38. const storage: IStorage <string> = new ConcreteStorage();
  39. storage.initialize();
  40. storage.set(0, 'foo');
  41. assert.equal(storage.getLength(), 1);
  42. });
  43. });
  44. describe('getKeyOf (value: T): number | null', () => {
  45. it('should returns key of string value', () => {
  46. const storage: IStorage <string> = new ConcreteStorage();
  47. storage.initialize();
  48. storage.set(0, 'foo');
  49. assert.equal(storage.getKeyOf('foo'), 0);
  50. });
  51. it('should returns key of object if objects in `set` and `get` are two linked objects', () => {
  52. const storage: IStorage <Object> = new ConcreteStorage();
  53. const object: Object = {
  54. foo: 'bar'
  55. };
  56. storage.initialize();
  57. storage.set(0, object);
  58. assert.equal(storage.getKeyOf(object), 0);
  59. });
  60. it('should return `null` if objects in `set` and `get` are two equal objects', () => {
  61. const storage: IStorage <Object> = new ConcreteStorage();
  62. storage.initialize();
  63. storage.set(0, {
  64. foo: 'bar'
  65. });
  66. assert.equal(storage.getKeyOf({
  67. foo: 'bar'
  68. }), null);
  69. });
  70. });
  71. describe('set (key: number, value: T): void', () => {
  72. it('should set value to the storage', () => {
  73. const storage: IStorage <string> = new ConcreteStorage();
  74. storage.initialize();
  75. storage.set(0, 'foo');
  76. assert.equal(storage.get(0), 'foo');
  77. assert.equal(storage.getLength(), 1);
  78. });
  79. });
  80. describe('mergeWith (storage: this, mergeId: boolean = false): void', () => {
  81. it('should merge two storages', () => {
  82. const storage1: IStorage <string> = new ConcreteStorage();
  83. const storage2: IStorage <string> = new ConcreteStorage();
  84. storage1.initialize();
  85. storage1.set(0, 'foo');
  86. storage2.initialize();
  87. storage2.set(1, 'bar');
  88. storage1.mergeWith(storage2, false);
  89. assert.deepEqual(storage1.getStorage(), ['foo', 'bar']);
  90. });
  91. });
  92. });