|
1 | 1 | import { runFakeRxjs } from '@hypertrace/test-utils';
|
2 | 2 | import { AbstractStorage } from './abstract-storage';
|
| 3 | +import { DictionaryStorageImpl } from './dictionary-storage-impl'; |
3 | 4 |
|
4 | 5 | describe('Abstract storage', () => {
|
5 | 6 | let storage: AbstractStorage;
|
6 | 7 |
|
7 | 8 | beforeEach(() => {
|
8 |
| - storage = new (class extends AbstractStorage {})(localStorage); |
9 |
| - }); |
10 |
| - |
11 |
| - afterEach(() => { |
12 |
| - localStorage.clear(); |
| 9 | + storage = new (class extends AbstractStorage {})(new DictionaryStorageImpl()); |
13 | 10 | });
|
14 | 11 |
|
15 | 12 | test('should support basic crud operations', () => {
|
@@ -37,4 +34,60 @@ describe('Abstract storage', () => {
|
37 | 34 | expectObservable(storage.watch('bar')).toBe('u--b-----', { u: undefined, b: 'b' });
|
38 | 35 | });
|
39 | 36 | });
|
| 37 | + |
| 38 | + test('should support scoped storage with no fallback', () => { |
| 39 | + const globalDictionary = new DictionaryStorageImpl({ foo: 'bad-foo' }); |
| 40 | + |
| 41 | + const scopedStorage = new (class extends AbstractStorage {})(globalDictionary, { |
| 42 | + scopeKey: 'test-scope' |
| 43 | + }); |
| 44 | + |
| 45 | + expect(scopedStorage.get('foo')).toBeUndefined(); |
| 46 | + expect(scopedStorage.contains('foo')).toBe(false); |
| 47 | + scopedStorage.set('foo', 'bar'); |
| 48 | + expect(scopedStorage.get('foo')).toBe('bar'); |
| 49 | + expect(globalDictionary.toJsonString()).toBe('{"foo":"bad-foo","test-scope":"{\\"foo\\":\\"bar\\"}"}'); |
| 50 | + expect(scopedStorage.contains('foo')).toBe(true); |
| 51 | + scopedStorage.set('foo', 'baz'); |
| 52 | + expect(globalDictionary.toJsonString()).toBe('{"foo":"bad-foo","test-scope":"{\\"foo\\":\\"baz\\"}"}'); |
| 53 | + scopedStorage.set('a', 'b'); |
| 54 | + scopedStorage.delete('foo'); |
| 55 | + expect(globalDictionary.toJsonString()).toBe('{"foo":"bad-foo","test-scope":"{\\"a\\":\\"b\\"}"}'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('should support scoped storage with readonly fallback', () => { |
| 59 | + const globalDictionary = new DictionaryStorageImpl({ foo: 'original-foo' }); |
| 60 | + |
| 61 | + const scopedStorage = new (class extends AbstractStorage {})(globalDictionary, { |
| 62 | + scopeKey: 'test-scope', |
| 63 | + fallbackPolicy: 'read-only' |
| 64 | + }); |
| 65 | + |
| 66 | + expect(scopedStorage.get('foo')).toBe('original-foo'); |
| 67 | + expect(scopedStorage.contains('foo')).toBe(true); |
| 68 | + scopedStorage.set('foo', 'bar'); |
| 69 | + expect(scopedStorage.get('foo')).toBe('bar'); |
| 70 | + expect(globalDictionary.toJsonString()).toBe('{"foo":"original-foo","test-scope":"{\\"foo\\":\\"bar\\"}"}'); |
| 71 | + scopedStorage.delete('foo'); |
| 72 | + expect(scopedStorage.contains('foo')).toBe(true); |
| 73 | + expect(scopedStorage.get('foo')).toBe('original-foo'); |
| 74 | + expect(globalDictionary.toJsonString()).toBe('{"foo":"original-foo","test-scope":"{}"}'); |
| 75 | + }); |
| 76 | + |
| 77 | + test('should migrate on read if configured', () => { |
| 78 | + const globalDictionary = new DictionaryStorageImpl({ foo: 'original-foo' }); |
| 79 | + |
| 80 | + const scopedStorage = new (class extends AbstractStorage {})(globalDictionary, { |
| 81 | + scopeKey: 'test-scope', |
| 82 | + fallbackPolicy: 'read-and-migrate' |
| 83 | + }); |
| 84 | + |
| 85 | + expect(scopedStorage.get('foo')).toBe('original-foo'); |
| 86 | + expect(scopedStorage.contains('foo')).toBe(true); |
| 87 | + expect(globalDictionary.toJsonString()).toBe('{"test-scope":"{\\"foo\\":\\"original-foo\\"}"}'); |
| 88 | + |
| 89 | + scopedStorage.delete('foo'); |
| 90 | + expect(scopedStorage.contains('foo')).toBe(false); |
| 91 | + expect(globalDictionary.toJsonString()).toBe('{"test-scope":"{}"}'); |
| 92 | + }); |
40 | 93 | });
|
0 commit comments