MapStorage.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { assert } from 'chai';
  2. import { IStorage } from '../../../src/interfaces/storages/IStorage';
  3. import { MapStorage } from '../../../src/storages/MapStorage';
  4. class ConcreteStorage extends MapStorage <string> {
  5. constructor () {
  6. super();
  7. }
  8. }
  9. describe('MapStorage', () => {
  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('foo', 'bar'), Error);
  14. });
  15. });
  16. describe('getStorage (): Map <string | number, T>', () => {
  17. it('should returns storage', () => {
  18. const storage: IStorage <string> = new ConcreteStorage();
  19. storage.initialize();
  20. assert.instanceOf(storage.getStorage(), Map);
  21. });
  22. });
  23. describe('get (key: string | number): T', () => {
  24. it('should returns value from storage by key', () => {
  25. const storage: IStorage <string> = new ConcreteStorage();
  26. storage.initialize();
  27. storage.set('foo', 'bar');
  28. assert.equal(storage.get('foo'), 'bar');
  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('foo'), 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('foo', 'bar');
  41. assert.equal(storage.getLength(), 1);
  42. });
  43. });
  44. describe('getKeyOf (value: T): string | number | null', () => {
  45. it('should returns key of string value', () => {
  46. const storage: IStorage <string> = new ConcreteStorage();
  47. storage.initialize();
  48. storage.set('foo', 'bar');
  49. assert.equal(storage.getKeyOf('bar'), 'foo');
  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. bar: 'baz'
  55. };
  56. storage.initialize();
  57. storage.set('foo', object);
  58. assert.equal(storage.getKeyOf(object), 'foo');
  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('foo', {
  64. bar: 'baz'
  65. });
  66. assert.equal(storage.getKeyOf({
  67. bar: 'baz'
  68. }), null);
  69. });
  70. });
  71. describe('set (key: string | 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('foo', 'bar');
  76. assert.equal(storage.get('foo'), 'bar');
  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('foo', 'bar');
  86. storage2.initialize();
  87. storage2.set('baz', 'quux');
  88. storage1.mergeWith(storage2, false);
  89. assert.deepEqual(Array.from(storage1.getStorage()), [['foo', 'bar'], ['baz', 'quux']]);
  90. });
  91. });
  92. });