aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/background/infrastructures/memory-storage.js8
-rw-r--r--test/background/infrastructures/memory-storage.test.js41
2 files changed, 47 insertions, 2 deletions
diff --git a/src/background/infrastructures/memory-storage.js b/src/background/infrastructures/memory-storage.js
index 0a05928..234a9b8 100644
--- a/src/background/infrastructures/memory-storage.js
+++ b/src/background/infrastructures/memory-storage.js
@@ -2,10 +2,14 @@ const db = {};
export default class MemoryStorage {
set(name, value) {
- db[name] = value;
+ let data = JSON.stringify(value);
+ if (typeof data === 'undefined') {
+ throw new Error('value is not serializable');
+ }
+ db[name] = data;
}
get(name) {
- return db[name];
+ return JSON.parse(db[name]);
}
}
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();
+ })
+});