2016-11-10 07:34:26 +00:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
|
|
|
import LocalStorage from '../localstorage';
|
|
|
|
|
|
|
|
describe('LocalStorage()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
// There has to be a better way to reset this...
|
|
|
|
setTimeout.mock.calls = [];
|
|
|
|
});
|
|
|
|
|
2016-11-10 09:00:29 +00:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllTimers();
|
2016-11-10 19:08:45 +00:00
|
|
|
});
|
2016-11-10 09:00:29 +00:00
|
|
|
|
2016-11-10 07:34:26 +00:00
|
|
|
it('create directory', () => {
|
|
|
|
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
|
|
|
|
new LocalStorage(basePath);
|
|
|
|
const dir = fs.readdirSync(basePath);
|
|
|
|
expect(dir.length).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does basic operations', () => {
|
|
|
|
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
|
|
|
|
const localStorage = new LocalStorage(basePath);
|
2016-11-10 07:37:46 +00:00
|
|
|
|
|
|
|
// Test get and set
|
|
|
|
localStorage.setItem('foo', 'bar 1');
|
2016-11-10 07:34:26 +00:00
|
|
|
localStorage.setItem('foo', 'bar');
|
|
|
|
expect(localStorage.getItem('foo', 'BAD')).toBe('bar');
|
2016-11-10 07:37:46 +00:00
|
|
|
|
|
|
|
// Test Object storage
|
|
|
|
localStorage.setItem('obj', {foo: 'bar', arr: [1, 2, 3]});
|
|
|
|
expect(localStorage.getItem('obj')).toEqual({foo: 'bar', arr: [1, 2, 3]});
|
|
|
|
|
|
|
|
// Test default values
|
|
|
|
expect(localStorage.getItem('dne', 'default')).toEqual('default');
|
|
|
|
expect(localStorage.getItem('dne')).toEqual('default');
|
2016-11-10 07:34:26 +00:00
|
|
|
});
|
|
|
|
|
2016-11-10 09:00:29 +00:00
|
|
|
it('does handles malformed files', () => {
|
|
|
|
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
|
|
|
|
const localStorage = new LocalStorage(basePath);
|
|
|
|
|
|
|
|
// Assert default is returned on bad JSON
|
|
|
|
fs.writeFileSync(path.join(basePath, 'key'), '{bad JSON');
|
|
|
|
expect(localStorage.getItem('key', 'default')).toBe('default');
|
|
|
|
|
|
|
|
// Assert that writing our file actually works
|
|
|
|
fs.writeFileSync(path.join(basePath, 'key'), '{"good": "JSON"}');
|
|
|
|
expect(localStorage.getItem('key', 'default')).toEqual({good: 'JSON'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does handles failing to write file', () => {
|
|
|
|
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
|
|
|
|
const localStorage = new LocalStorage(basePath);
|
|
|
|
fs.rmdirSync(basePath);
|
|
|
|
localStorage.setItem('key', 'value');
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
// Since the above operation failed to write, we should now get back
|
|
|
|
// the default value
|
|
|
|
expect(localStorage.getItem('key', 'different')).toBe('different');
|
|
|
|
});
|
|
|
|
|
2016-11-10 07:34:26 +00:00
|
|
|
it('stores a key', () => {
|
|
|
|
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
|
|
|
|
const localStorage = new LocalStorage(basePath);
|
|
|
|
localStorage.setItem('foo', 'bar');
|
|
|
|
|
|
|
|
// Assert timeouts are called
|
|
|
|
expect(setTimeout.mock.calls.length).toBe(1);
|
|
|
|
expect(setTimeout.mock.calls[0][1]).toBe(100);
|
|
|
|
|
|
|
|
// Force debouncer to flush
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
// Assert there is one item stored
|
|
|
|
expect(fs.readdirSync(basePath).length).toEqual(1);
|
|
|
|
|
|
|
|
// Assert the contents are correct
|
|
|
|
const contents = fs.readFileSync(path.join(basePath, 'foo'), 'utf8');
|
|
|
|
expect(contents).toEqual('"bar"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('debounces key sets', () => {
|
|
|
|
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
|
|
|
|
const localStorage = new LocalStorage(basePath);
|
|
|
|
localStorage.setItem('foo', 'bar1');
|
|
|
|
localStorage.setItem('another', 10);
|
|
|
|
localStorage.setItem('foo', 'bar3');
|
|
|
|
|
|
|
|
// Assert timeouts are called
|
|
|
|
expect(setTimeout.mock.calls.length).toBe(3);
|
|
|
|
expect(setTimeout.mock.calls[0][1]).toBe(100);
|
|
|
|
expect(setTimeout.mock.calls[1][1]).toBe(100);
|
|
|
|
expect(setTimeout.mock.calls[2][1]).toBe(100);
|
|
|
|
|
|
|
|
expect(fs.readdirSync(basePath).length).toEqual(0);
|
|
|
|
|
|
|
|
// Force flush
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
// Make sure only one item exists
|
|
|
|
expect(fs.readdirSync(basePath).length).toEqual(2);
|
|
|
|
expect(fs.readFileSync(path.join(basePath, 'foo'), 'utf8')).toEqual('"bar3"');
|
|
|
|
expect(fs.readFileSync(path.join(basePath, 'another'), 'utf8')).toEqual('10');
|
|
|
|
});
|
|
|
|
});
|