From b5b6ba0c74853e1f1a2da11ef88d2bb8daeaac37 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 28 Jul 2018 12:36:42 +0900 Subject: Deep-copy objects on MemoryStorage and add tests --- .../infrastructures/memory-storage.test.js | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/background/infrastructures/memory-storage.test.js (limited to 'test/background/infrastructures') diff --git a/test/background/infrastructures/memory-storage.test.js b/test/background/infrastructures/memory-storage.test.js new file mode 100644 index 0000000..5d62880 --- /dev/null +++ b/test/background/infrastructures/memory-storage.test.js @@ -0,0 +1,41 @@ +import MemoryStorage from 'background/infrastructures/memory-storage'; + +describe("background/infrastructures/memory-storage", () => { + let versionRepository; + + it('stores values', () => { + let cache = new MemoryStorage(); + cache.set('number', 123); + expect(cache.get('number')).to.equal(123); + + cache.set('string', '123'); + expect(cache.get('string')).to.equal('123'); + + cache.set('object', { hello: '123' }); + expect(cache.get('object')).to.deep.equal({ hello: '123' }); + }); + + it('stored on shared memory', () => { + let cache = new MemoryStorage(); + cache.set('red', 'apple'); + + cache = new MemoryStorage(); + let got = cache.get('red'); + expect(got).to.equal('apple'); + }); + + it('stored cloned objects', () => { + let cache = new MemoryStorage(); + let recipe = { sugar: '300g' }; + cache.set('recipe', recipe); + + recipe.salt = '20g' + let got = cache.get('recipe', recipe); + expect(got).to.deep.equal({ sugar: '300g' }); + }); + + it('throws an error with unserializable objects', () => { + let cache = new MemoryStorage(); + expect(() => cache.set('fn', setTimeout)).to.throw(); + }) +}); -- cgit v1.2.3 From 691e9ca8f28a8430e4765ab1a9ae13687289a53b Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 28 Jul 2018 17:06:20 +0900 Subject: Fix last tab is undefined --- src/background/infrastructures/memory-storage.js | 6 +++++- src/background/usecases/operation.js | 4 +++- test/background/infrastructures/memory-storage.test.js | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) (limited to 'test/background/infrastructures') diff --git a/src/background/infrastructures/memory-storage.js b/src/background/infrastructures/memory-storage.js index 234a9b8..3a7e4f2 100644 --- a/src/background/infrastructures/memory-storage.js +++ b/src/background/infrastructures/memory-storage.js @@ -10,6 +10,10 @@ export default class MemoryStorage { } get(name) { - return JSON.parse(db[name]); + let data = db[name]; + if (!data) { + return undefined; + } + return JSON.parse(data); } } diff --git a/src/background/usecases/operation.js b/src/background/usecases/operation.js index f19c632..86f39ca 100644 --- a/src/background/usecases/operation.js +++ b/src/background/usecases/operation.js @@ -183,7 +183,9 @@ export default class OperationInteractor { onTabSelected(tabId) { let lastId = this.cache.get(CURRENT_SELECTED_KEY); - this.cache.set(LAST_SELECTED_KEY, lastId); + if (lastId) { + this.cache.set(LAST_SELECTED_KEY, lastId); + } this.cache.set(CURRENT_SELECTED_KEY, tabId); } } diff --git a/test/background/infrastructures/memory-storage.test.js b/test/background/infrastructures/memory-storage.test.js index 5d62880..0fea895 100644 --- a/test/background/infrastructures/memory-storage.test.js +++ b/test/background/infrastructures/memory-storage.test.js @@ -15,6 +15,11 @@ describe("background/infrastructures/memory-storage", () => { expect(cache.get('object')).to.deep.equal({ hello: '123' }); }); + it('returns undefined if no keys', () => { + let cache = new MemoryStorage(); + expect(cache.get('no-keys')).to.be.undefined; + }) + it('stored on shared memory', () => { let cache = new MemoryStorage(); cache.set('red', 'apple'); -- cgit v1.2.3