Bug 1363680 - Adapt mocha tests. r=Honza draft
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Mon, 05 Jun 2017 13:41:09 +0200
changeset 589003 aa94cd8db6785b19008f67086cd0a9ca3371bcab
parent 589002 ccd6b6c4b096975e22f3d9193f43504a97733ed8
child 631738 b9c65b066406bab613e186333b926b69a242d0d3
push id62217
push userbmo:nchevobbe@mozilla.com
push dateMon, 05 Jun 2017 12:19:44 +0000
reviewersHonza
bugs1363680
milestone55.0a1
Bug 1363680 - Adapt mocha tests. r=Honza MozReview-Commit-ID: Jl48dtpqlm7
devtools/client/webconsole/new-console-output/test/components/console-api-call.test.js
devtools/client/webconsole/new-console-output/test/helpers.js
devtools/client/webconsole/new-console-output/test/store/messages.test.js
--- a/devtools/client/webconsole/new-console-output/test/components/console-api-call.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/console-api-call.test.js
@@ -62,20 +62,22 @@ describe("ConsoleAPICall component:", ()
       const secondElementStyle = elements.eq(1).prop("style");
       // Allowed styles are applied accordingly on the second element.
       expect(secondElementStyle.color).toBe(`red`);
       // Forbidden styles are not applied.
       expect(secondElementStyle.background).toBe(undefined);
     });
 
     it("renders repeat node", () => {
-      const message =
-        stubPreparedMessages.get("console.log('foobar', 'test')")
-        .set("repeat", 107);
-      const wrapper = render(ConsoleApiCall({ message, serviceContainer }));
+      const message = stubPreparedMessages.get("console.log('foobar', 'test')");
+      const wrapper = render(ConsoleApiCall({
+        message,
+        serviceContainer,
+        repeat: 107
+      }));
 
       expect(wrapper.find(".message-repeats").text()).toBe("107");
       expect(wrapper.find(".message-repeats").prop("title")).toBe("107 repeats");
 
       let selector = "span > span.message-flex-body > " +
         "span.message-body.devtools-monospace + span.message-repeats";
       expect(wrapper.find(selector).length).toBe(1);
     });
--- a/devtools/client/webconsole/new-console-output/test/helpers.js
+++ b/devtools/client/webconsole/new-console-output/test/helpers.js
@@ -6,16 +6,19 @@
 let ReactDOM = require("devtools/client/shared/vendor/react-dom");
 let React = require("devtools/client/shared/vendor/react");
 var TestUtils = React.addons.TestUtils;
 
 const actions = 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");
 
 /**
  * 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({}, actions);
@@ -61,15 +64,28 @@ function shallowRenderComponent(componen
 
 /**
  * Create deep copy of given packet object.
  */
 function clonePacket(packet) {
   return JSON.parse(JSON.stringify(packet));
 }
 
+/**
+ * Return the message in the store at the given index.
+ *
+ * @param {object} state - The redux state of the console.
+ * @param {int} index - The index of the message in the map.
+ * @return {Message} - The message, or undefined if the index does not exists in the map.
+ */
+function getMessageAt(state, index) {
+  const messages = getAllMessagesById(state);
+  return messages.get([...messages.keys()][index]);
+}
+
 module.exports = {
+  clonePacket,
+  getMessageAt,
+  renderComponent,
   setupActions,
   setupStore,
-  renderComponent,
   shallowRenderComponent,
-  clonePacket
 };
--- a/devtools/client/webconsole/new-console-output/test/store/messages.test.js
+++ b/devtools/client/webconsole/new-console-output/test/store/messages.test.js
@@ -2,23 +2,25 @@
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
 const {
   getAllGroupsById,
   getAllMessagesById,
   getAllMessagesTableDataById,
   getAllMessagesUiById,
+  getAllRepeatById,
   getCurrentGroup,
   getVisibleMessages,
 } = require("devtools/client/webconsole/new-console-output/selectors/messages");
 const {
+  clonePacket,
+  getMessageAt,
   setupActions,
   setupStore,
-  clonePacket
 } = require("devtools/client/webconsole/new-console-output/test/helpers");
 const { stubPackets, stubPreparedMessages } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs/index");
 const {
   MESSAGE_TYPE,
 } = require("devtools/client/webconsole/new-console-output/constants");
 
 const expect = require("expect");
 
@@ -52,33 +54,59 @@ describe("Message reducer:", () => {
       packet.message.timeStamp = 1;
       dispatch(actions.messageAdd(packet));
       packet.message.timeStamp = 2;
       dispatch(actions.messageAdd(packet));
 
       const messages = getAllMessagesById(getState());
 
       expect(messages.size).toBe(1);
-      expect(messages.first().repeat).toBe(4);
+
+      const repeat = getAllRepeatById(getState());
+      expect(repeat[messages.first().id]).toBe(4);
+    });
+
+    it("does not increment repeat after closing a group", () => {
+      const logKey = "console.log('foobar', 'test')";
+      const { getState } = setupStore([
+        logKey,
+        logKey,
+        "console.group('bar')",
+        logKey,
+        logKey,
+        logKey,
+        "console.groupEnd()",
+        logKey,
+      ]);
+
+      const messages = getAllMessagesById(getState());
+
+      expect(messages.size).toBe(4);
+      const repeat = getAllRepeatById(getState());
+      expect(repeat[messages.first().id]).toBe(2);
+      expect(repeat[getMessageAt(getState(), 2).id]).toBe(3);
+      expect(repeat[messages.last().id]).toBe(undefined);
     });
 
     it("does not clobber a unique message", () => {
       const key1 = "console.log('foobar', 'test')";
       const { dispatch, getState } = setupStore([key1, key1]);
 
       const packet = stubPackets.get(key1);
       dispatch(actions.messageAdd(packet));
 
       const packet2 = stubPackets.get("console.log(undefined)");
       dispatch(actions.messageAdd(packet2));
 
       const messages = getAllMessagesById(getState());
       expect(messages.size).toBe(2);
-      expect(messages.first().repeat).toBe(3);
-      expect(messages.last().repeat).toBe(1);
+
+      const repeat = getAllRepeatById(getState());
+      expect(repeat[messages.first().id]).toBe(3);
+      expect(repeat[messages.last().id]).toBe(undefined);
     });
 
     it("adds a message in response to console.clear()", () => {
       const { dispatch, getState } = setupStore([]);
 
       dispatch(actions.messageAdd(stubPackets.get("console.clear()")));
 
       const messages = getAllMessagesById(getState());
@@ -99,16 +127,17 @@ describe("Message reducer:", () => {
 
       const state = getState();
       expect(getAllMessagesById(state).size).toBe(0);
       expect(getVisibleMessages(state).length).toBe(0);
       expect(getAllMessagesUiById(state).size).toBe(0);
       expect(getAllGroupsById(state).size).toBe(0);
       expect(getAllMessagesTableDataById(state).size).toBe(0);
       expect(getCurrentGroup(state)).toBe(null);
+      expect(getAllRepeatById(state)).toEqual({});
     });
 
     it("properly limits number of messages", () => {
       const { dispatch, getState } = setupStore([]);
 
       const logLimit = 1000;
       const packet = clonePacket(stubPackets.get("console.log(undefined)"));