Skip to content

Instantly share code, notes, and snippets.

@janl janl/checkStorage.js
Last active Nov 17, 2015

Embed
What would you like to do?
module.exports.checkStorage = function () {
try {
global.mozSimpleStorage = require('sdk/simple-storage');
} catch() {
global.mozSimpleStorage = false;
}
if (chrome.storage) { // We're running inside the Chrome/Web Extension context
return 'chromeOrWebExt';
} else if (window.localStorage) { // We're running inside a normal webpage
return 'localStorage';
} else if (mozSimpleStorage) { //We're running in a Firefox Add-On context
return 'firefoxAddOn';
}
};
var storage = require('checkStorage.js');
var test = require('tape');
test ('checkStorage tests with chrome', function (t) {
global.chrome = {'storage': true}; // Mock a chrome/Web extension environment
t.equal(storage.checkStorage(), 'chromeOrWebExt', 'if we find the chrome API, return chromeOrWebExt');
global.chrome = {'storage': false};
t.end();
});
test ('checkStorage tests with localStorage', function (t) {
global.window = {'localStorage': true}; // Add localStorage API mocking.
t.equal(storage.checkStorage(), 'localStorage', 'if we find the localStorage API, return localStorage');
global.window = {'localStorage': false};
t.end();
});
test ('checkStorage tests with simple-storage', function (t) {
global.mozSimpleStorage = true;
t,equal(...)
t.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.