Skip to content

Instantly share code, notes, and snippets.

@janl
Last active November 17, 2015 13:44
Show Gist options
  • Save janl/530581b17340f79fb76f to your computer and use it in GitHub Desktop.
Save janl/530581b17340f79fb76f to your computer and use it in GitHub Desktop.
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