My jest.Spyon
implementation seems pretty straight forward, but the spied function "does not" get called even though I see that it must be getting called.
Function
/**
* Initialize the local & sync storage when the user first installs TabMerger.
* @param {{blacklist: string, color: string, dark: boolean,
* open: "with" | "without", restore: "keep" | "remove", title: string}} default_settings TabMerger's original default settings
* @param {{color: string, created: string, tabs: object[], title: string}} default_group TabMerger's original default group (with up-to-date timestamp)
* @param {HTMLElement} sync_node Node indicating the "Last Sync" time
* @param {Function} setGroups For re-rendering the initial groups
* @param {Function} setTabTotal For re-rendering the total tab counter
*
* @see defaultSettings in App.js
* @see defaultGroup in App.js
*/
// prettier-ignore
export function storageInit(default_settings, default_group, sync_node, setGroups, setTabTotal) {
chrome.storage.sync.get(null, (sync) => {
if (!sync.settings) {
chrome.storage.sync.set({ settings: default_settings }, () => {});
toggleDarkMode(true);
} else {
toggleDarkMode(sync.settings.dark);
}
if (sync["group-0"]) {
toggleSyncTimestampHelper(true, sync_node);
}
delete sync.settings;
chrome.storage.local.get("groups", (local) => {
var ls_entry = local.groups || { "group-0": default_group };
chrome.storage.local.remove(["groups"], () => {
chrome.storage.local.set({ groups: ls_entry }, () => {
setGroups(JSON.stringify(ls_entry));
updateTabTotal(ls_entry, setTabTotal);
});
});
});
});
}
Test
import * as AppFunc from "../src/App/App_functions";
/*
{
AppFunc:
{
toggleDarkMode: [Function: toggleDarkMode],
...
toggleSyncTimestampHelper: [Function: toggleSyncTimestampHelper],
storageInit: [Function: storageInit],
...
}
}
*/
...
test("sync group-0 exists", () => {
const spy = jest.spyOn(AppFunc, "toggleSyncTimestampHelper");
sessionStorage.setItem("group-0", 1);
// prettier-ignore
AppFunc.storageInit(default_settings, default_group, sync_node, mockSet, mockSet);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(true, sync_node);
});
Coverage Report
As can be seen in line 197, it is clearly called once.
Error
This is very confusing because my tests successfully spyOn chrome.storage.___.___
API using jest.spyOn(chrome.storage.local, "get")
but this fails.
If it helps, my repository is open source: https://github.com/lbragile/TabMerger, but this latest test is not included there yet.