Bug 1435092 - Fix mocha tests broken due to changes to prefs; r=bgrins. draft
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Wed, 14 Feb 2018 12:11:12 +0100 (2018-02-14)
changeset 756886 b1b5bc2d35624677fc241194ab1a49502cafe31d
parent 756885 9dc60df24d0514c8bd0aac7655483eb3d026a16a
push id99576
push userbmo:nchevobbe@mozilla.com
push dateMon, 19 Feb 2018 07:33:15 +0000 (2018-02-19)
reviewersbgrins
bugs1435092
milestone60.0a1
Bug 1435092 - Fix mocha tests broken due to changes to prefs; r=bgrins. The mocha test are broken because we mock Services.pref, and the assumptions we make in the mock are no longer valid. Since we do have a fully functional replacement for Services in devtools-modules, let's use it in our test. Which means we need to introduce a mock for localStorage, since it's what the devtools-modules Services uses, and helper functions to replace what was in the Services mock. MozReview-Commit-ID: KLHERfSa1Il
devtools/client/webconsole/new-console-output/test/components/filter-bar.test.js
devtools/client/webconsole/new-console-output/test/fixtures/Services.js
devtools/client/webconsole/new-console-output/test/helpers.js
devtools/client/webconsole/new-console-output/test/mocha-test-setup.js
devtools/client/webconsole/new-console-output/test/package.json
devtools/client/webconsole/new-console-output/test/store/filters.test.js
devtools/client/webconsole/new-console-output/test/store/messages.test.js
devtools/client/webconsole/new-console-output/utils/prefs.js
--- a/devtools/client/webconsole/new-console-output/test/components/filter-bar.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/filter-bar.test.js
@@ -16,23 +16,22 @@ const FilterBar = createFactory(require(
 const { getAllUi } = require("devtools/client/webconsole/new-console-output/selectors/ui");
 const { getAllFilters } = require("devtools/client/webconsole/new-console-output/selectors/filters");
 const {
   MESSAGES_CLEAR,
   FILTERS,
   PREFS,
 } = require("devtools/client/webconsole/new-console-output/constants");
 
-const { setupStore } = require("devtools/client/webconsole/new-console-output/test/helpers");
+const { setupStore, prefsService, clearPrefs } = require("devtools/client/webconsole/new-console-output/test/helpers");
 const serviceContainer = require("devtools/client/webconsole/new-console-output/test/fixtures/serviceContainer");
-const ServicesMock = require("Services");
 
 describe("FilterBar component:", () => {
   afterEach(() => {
-    ServicesMock.prefs.testHelpers.clearPrefs();
+    clearPrefs();
   });
 
   it("initial render", () => {
     const store = setupStore();
 
     const wrapper = render(Provider({store}, FilterBar({
       serviceContainer,
       hidePersistLogsCheckbox: false,
@@ -214,26 +213,26 @@ describe("FilterBar component:", () => {
     const toolbar = wrapper.find(".webconsole-filterbar-filtered-messages");
     expect(toolbar.exists()).toBeFalsy();
   });
 
   it("displays filter bar when button is clicked", () => {
     const store = setupStore();
 
     expect(getAllUi(store.getState()).filterBarVisible).toBe(false);
-    expect(ServicesMock.prefs.getBoolPref(PREFS.UI.FILTER_BAR), false);
+    expect(prefsService.getBoolPref(PREFS.UI.FILTER_BAR), false);
 
     const wrapper = mount(Provider({store}, FilterBar({
       serviceContainer,
       hidePersistLogsCheckbox: false,
     })));
     wrapper.find(".devtools-filter-icon").simulate("click");
 
     expect(getAllUi(store.getState()).filterBarVisible).toBe(true);
-    expect(ServicesMock.prefs.getBoolPref(PREFS.UI.FILTER_BAR), true);
+    expect(prefsService.getBoolPref(PREFS.UI.FILTER_BAR), true);
 
     const secondaryBar = wrapper.find(".webconsole-filterbar-secondary");
     expect(secondaryBar.length).toBe(1);
 
     // Buttons are displayed
     const filterBtn = props => FilterButton(
       Object.assign({}, {
         active: true,
@@ -285,26 +284,26 @@ describe("FilterBar component:", () => {
     wrapper.find(".devtools-plaininput").simulate("input", { target: { value: "a" } });
     expect(store.getState().filters.text).toBe("a");
   });
 
   it("toggles persist logs when checkbox is clicked", () => {
     const store = setupStore();
 
     expect(getAllUi(store.getState()).persistLogs).toBe(false);
-    expect(ServicesMock.prefs.getBoolPref(PREFS.UI.PERSIST), false);
+    expect(prefsService.getBoolPref(PREFS.UI.PERSIST), false);
 
     const wrapper = mount(Provider({store}, FilterBar({
       serviceContainer,
       hidePersistLogsCheckbox: false,
     })));
     wrapper.find(".filter-checkbox input").simulate("change");
 
     expect(getAllUi(store.getState()).persistLogs).toBe(true);
-    expect(ServicesMock.prefs.getBoolPref(PREFS.UI.PERSIST), true);
+    expect(prefsService.getBoolPref(PREFS.UI.PERSIST), true);
   });
 
   it(`doesn't render "Persist logs" input when "hidePersistLogsCheckbox" is true`, () => {
     const store = setupStore();
 
     const wrapper = render(Provider({store}, FilterBar({
       serviceContainer,
       hidePersistLogsCheckbox: true,
deleted file mode 100644
--- a/devtools/client/webconsole/new-console-output/test/fixtures/Services.js
+++ /dev/null
@@ -1,46 +0,0 @@
-/* Any copyright is dedicated to the Public Domain.
-   http://creativecommons.org/publicdomain/zero/1.0/ */
-
-"use strict";
-
-const {
-  DEFAULT_FILTERS_VALUES,
-  FILTERS,
-  PREFS
-} = require("devtools/client/webconsole/new-console-output/constants");
-
-function getDefaultPrefs() {
-  return Object.assign({
-    "devtools.hud.loglimit": 1000,
-    [PREFS.UI.FILTER_BAR]: false,
-    [PREFS.UI.PERSIST]: false,
-  }, Object.entries(PREFS.FILTER).reduce((res, [key, pref]) => {
-    res[pref] = DEFAULT_FILTERS_VALUES[FILTERS[key]];
-    return res;
-  }, {}));
-}
-
-let prefs = Object.assign({}, getDefaultPrefs());
-
-module.exports = {
-  prefs: {
-    getIntPref: pref => prefs[pref],
-    getBoolPref: pref => prefs[pref],
-    setBoolPref: (pref, value) => {
-      prefs[pref] = value;
-    },
-    clearUserPref: (pref) => {
-      prefs[pref] = (getDefaultPrefs())[pref];
-    },
-    testHelpers: {
-      getAllPrefs: () => prefs,
-      getFiltersPrefs: () => Object.values(PREFS.FILTER).reduce((res, pref) => {
-        res[pref] = prefs[pref];
-        return res;
-      }, {}),
-      clearPrefs: () => {
-        prefs = Object.assign({}, getDefaultPrefs());
-      }
-    }
-  }
-};
--- a/devtools/client/webconsole/new-console-output/test/helpers.js
+++ b/devtools/client/webconsole/new-console-output/test/helpers.js
@@ -2,19 +2,20 @@
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 const reduxActions = require("devtools/client/webconsole/new-console-output/actions/index");
 const { configureStore } = require("devtools/client/webconsole/new-console-output/store");
 const { IdGenerator } = require("devtools/client/webconsole/new-console-output/utils/id-generator");
 const { stubPackets } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs/index");
-const {
-  getAllMessagesById,
-} = require("devtools/client/webconsole/new-console-output/selectors/messages");
+const { getAllMessagesById } = require("devtools/client/webconsole/new-console-output/selectors/messages");
+const { getPrefsService } = require("devtools/client/webconsole/new-console-output/utils/prefs");
+const prefsService = getPrefsService({});
+const { PREFS } = require("devtools/client/webconsole/new-console-output/constants");
 
 /**
  * Prepare actions for use in testing.
  */
 function setupActions() {
   // Some actions use dependency injection. This helps them avoid using state in
   // a hard-to-test way. We need to inject stubbed versions of these dependencies.
   const wrappedActions = Object.assign({}, reduxActions);
@@ -91,16 +92,34 @@ function getFirstMessage(state) {
  * @param {object} state - The redux state of the console.
  * @return {Message} - The last message, or undefined if there are no message in store.
  */
 function getLastMessage(state) {
   const lastIndex = getAllMessagesById(state).size - 1;
   return getMessageAt(state, lastIndex);
 }
 
+function getFiltersPrefs() {
+  return Object.values(PREFS.FILTER).reduce((res, pref) => {
+    res[pref] = prefsService.getBoolPref(pref);
+    return res;
+  }, {});
+}
+
+function clearPrefs() {
+  [
+    "devtools.hud.loglimit",
+    ...Object.values(PREFS.FILTER),
+    ...Object.values(PREFS.UI),
+  ].forEach(prefsService.clearUserPref);
+}
+
 module.exports = {
+  clearPrefs,
   clonePacket,
-  getMessageAt,
+  getFiltersPrefs,
   getFirstMessage,
   getLastMessage,
+  getMessageAt,
+  prefsService,
   setupActions,
   setupStore,
 };
--- a/devtools/client/webconsole/new-console-output/test/mocha-test-setup.js
+++ b/devtools/client/webconsole/new-console-output/test/mocha-test-setup.js
@@ -1,15 +1,33 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
 const mcRoot = `${__dirname}/../../../../../`;
 const getModule = mcPath => `module.exports = require("${mcRoot}${mcPath}");`;
 
+const { Services: { pref } } = require("devtools-modules");
+pref("devtools.debugger.remote-timeout", 10000);
+pref("devtools.hud.loglimit", 10000);
+pref("devtools.webconsole.filter.error", true);
+pref("devtools.webconsole.filter.warn", true);
+pref("devtools.webconsole.filter.info", true);
+pref("devtools.webconsole.filter.log", true);
+pref("devtools.webconsole.filter.debug", true);
+pref("devtools.webconsole.filter.css", false);
+pref("devtools.webconsole.filter.net", false);
+pref("devtools.webconsole.filter.netxhr", false);
+pref("devtools.webconsole.ui.filterbar", false);
+pref("devtools.webconsole.inputHistoryCount", 50);
+pref("devtools.webconsole.persistlog", false);
+pref("devtools.webconsole.timestampMessages", false);
+pref("devtools.webconsole.autoMultiline", true);
+pref("devtools.webconsole.sidebarToggle", true);
+
 // Point to vendored-in files and mocks when needed.
 const requireHacker = require("require-hacker");
 requireHacker.global_hook("default", (path, module) => {
   switch (path) {
     // For Enzyme
     case "react-dom":
       return getModule("devtools/client/shared/vendor/react-dom");
     case "react-dom/server":
@@ -30,18 +48,17 @@ requireHacker.global_hook("default", (pa
     case "devtools/shared/l10n":
       return getModule(
         "devtools/client/webconsole/new-console-output/test/fixtures/LocalizationHelper");
     case "devtools/shared/plural-form":
       return getModule(
         "devtools/client/webconsole/new-console-output/test/fixtures/PluralForm");
     case "Services":
     case "Services.default":
-      return getModule(
-        "devtools/client/webconsole/new-console-output/test/fixtures/Services");
+      return `module.exports = require("devtools-modules/src/Services")`;
     case "devtools/shared/client/object-client":
       return `() => {}`;
     case "devtools/client/netmonitor/src/components/TabboxPanel":
       return "{}";
   }
 
   // We need to rewrite all the modules assuming the root is mozilla-central and give them
   // an absolute path.
--- a/devtools/client/webconsole/new-console-output/test/package.json
+++ b/devtools/client/webconsole/new-console-output/test/package.json
@@ -9,21 +9,23 @@
       "Here's the script to run tests with `npm test`. Here's what it does: ",
       " * Run mocha on components, middleware, store and utils folders, on .test.js files.",
       "   We need to specify them so we don't run unwanted tests (e.g. in node_modules).",
       " * We require jsdom-global to inject `document` and `window` objects which are",
       "   not in nodejs natively.",
       " * Finally we require mocha-test-setup where we configure Enzyme and",
       "   intercept require() calls with require-hacker and modify them if needed."
     ],
-    "test": "mocha \"./{,@(components|middleware|store|utils)/**/}*.test.js\" -r jsdom-global/register -r ./mocha-test-setup.js"
+    "test": "mocha \"./{,@(components|middleware|store|utils)/**/}*.test.js\" -r mock-local-storage -r jsdom-global/register -r ./mocha-test-setup.js"
   },
   "dependencies": {
+    "devtools-modules": "0.0.31",
     "enzyme": "^3.3.0",
     "enzyme-adapter-react-16": "^1.1.1",
     "expect": "^1.16.0",
     "jsdom": "^9.4.1",
     "jsdom-global": "^2.0.0",
     "mocha": "^5.0.1",
+    "mock-local-storage": "^1.0.5",
     "require-hacker": "^2.1.4",
     "sinon": "^1.17.5"
   }
 }
--- a/devtools/client/webconsole/new-console-output/test/store/filters.test.js
+++ b/devtools/client/webconsole/new-console-output/test/store/filters.test.js
@@ -5,21 +5,20 @@
 
 const expect = require("expect");
 
 const actions = require("devtools/client/webconsole/new-console-output/actions/index");
 const { messagesAdd } = require("devtools/client/webconsole/new-console-output/actions/index");
 const { ConsoleCommand } = require("devtools/client/webconsole/new-console-output/types");
 const { getVisibleMessages } = require("devtools/client/webconsole/new-console-output/selectors/messages");
 const { getAllFilters } = require("devtools/client/webconsole/new-console-output/selectors/filters");
-const { setupStore } = require("devtools/client/webconsole/new-console-output/test/helpers");
+const { setupStore, getFiltersPrefs } = require("devtools/client/webconsole/new-console-output/test/helpers");
 const { FILTERS, PREFS } = require("devtools/client/webconsole/new-console-output/constants");
 const { stubPackets } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs/index");
 const { stubPreparedMessages } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs/index");
-const ServicesMock = require("Services");
 
 describe("Filtering", () => {
   let store;
   let numMessages;
   // Number of messages in prepareBaseStore which are not filtered out, i.e. Evaluation
   // Results, console commands and console.groups .
   const numUnfilterableMessages = 3;
 
@@ -221,17 +220,17 @@ describe("Clear filters", () => {
       [FILTERS.DEBUG]: true,
       // changed
       [FILTERS.ERROR]: false,
       [FILTERS.CSS]: true,
       [FILTERS.NET]: true,
       [FILTERS.NETXHR]: true,
       [FILTERS.TEXT]: "foobar",
     });
-    expect(ServicesMock.prefs.testHelpers.getFiltersPrefs()).toEqual({
+    expect(getFiltersPrefs()).toEqual({
       [PREFS.FILTER.WARN]: true,
       [PREFS.FILTER.LOG]: true,
       [PREFS.FILTER.INFO]: true,
       [PREFS.FILTER.DEBUG]: true,
       [PREFS.FILTER.ERROR]: false,
       [PREFS.FILTER.CSS]: true,
       [PREFS.FILTER.NET]: true,
       [PREFS.FILTER.NETXHR]: true,
@@ -246,17 +245,17 @@ describe("Clear filters", () => {
       [FILTERS.INFO]: true,
       [FILTERS.LOG]: true,
       [FILTERS.NET]: false,
       [FILTERS.NETXHR]: false,
       [FILTERS.WARN]: true,
       [FILTERS.TEXT]: "",
     });
 
-    expect(ServicesMock.prefs.testHelpers.getFiltersPrefs()).toEqual({
+    expect(getFiltersPrefs()).toEqual({
       [PREFS.FILTER.CSS]: false,
       [PREFS.FILTER.DEBUG]: true,
       [PREFS.FILTER.ERROR]: true,
       [PREFS.FILTER.INFO]: true,
       [PREFS.FILTER.LOG]: true,
       [PREFS.FILTER.NET]: false,
       [PREFS.FILTER.NETXHR]: false,
       [PREFS.FILTER.WARN]: true,
@@ -285,17 +284,17 @@ describe("Resets filters", () => {
       [FILTERS.ERROR]: false,
       [FILTERS.LOG]: false,
       [FILTERS.CSS]: true,
       [FILTERS.NET]: true,
       [FILTERS.NETXHR]: true,
       [FILTERS.TEXT]: "foobar",
     });
 
-    expect(ServicesMock.prefs.testHelpers.getFiltersPrefs()).toEqual({
+    expect(getFiltersPrefs()).toEqual({
       [PREFS.FILTER.WARN]: true,
       [PREFS.FILTER.INFO]: true,
       [PREFS.FILTER.DEBUG]: true,
       [PREFS.FILTER.ERROR]: false,
       [PREFS.FILTER.LOG]: false,
       [PREFS.FILTER.CSS]: true,
       [PREFS.FILTER.NET]: true,
       [PREFS.FILTER.NETXHR]: true,
@@ -312,17 +311,17 @@ describe("Resets filters", () => {
       [FILTERS.DEBUG]: true,
       [FILTERS.TEXT]: "",
       // non-default filters weren't changed
       [FILTERS.CSS]: true,
       [FILTERS.NET]: true,
       [FILTERS.NETXHR]: true,
     });
 
-    expect(ServicesMock.prefs.testHelpers.getFiltersPrefs()).toEqual({
+    expect(getFiltersPrefs()).toEqual({
       [PREFS.FILTER.ERROR]: true,
       [PREFS.FILTER.WARN]: true,
       [PREFS.FILTER.LOG]: true,
       [PREFS.FILTER.INFO]: true,
       [PREFS.FILTER.DEBUG]: true,
       [PREFS.FILTER.CSS]: true,
       [PREFS.FILTER.NET]: true,
       [PREFS.FILTER.NETXHR]: true,
--- a/devtools/client/webconsole/new-console-output/test/store/messages.test.js
+++ b/devtools/client/webconsole/new-console-output/test/store/messages.test.js
@@ -312,37 +312,44 @@ describe("Message reducer:", () => {
       packet = stubPackets.get("console.log(undefined)");
       dispatch(actions.messagesAdd([packet]));
 
       // repeatById should now be empty.
       expect(getAllRepeatById(getState())).toEqual({});
     });
 
     it("properly limits number of messages", () => {
-      const { dispatch, getState } = setupStore();
+      const logLimit = 1000;
+      const { dispatch, getState } = setupStore([], {
+        storeOptions: {
+          logLimit,
+        }
+      });
 
-      const logLimit = 1000;
       const packet = clonePacket(stubPackets.get("console.log(undefined)"));
 
       for (let i = 1; i <= logLimit + 2; i++) {
         packet.message.arguments = [`message num ${i}`];
         dispatch(actions.messagesAdd([packet]));
       }
 
       const messages = getAllMessagesById(getState());
       expect(messages.size).toBe(logLimit);
       expect(getFirstMessage(getState()).parameters[0]).toBe(`message num 3`);
       expect(getLastMessage(getState()).parameters[0])
         .toBe(`message num ${logLimit + 2}`);
     });
 
     it("properly limits number of messages when there are nested groups", () => {
-      const { dispatch, getState } = setupStore();
-
       const logLimit = 1000;
+      const { dispatch, getState } = setupStore([], {
+        storeOptions: {
+          logLimit,
+        }
+      });
 
       const packet = clonePacket(stubPackets.get("console.log(undefined)"));
       const packetGroup = clonePacket(stubPackets.get("console.group('bar')"));
       const packetGroupEnd = clonePacket(stubPackets.get("console.groupEnd()"));
 
       packetGroup.message.arguments = [`group-1`];
       dispatch(actions.messagesAdd([packetGroup]));
       packetGroup.message.arguments = [`group-1-1`];
--- a/devtools/client/webconsole/new-console-output/utils/prefs.js
+++ b/devtools/client/webconsole/new-console-output/utils/prefs.js
@@ -30,14 +30,15 @@ function getPreferenceName(hud, suffix) 
 function getPrefsService(hud) {
   const getPrefName = pref => getPreferenceName(hud, pref);
 
   return {
     getBoolPref: (pref, deflt) => Services.prefs.getBoolPref(getPrefName(pref), deflt),
     getIntPref: (pref, deflt) => Services.prefs.getIntPref(getPrefName(pref), deflt),
     setBoolPref: (pref, value) => Services.prefs.setBoolPref(getPrefName(pref), value),
     clearUserPref: pref => Services.prefs.clearUserPref(getPrefName(pref)),
+    getPrefName,
   };
 }
 
 module.exports = {
   getPrefsService,
 };