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

--HG--
extra : rebase_source : b1b5bc2d35624677fc241194ab1a49502cafe31d
This commit is contained in:
Nicolas Chevobbe 2018-02-14 12:11:12 +01:00
Родитель 2d3e2bd1c9
Коммит 5dc6ad75e7
8 изменённых файлов: 68 добавлений и 70 удалений

Просмотреть файл

@ -21,13 +21,12 @@ const {
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", () => {
@ -219,7 +218,7 @@ describe("FilterBar component:", () => {
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,
@ -228,7 +227,7 @@ describe("FilterBar component:", () => {
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);
@ -290,7 +289,7 @@ describe("FilterBar component:", () => {
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,
@ -299,7 +298,7 @@ describe("FilterBar component:", () => {
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`, () => {

Просмотреть файл

@ -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());
}
}
}
};

Просмотреть файл

@ -7,9 +7,10 @@ const reduxActions = require("devtools/client/webconsole/new-console-output/acti
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.
@ -96,11 +97,29 @@ function getLastMessage(state) {
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,
};

Просмотреть файл

@ -5,6 +5,24 @@
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) => {
@ -35,8 +53,7 @@ requireHacker.global_hook("default", (path, module) => {
"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":

Просмотреть файл

@ -14,15 +14,17 @@
" * 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"
}

Просмотреть файл

@ -10,11 +10,10 @@ const { messagesAdd } = require("devtools/client/webconsole/new-console-output/a
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;
@ -226,7 +225,7 @@ describe("Clear filters", () => {
[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,
@ -251,7 +250,7 @@ describe("Clear filters", () => {
[FILTERS.TEXT]: "",
});
expect(ServicesMock.prefs.testHelpers.getFiltersPrefs()).toEqual({
expect(getFiltersPrefs()).toEqual({
[PREFS.FILTER.CSS]: false,
[PREFS.FILTER.DEBUG]: true,
[PREFS.FILTER.ERROR]: true,
@ -290,7 +289,7 @@ describe("Resets filters", () => {
[FILTERS.TEXT]: "foobar",
});
expect(ServicesMock.prefs.testHelpers.getFiltersPrefs()).toEqual({
expect(getFiltersPrefs()).toEqual({
[PREFS.FILTER.WARN]: true,
[PREFS.FILTER.INFO]: true,
[PREFS.FILTER.DEBUG]: true,
@ -317,7 +316,7 @@ describe("Resets filters", () => {
[FILTERS.NETXHR]: true,
});
expect(ServicesMock.prefs.testHelpers.getFiltersPrefs()).toEqual({
expect(getFiltersPrefs()).toEqual({
[PREFS.FILTER.ERROR]: true,
[PREFS.FILTER.WARN]: true,
[PREFS.FILTER.LOG]: true,

Просмотреть файл

@ -317,9 +317,13 @@ describe("Message reducer:", () => {
});
it("properly limits number of messages", () => {
const { dispatch, getState } = setupStore();
const logLimit = 1000;
const { dispatch, getState } = setupStore([], {
storeOptions: {
logLimit,
}
});
const packet = clonePacket(stubPackets.get("console.log(undefined)"));
for (let i = 1; i <= logLimit + 2; i++) {
@ -335,9 +339,12 @@ describe("Message reducer:", () => {
});
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')"));

Просмотреть файл

@ -35,6 +35,7 @@ function getPrefsService(hud) {
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,
};
}