Skip to content

Commit

Permalink
fluid-projectGH-32: in progress of adding tests for background.js
Browse files Browse the repository at this point in the history
  • Loading branch information
cindyli committed Oct 28, 2022
1 parent 89b7aac commit b33d769
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 35 deletions.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js']
}
2 changes: 2 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Add chrome object to global scope
Object.assign(global, require("jest-chrome"));
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test": "run-s test:node test:browser",
"test:node": "nyc node tests/node/all-tests.js",
"test:browser": "testem ci --file tests/browser/testem.js",
"test:jest": "jest",
"test:report": "nyc report -r text-summary -r html -r json",
"posttest": "npm run test:report",
"clean": "run-p clean:*",
Expand All @@ -42,18 +43,19 @@
"dev:webpack": "mix"
},
"dependencies": {
"infusion": "4.3.0"
"infusion": "4.5.0"
},
"devDependencies": {
"eslint-config-fluid": "2.0.1",
"eslint-config-fluid": "2.1.1",
"fluid-lint-all": "1.2.2",
"fluid-testem": "2.1.15",
"jest": "27.0.0",
"jest-chrome": "0.7.2",
"laravel-mix": "6.0.49",
"node-jqunit": "1.1.9",
"npm-run-all": "4.1.5",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "14.0.0",
"testem": "3.9.0"
"testem": "3.9.0",
}
}
4 changes: 2 additions & 2 deletions src/html/adjusters.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<link rel="stylesheet" type="text/css" href="../lib/infusion/src/framework/core/css/fluid.css" />

<!-- Component styles -->
<link rel="stylesheet" type="text/css" href="../lib/infusion/src/framework/preferences/css/Enactors_base.css" />
<link rel="stylesheet" type="text/css" href="../lib/infusion/src/framework/preferences/css/Enactors.css" />
<link rel="stylesheet" type="text/css" href="../lib/infusion/src/framework/preferences/css/PrefsEditor.css" />
<link rel="stylesheet" type="text/css" href="../lib/infusion/src/framework/preferences/css/SeparatedPanelPrefsEditorFrame.css" />
<link rel="stylesheet" type="text/css" href="../lib/infusion/src/framework/preferences/css/SeparatedPanelPrefsEditor.css" />
<link rel="stylesheet" type="text/css" href="../css/adjusters.css" />

