Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: browser.storage should not include unknown keys in the result #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 14 additions & 33 deletions __tests__/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,21 @@ describe('browser.storage', () => {
test('a string key', (done) => {
const key = 'test';
storage.get(key, (result) => {
expect(result).toBeDefined();
expect(typeof result === 'object').toBeTruthy();
expect(result).toHaveProperty(key, undefined);
expect(result).toStrictEqual({});
done();
});
});
test('an array key', (done) => {
const keys = ['test1', 'test2'];
storage.get(keys, (result) => {
expect(result).toBeDefined();
expect(typeof result === 'object').toBeTruthy();
keys.forEach((k) => {
expect(result).toHaveProperty(k, undefined);
});
expect(result).toStrictEqual({});
done();
});
});
test('an object key', (done) => {
const key = { test: [] };
storage.get(key, (result) => {
expect(result).toBeDefined();
expect(typeof result === 'object').toBeTruthy();
Object.keys(key).forEach((k) => {
expect(result).toHaveProperty(k);
expect(result[k]).toEqual(key[k]);
});
expect(result).toStrictEqual({ test: [] });
done();
});
});
Expand All @@ -71,8 +60,8 @@ describe('browser.storage', () => {
const key = 'key';
return expect(storage.get(key)).resolves.toEqual({ key: undefined });
});
test('getBytesInUse', (done) => {
const callback = jest.fn(() => done());
test('getBytesInUse', () => {
const callback = jest.fn();
expect(jest.isMockFunction(storage.getBytesInUse)).toBe(true);
storage.getBytesInUse('key', callback);
expect(storage.getBytesInUse).toHaveBeenCalledTimes(1);
Expand All @@ -81,8 +70,8 @@ describe('browser.storage', () => {
test('getBytesInUse promise', () => {
return expect(storage.getBytesInUse('key')).resolves.toBe(0);
});
test('set', (done) => {
const callback = jest.fn(() => done());
test('set', () => {
const callback = jest.fn();
expect(jest.isMockFunction(storage.set)).toBe(true);
storage.set({ key: 'foo' }, callback);
expect(storage.set).toHaveBeenCalledTimes(1);
Expand All @@ -91,8 +80,8 @@ describe('browser.storage', () => {
test('set promise', () => {
return expect(storage.set(1)).resolves.toBeUndefined();
});
test('remove', (done) => {
const callback = jest.fn(() => done());
test('remove', () => {
const callback = jest.fn();
expect(jest.isMockFunction(storage.remove)).toBe(true);
storage.remove('key', callback);
expect(storage.remove).toHaveBeenCalledTimes(1);
Expand All @@ -101,8 +90,8 @@ describe('browser.storage', () => {
test('remove promise', () => {
return expect(storage.remove(['foo', 'bar'])).resolves.toBeUndefined();
});
test('clear', (done) => {
const callback = jest.fn(() => done());
test('clear', () => {
const callback = jest.fn();
expect(jest.isMockFunction(browser.storage.sync.clear)).toBe(true);
storage.clear(callback);
expect(storage.clear).toHaveBeenCalledTimes(1);
Expand All @@ -120,24 +109,16 @@ describe('browser.storage', () => {
storage.set({ key: 'value', foo: 'bar', foo2: 'bar2' }, () => {
// get 'key'
storage.get(['key'], (result) => {
expect(result).toBeDefined();
expect(typeof result === 'object').toBeTruthy();
expect(result).toHaveProperty('key', 'value');
expect(result).not.toHaveProperty('foo');
expect(result).not.toHaveProperty('foo2');
expect(result).toStrictEqual({ key: 'value' });
// remove 'key'
storage.remove('key', () => {
// get all values
storage.get(null, (result) => {
expect(result).toHaveProperty('key', undefined);
expect(result).toHaveProperty('foo', 'bar');
expect(result).toHaveProperty('foo2', 'bar2');
expect(result).toStrictEqual({ foo: 'bar', foo2: 'bar2' });
// clear values
storage.clear(() => {
storage.get(['key', 'foo', 'foo2'], (result) => {
expect(result).toHaveProperty('key', undefined);
expect(result).toHaveProperty('foo', undefined);
expect(result).toHaveProperty('foo2', undefined);
expect(result).toStrictEqual({});
done();
});
});
Expand Down
219 changes: 75 additions & 144 deletions dist/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,52 @@ function _defineProperty(obj, key, value) {
}
return obj;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

// https://developer.chrome.com/extensions/omnibox

Expand Down Expand Up @@ -259,115 +305,34 @@ var createEventListeners = function createEventListeners() {
};
};

var syncStore = {};
var localStore = {};
var managedStore = {};
function resolveKey(key, store) {
if (typeof key === 'string') {
var result = {};
result[key] = store[key];
return result;
if (key in store) {
return {
key: store[key]
};
} else {
return {};
}
} else if (Array.isArray(key)) {
return key.reduce(function (acc, curr) {
acc[curr] = store[curr];
return acc;
return key.reduce(function (acc, currKey) {
return _objectSpread2(_objectSpread2({}, acc), resolveKey(currKey, store));
}, {});
} else if (_typeof(key) === 'object') {
return Object.keys(key).reduce(function (acc, curr) {
acc[curr] = store[curr] || key[curr];
return acc;
return Object.entries(key).reduce(function (acc, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
currKey = _ref2[0],
fallbackValue = _ref2[1];
return _objectSpread2(_objectSpread2({}, acc), {}, _defineProperty({}, currKey, fallbackValue), resolveKey(currKey, store));
}, {});
}
throw new Error('Wrong key given');
}
var storage = {
sync: {
get: jest.fn(function (id, cb) {
var result = id === null || id === undefined ? syncStore : resolveKey(id, syncStore);
if (cb !== undefined) {
return cb(result);
}
return Promise.resolve(result);
}),
getBytesInUse: jest.fn(function (id, cb) {
if (cb !== undefined) {
return cb(0);
}
return Promise.resolve(0);
}),
set: jest.fn(function (payload, cb) {
Object.keys(payload).forEach(function (key) {
return syncStore[key] = payload[key];
});
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
remove: jest.fn(function (id, cb) {
var keys = typeof id === 'string' ? [id] : id;
keys.forEach(function (key) {
return delete syncStore[key];
});
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
clear: jest.fn(function (cb) {
syncStore = {};
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
onChanged: createEventListeners()
},
local: {
get: jest.fn(function (id, cb) {
var result = id === null || id === undefined ? localStore : resolveKey(id, localStore);
if (cb !== undefined) {
return cb(result);
}
return Promise.resolve(result);
}),
getBytesInUse: jest.fn(function (id, cb) {
if (cb !== undefined) {
return cb(0);
}
return Promise.resolve(0);
}),
set: jest.fn(function (payload, cb) {
Object.keys(payload).forEach(function (key) {
return localStore[key] = payload[key];
});
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
remove: jest.fn(function (id, cb) {
var keys = typeof id === 'string' ? [id] : id;
keys.forEach(function (key) {
return delete localStore[key];
});
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
clear: jest.fn(function (cb) {
localStore = {};
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
onChanged: createEventListeners()
},
session: {
function mockStore() {
var store = {};
return {
get: jest.fn(function (id, cb) {
var result = id === null || id === undefined ? localStore : resolveKey(id, localStore);
var result = id === null || id === undefined ? store : resolveKey(id, store);
if (cb !== undefined) {
return cb(result);
}
Expand All @@ -381,7 +346,7 @@ var storage = {
}),
set: jest.fn(function (payload, cb) {
Object.keys(payload).forEach(function (key) {
return localStore[key] = payload[key];
return store[key] = payload[key];
});
if (cb !== undefined) {
return cb();
Expand All @@ -391,64 +356,30 @@ var storage = {
remove: jest.fn(function (id, cb) {
var keys = typeof id === 'string' ? [id] : id;
keys.forEach(function (key) {
return delete localStore[key];
return delete store[key];
});
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
clear: jest.fn(function (cb) {
localStore = {};
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
onChanged: createEventListeners()
},
managed: {
get: jest.fn(function (id, cb) {
var result = id === null || id === undefined ? managedStore : resolveKey(id, managedStore);
if (cb !== undefined) {
return cb(result);
}
return Promise.resolve(result);
}),
getBytesInUse: jest.fn(function (id, cb) {
if (cb !== undefined) {
return cb(0);
}
return Promise.resolve(0);
}),
set: jest.fn(function (payload, cb) {
Object.keys(payload).forEach(function (key) {
return managedStore[key] = payload[key];
Object.keys(store).forEach(function (key) {
return delete store[key];
});
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
remove: jest.fn(function (id, cb) {
var keys = typeof id === 'string' ? [id] : id;
keys.forEach(function (key) {
return delete managedStore[key];
});
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
clear: jest.fn(function (cb) {
managedStore = {};
if (cb !== undefined) {
return cb();
}
return Promise.resolve();
}),
onChanged: createEventListeners()
},
};
}
var storage = {
sync: mockStore(),
local: mockStore(),
session: mockStore(),
managed: mockStore(),
onChanged: createEventListeners()
};

Expand Down
Loading