<script src="../lib/infusion/dist/infusion-uio.js"></script>
Expand Down
82 changes: 69 additions & 13 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,18 @@ uioPlus.messageHandlers = {

// Functions

/**
* A recursive function for creating checkbox menu items.
*
* @param {Object} menuItems - menu items structure. See `uioPlus.contextMenuItems` for an example.
* @param {Number} parentId - the parent menu item id.
* @param {Object} storage - the currently saved preferences.
*/
uioPlus.createMenuItems = async (menuItems, parentId, storage) => {
if (!menuItems) {
return undefined;
}

storage ??= await chrome.storage.local.get("preferences");

Object.entries(menuItems).forEach(([id, props]) => {
Expand All @@ -110,36 +121,68 @@ uioPlus.createMenuItems = async (menuItems, parentId, storage) => {
});
};

/**
* Update checkbox states based on values updated into the local storage.
*
* @param {Object} menuItems - the changed preferences.
* @param {Object} menuItems - menu items structure. See `uioPlus.contextMenuItems` for an example.
*/
uioPlus.updateQuickPanelState = (menuItems, changes) => {
Object.keys(menuItems.preferences.children).forEach(prefName => {
if (changes.preferences.newValue?.[prefName] !== changes.preferences.oldValue?.[prefName]) {
chrome.contextMenus.update(prefName, {
checked: !!changes.preferences.newValue?.[prefName]
});
}
});
};

/**
* Save preferences whose values are boolean.
* If the previously saved preference value is false, remove it from the saved preferences object. Otherwise, save it.
*
* @param {String} prefName - a preference name.
* @param {Boolean} arrays - the arrays to filter `toFilter` with.
* @return {Promise} - the result of saving the preference.
*/
uioPlus.storePref = async (prefName, state) => {
console.log("in storePref");
let {preferences = {}} = await chrome.storage.local.get("preferences");
state ? preferences[prefName] = state : delete preferences[prefName];
return chrome.storage.local.set({"preferences": preferences});
};

/**
* Save the zoom preference.
*
* @param {Number} zoom - the zoom value.
* @return {Promise} - the result of saving the preference.
*/
uioPlus.storeZoom = async (zoom) => {
if (!zoom) {
return undefined;
}

let {preferences = {}} = await chrome.storage.local.get("preferences");
if ((preferences.uioPlus_prefs_zoom || 1) !== zoom) {
preferences.uioPlus_prefs_zoom = zoom;
return chrome.storage.local.set({"preferences": preferences});
}
};

uioPlus.updateQuickPanelState = (changes) => {
Object.keys(uioPlus.contextMenuItems.preferences.children).forEach(prefName => {
if (changes.preferences.newValue?.[prefName] !== changes.preferences.oldValue?.[prefName]) {
chrome.contextMenus.update(prefName, {
checked: !!changes.preferences.newValue?.[prefName]
});
}
});
};

/**
* Apply zoom to a tab when the zoom value is changed.
*
* @param {Number} zoom - the zoom value.
* @param {Number} tabId - the tab id.
* @return {Promise} - the result of the applying action.
*/
uioPlus.applyZoom = async (zoom, tabId) => {
zoom = zoom || 1;
let currentZoom = await chrome.tabs.getZoom(tabId);

if (currentZoom !== zoom) {
chrome.tabs.setZoom(tabId, zoom);
return chrome.tabs.setZoom(tabId, zoom);
}
};

Expand All @@ -152,7 +195,7 @@ chrome.contextMenus.onClicked.addListener((onClickData) => {
chrome.storage.onChanged.addListener(
(changes, areaName) => {
if (areaName === "local" && changes.preferences) {
uioPlus.updateQuickPanelState(changes);
uioPlus.updateQuickPanelState(uioPlus.contextMenuItems, changes);

uioPlus.applyZoom(changes.preferences.newValue?.uioPlus_prefs_zoom);
}
Expand Down Expand Up @@ -184,4 +227,17 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
});

// Quick Panel Init
uioPlus.createMenuItems(uioPlus.contextMenuItems);
let menu;
// Create the menu only once
if (!menu) {
menu = uioPlus.createMenuItems(uioPlus.contextMenuItems);
}

// For tests only as jest and jest-chrome only works with node.js module scripts
if (typeof exports !== 'undefined') {
exports.createMenuItems = uioPlus.createMenuItems;
exports.storePref = uioPlus.storePref;
exports.storeZoom = uioPlus.storeZoom;
exports.applyZoom = uioPlus.applyZoom;
exports.updateQuickPanelState = uioPlus.updateQuickPanelState;
}
23 changes: 7 additions & 16 deletions src/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"use strict";

fluid.defaults("uioPlus.prefs.store", {
gradeNames: ["fluid.dataSource", "fluid.modelComponent"],
writableGrade: "uioPlus.prefs.store.writable",
gradeNames: ["fluid.dataSource", "fluid.dataSource.writable", "fluid.modelComponent"],
components: {
encoding: {
type: "fluid.dataSource.encoding.model"
Expand All @@ -29,11 +28,17 @@ fluid.defaults("uioPlus.prefs.store", {
"onRead.impl": {
listener: "uioPlus.prefs.store.getFromStorage",
args: ["{arguments}.1"]
},
"onWrite.impl": {
listener: "uioPlus.prefs.store.writeToStorage"
}
},
invokers: {
get: {
args: ["{that}", "{arguments}.0", "{that}.options.storage"]
},
set: {
args: ["{that}", "{arguments}.0", "{arguments}.1", "{that}.options.storage"] // directModel, model, options/callback
}
}
});
Expand All @@ -45,20 +50,6 @@ uioPlus.prefs.store.getFromStorage = function (options) {
return chrome.storage[storageArea].get(key);
};

fluid.defaults("uioPlus.prefs.store.writable", {
gradeNames: ["fluid.dataSource.writable", "fluid.modelComponent"],
listeners: {
"onWrite.impl": {
listener: "uioPlus.prefs.store.writeToStorage"
}
},
invokers: {
set: {
args: ["{that}", "{arguments}.0", "{arguments}.1", "{that}.options.storage"] // directModel, model, options/callback
}
}
});

uioPlus.prefs.store.writeToStorage = async function (payload, options) {
payload.preferences ??= {}; // for the case of an empty payload to ensure old preferences are removed
await chrome.storage[options.area].set(payload);
Expand Down
1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"js/schemas.js",
"js/store.js",
"js/contentView.js",
"js/panels.js",
"js/enactors.js",
"js/enhancer.js"
]
Expand Down
Loading

0 comments on commit b33d769

Please sign in to comment.