Bug 1358507 - Adapt tests to the new component architecture; r=bgrins draft
authornchevobbe <nchevobbe@mozilla.com>
Fri, 28 Apr 2017 12:15:45 +0200
changeset 570531 c825430a92c7346edee720f54a242b65467025c5
parent 570075 c3bf1874f85f50f02d0adbbd1e498a604c3b5ebe
child 626519 49390cf229713b0c7ff2c3adb25f71dd863bddae
push id56523
push userbmo:nchevobbe@mozilla.com
push dateSat, 29 Apr 2017 08:46:33 +0000
reviewersbgrins
bugs1358507
milestone55.0a1
Bug 1358507 - Adapt tests to the new component architecture; r=bgrins MozReview-Commit-ID: LfHkDyIwIv5
devtools/client/webconsole/new-console-output/test/components/console-api-call.test.js
devtools/client/webconsole/new-console-output/test/components/evaluation-result.test.js
devtools/client/webconsole/new-console-output/test/components/message-container.test.js
devtools/client/webconsole/new-console-output/test/components/message-repeat.test.js
devtools/client/webconsole/new-console-output/test/components/network-event-message.test.js
devtools/client/webconsole/new-console-output/test/components/page-error.test.js
devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_context_menu_copy_entire_message.js
devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_timestamps.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
@@ -87,24 +87,38 @@ describe("ConsoleAPICall component:", ()
       let wrapper = render(ConsoleApiCall({ message, serviceContainer, indent }));
       expect(wrapper.find(".indent").prop("style").width)
         .toBe(`${indent * INDENT_WIDTH}px`);
 
       wrapper = render(ConsoleApiCall({ message, serviceContainer}));
       expect(wrapper.find(".indent").prop("style").width).toBe(`0`);
     });
 
-    it("renders a timestamp", () => {
+    it("renders a timestamp when passed a truthy timestampsVisible prop", () => {
       const message = stubPreparedMessages.get("console.log('foobar', 'test')");
-      const wrapper = render(ConsoleApiCall({ message, serviceContainer }));
+      const wrapper = render(ConsoleApiCall({
+        message,
+        serviceContainer,
+        timestampsVisible: true,
+      }));
       const L10n = require("devtools/client/webconsole/new-console-output/test/fixtures/L10n");
       const { timestampString } = new L10n();
 
       expect(wrapper.find(".timestamp").text()).toBe(timestampString(message.timeStamp));
     });
+
+    it("does not render a timestamp when not asked to", () => {
+      const message = stubPreparedMessages.get("console.log('foobar', 'test')");
+      const wrapper = render(ConsoleApiCall({
+        message,
+        serviceContainer,
+      }));
+
+      expect(wrapper.find(".timestamp").length).toBe(0);
+    });
   });
 
   describe("console.count", () => {
     it("renders", () => {
       const message = stubPreparedMessages.get("console.count('bar')");
       const wrapper = render(ConsoleApiCall({ message, serviceContainer }));
 
       expect(wrapper.find(".message-body").text()).toBe("bar: 1");
--- a/devtools/client/webconsole/new-console-output/test/components/evaluation-result.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/evaluation-result.test.js
@@ -86,17 +86,30 @@ describe("EvaluationResult component:", 
     const message = stubPreparedMessages.get("1 + @");
     const wrapper = render(EvaluationResult({ message }));
 
     const locationLink = wrapper.find(`.message-location`);
     expect(locationLink.length).toBe(1);
     expect(locationLink.text()).toBe("debugger eval code:1:4");
   });
 
-  it("has a timestamp", () => {
+  it("has a timestamp when passed a truthy timestampsVisible prop", () => {
     const message = stubPreparedMessages.get("new Date(0)");
-    const wrapper = render(EvaluationResult({ message }));
+    const wrapper = render(EvaluationResult({
+      message,
+      timestampsVisible: true,
+    }));
     const L10n = require("devtools/client/webconsole/new-console-output/test/fixtures/L10n");
     const { timestampString } = new L10n();
 
     expect(wrapper.find(".timestamp").text()).toBe(timestampString(message.timeStamp));
   });
+
+  it("does not have a timestamp when timestampsVisible prop is falsy", () => {
+    const message = stubPreparedMessages.get("new Date(0)");
+    const wrapper = render(EvaluationResult({
+      message,
+      timestampsVisible: false,
+    }));
+
+    expect(wrapper.find(".timestamp").length).toBe(0);
+  });
 });
--- a/devtools/client/webconsole/new-console-output/test/components/message-container.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/message-container.test.js
@@ -1,21 +1,20 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
 // Test utils.
 const expect = require("expect");
 const {
   renderComponent,
-  shallowRenderComponent
 } = require("devtools/client/webconsole/new-console-output/test/helpers");
 
 // Components under test.
-const { MessageContainer } = require("devtools/client/webconsole/new-console-output/components/message-container");
+const { MessageContainer, getMessageComponent } = require("devtools/client/webconsole/new-console-output/components/message-container");
 const ConsoleApiCall = require("devtools/client/webconsole/new-console-output/components/message-types/console-api-call");
 const EvaluationResult = require("devtools/client/webconsole/new-console-output/components/message-types/evaluation-result");
 const PageError = require("devtools/client/webconsole/new-console-output/components/message-types/page-error");
 
 // Test fakes.
 const { stubPreparedMessages } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs/index");
 const serviceContainer = require("devtools/client/webconsole/new-console-output/test/fixtures/serviceContainer");
 
@@ -45,16 +44,12 @@ describe("MessageContainer component:", 
         message: stubPreparedMessages.get(
           "Unknown property ‘such-unknown-property’.  Declaration dropped."
         )
       }
     ];
 
     messageTypes.forEach(info => {
       const { component, message } = info;
-      const rendered = shallowRenderComponent(MessageContainer, {
-        message,
-        serviceContainer,
-      });
-      expect(rendered.type).toBe(component);
+      expect(getMessageComponent(message)).toBe(component);
     });
   });
 });
--- a/devtools/client/webconsole/new-console-output/test/components/message-repeat.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/message-repeat.test.js
@@ -9,17 +9,15 @@ const expect = require("expect");
 const {
   renderComponent
 } = require("devtools/client/webconsole/new-console-output/test/helpers");
 
 describe("MessageRepeat component:", () => {
   it("renders repeated value correctly", () => {
     const rendered = renderComponent(MessageRepeat, { repeat: 99 });
     expect(rendered.classList.contains("message-repeats")).toBe(true);
-    expect(rendered.style.visibility).toBe("visible");
     expect(rendered.textContent).toBe("99");
   });
 
-  it("renders an un-repeated value correctly", () => {
-    const rendered = renderComponent(MessageRepeat, { repeat: 1 });
-    expect(rendered.style.visibility).toBe("hidden");
+  it("does not render un-repeated value", () => {
+    expect(MessageRepeat({ repeat: 1 })).toBe(null);
   });
 });
--- a/devtools/client/webconsole/new-console-output/test/components/network-event-message.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/network-event-message.test.js
@@ -19,29 +19,44 @@ const serviceContainer = require("devtoo
 
 const EXPECTED_URL = "http://example.com/browser/devtools/client/webconsole/new-console-output/test/fixtures/stub-generators/inexistent.html";
 const EXPECTED_STATUS = /\[HTTP\/\d\.\d \d+ [A-Za-z ]+ \d+ms\]/;
 
 describe("NetworkEventMessage component:", () => {
   describe("GET request", () => {
     it("renders as expected", () => {
       const message = stubPreparedMessages.get("GET request eventTimings");
-      const wrapper = render(NetworkEventMessage({ message, serviceContainer }));
+      const wrapper = render(NetworkEventMessage({
+        message,
+        serviceContainer,
+        timestampsVisible: true,
+      }));
       const L10n = require("devtools/client/webconsole/new-console-output/test/fixtures/L10n");
       const { timestampString } = new L10n();
 
       expect(wrapper.find(".timestamp").text()).toBe(timestampString(message.timeStamp));
       expect(wrapper.find(".message-body .method").text()).toBe("GET");
       expect(wrapper.find(".message-body .xhr").length).toBe(0);
       expect(wrapper.find(".message-body .url").length).toBe(1);
       expect(wrapper.find(".message-body .url").text()).toBe(EXPECTED_URL);
       expect(wrapper.find(".message-body .status").length).toBe(1);
       expect(wrapper.find(".message-body .status").text()).toMatch(EXPECTED_STATUS);
     });
 
+    it("does not have a timestamp when timestampsVisible prop is falsy", () => {
+      const message = stubPreparedMessages.get("GET request eventTimings");
+      const wrapper = render(NetworkEventMessage({
+        message,
+        serviceContainer,
+        timestampsVisible: false,
+      }));
+
+      expect(wrapper.find(".timestamp").length).toBe(0);
+    });
+
     it("has the expected indent", () => {
       const message = stubPreparedMessages.get("GET request");
 
       const indent = 10;
       let wrapper = render(NetworkEventMessage({ message, serviceContainer, indent}));
       expect(wrapper.find(".indent").prop("style").width)
         .toBe(`${indent * INDENT_WIDTH}px`);
 
--- a/devtools/client/webconsole/new-console-output/test/components/page-error.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/page-error.test.js
@@ -22,17 +22,21 @@ const { INDENT_WIDTH } = require("devtoo
 
 // Test fakes.
 const { stubPreparedMessages } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs/index");
 const serviceContainer = require("devtools/client/webconsole/new-console-output/test/fixtures/serviceContainer");
 
 describe("PageError component:", () => {
   it("renders", () => {
     const message = stubPreparedMessages.get("ReferenceError: asdf is not defined");
-    const wrapper = render(PageError({ message, serviceContainer }));
+    const wrapper = render(PageError({
+      message,
+      serviceContainer,
+      timestampsVisible: true,
+    }));
     const L10n = require("devtools/client/webconsole/new-console-output/test/fixtures/L10n");
     const { timestampString } = new L10n();
 
     expect(wrapper.find(".timestamp").text()).toBe(timestampString(message.timeStamp));
 
     expect(wrapper.find(".message-body").text())
       .toBe("ReferenceError: asdf is not defined[Learn More]");
 
@@ -42,16 +46,27 @@ describe("PageError component:", () => {
 
     // There should be the location.
     const locationLink = wrapper.find(`.message-location`);
     expect(locationLink.length).toBe(1);
     // @TODO Will likely change. See bug 1307952
     expect(locationLink.text()).toBe("test-console-api.html:3:5");
   });
 
+  it("does not have a timestamp when timestampsVisible prop is falsy", () => {
+    const message = stubPreparedMessages.get("ReferenceError: asdf is not defined");
+    const wrapper = render(PageError({
+      message,
+      serviceContainer,
+      timestampsVisible: false,
+    }));
+
+    expect(wrapper.find(".timestamp").length).toBe(0);
+  });
+
   it("renders an error with a longString exception message", () => {
     const message = stubPreparedMessages.get("TypeError longString message");
     const wrapper = render(PageError({ message, serviceContainer }));
 
     const text = wrapper.find(".message-body").text();
     expect(text.startsWith("Error: Long error Long error")).toBe(true);
   });
 
--- a/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_context_menu_copy_entire_message.js
+++ b/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_context_menu_copy_entire_message.js
@@ -1,34 +1,44 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
+const {PrefObserver} = require("devtools/client/shared/prefs");
+
 // RegExp that validates copied text for log lines.
-const LOG_FORMAT = /^[\d:.]+ .+ \d+ .+:\d+$/;
+const LOG_FORMAT_WITH_TIMESTAMP = /^[\d:.]+ .+ (\d+ )?.+:\d+$/;
+const LOG_FORMAT_WITHOUT_TIMESTAMP = /^.+ (\d+ )?.+:\d+$/;
 // RegExp that validates copied text for stacktrace lines.
 const TRACE_FORMAT = /^\t.+ .+:\d+:\d+$/;
 
+const PREF_MESSAGE_TIMESTAMP = "devtools.webconsole.timestampMessages";
+
 const TEST_URI = `data:text/html;charset=utf-8,<script>
   window.logStuff = function () {
     console.log("simple text message");
     function wrapper() {
       console.trace();
     }
     wrapper();
   };
 </script>`;
 
 // Test the Copy menu item of the webconsole copies the expected clipboard text for
 // different log messages.
 
 add_task(function* () {
+  let observer = new PrefObserver("");
+  let onPrefUpdated = observer.once(PREF_MESSAGE_TIMESTAMP, () => {});
+  Services.prefs.setBoolPref(PREF_MESSAGE_TIMESTAMP, true);
+  yield onPrefUpdated;
+
   let hud = yield openNewTabAndConsole(TEST_URI);
   hud.jsterm.clearOutput();
 
   info("Call the log function defined in the test page");
   yield ContentTask.spawn(gBrowser.selectedBrowser, null, () => {
     content.wrappedJSObject.logStuff();
   });
 
@@ -36,30 +46,67 @@ add_task(function* () {
   let message = yield waitFor(() => findMessage(hud, "simple text message"));
   let clipboardText = yield copyMessageContent(hud, message);
   ok(true, "Clipboard text was found and saved");
 
   info("Check copied text for simple log message");
   let lines = clipboardText.split("\n");
   ok(lines.length, 2, "There are 2 lines in the copied text");
   is(lines[1], "", "The last line is an empty new line");
-  ok(LOG_FORMAT.test(lines[0]), "Log line has the right format:\n" + lines[0]);
+  ok(LOG_FORMAT_WITH_TIMESTAMP.test(lines[0]),
+    "Log line has the right format:\n" + lines[0]);
 
   info("Test copy menu item for the stack trace message");
   message = yield waitFor(() => findMessage(hud, "console.trace"));
   clipboardText = yield copyMessageContent(hud, message);
   ok(true, "Clipboard text was found and saved");
 
   info("Check copied text for stack trace message");
   lines = clipboardText.split("\n");
   ok(lines.length, 4, "There are 4 lines in the copied text");
   is(lines[3], "", "The last line is an empty new line");
-  ok(LOG_FORMAT.test(lines[0]), "Log line has the right format:\n" + lines[0]);
+  ok(LOG_FORMAT_WITH_TIMESTAMP.test(lines[0]),
+    "Log line has the right format:\n" + lines[0]);
   ok(TRACE_FORMAT.test(lines[1]), "Stacktrace line has the right format:\n" + lines[1]);
   ok(TRACE_FORMAT.test(lines[2]), "Stacktrace line has the right format:\n" + lines[2]);
+
+  info("Test copy menu item without timestamp");
+
+  onPrefUpdated = observer.once(PREF_MESSAGE_TIMESTAMP, () => {});
+  Services.prefs.setBoolPref(PREF_MESSAGE_TIMESTAMP, false);
+  yield onPrefUpdated;
+
+  info("Test copy menu item for the simple log");
+  message = yield waitFor(() => findMessage(hud, "simple text message"));
+  clipboardText = yield copyMessageContent(hud, message);
+  ok(true, "Clipboard text was found and saved");
+
+  info("Check copied text for simple log message");
+  lines = clipboardText.split("\n");
+  ok(lines.length, 2, "There are 2 lines in the copied text");
+  is(lines[1], "", "The last line is an empty new line");
+  ok(LOG_FORMAT_WITHOUT_TIMESTAMP.test(lines[0]),
+    "Log line has the right format:\n" + lines[0]);
+
+  info("Test copy menu item for the stack trace message");
+  message = yield waitFor(() => findMessage(hud, "console.trace"));
+  clipboardText = yield copyMessageContent(hud, message);
+  ok(true, "Clipboard text was found and saved");
+
+  info("Check copied text for stack trace message");
+  lines = clipboardText.split("\n");
+  ok(lines.length, 4, "There are 4 lines in the copied text");
+  is(lines[3], "", "The last line is an empty new line");
+  ok(LOG_FORMAT_WITHOUT_TIMESTAMP.test(lines[0]),
+    "Log line has the right format:\n" + lines[0]);
+  ok(TRACE_FORMAT.test(lines[1]), "Stacktrace line has the right format:\n" + lines[1]);
+  ok(TRACE_FORMAT.test(lines[2]), "Stacktrace line has the right format:\n" + lines[2]);
+
+  observer.destroy();
+  Services.prefs.clearUserPref(PREF_MESSAGE_TIMESTAMP);
 });
 
 /**
  * Simple helper method to open the context menu on a given message, and click on the copy
  * menu item.
  */
 function* copyMessageContent(hud, message) {
   let menuPopup = yield openContextMenu(hud, message);
--- a/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_timestamps.js
+++ b/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_timestamps.js
@@ -5,54 +5,64 @@
 
 // Test for the message timestamps option: check if the preference toggles the
 // display of messages in the console output. See bug 722267.
 
 "use strict";
 
 const {PrefObserver} = require("devtools/client/shared/prefs");
 
-const TEST_URI = "data:text/html;charset=utf-8,Web Console test for " +
-                 "bug 1307871 - preference for toggling timestamps in messages";
+const TEST_URI = `data:text/html;charset=utf-8,
+  Web Console test for bug 1307871 - preference for toggling timestamps in messages
+  <script>
+    window.logMessage = function () {
+      console.log("simple text message");
+    };
+  </script>`;
 const PREF_MESSAGE_TIMESTAMP = "devtools.webconsole.timestampMessages";
 
 add_task(function* () {
   let hud = yield openNewTabAndConsole(TEST_URI);
-  let outputNode = hud.ui.experimentalOutputNode;
-  let outputEl = outputNode.querySelector(".webconsole-output");
 
-  testPrefDefaults(outputEl);
+  info("Call the log function defined in the test page");
+  yield ContentTask.spawn(gBrowser.selectedBrowser, null, () => {
+    content.wrappedJSObject.logMessage();
+  });
+
+  yield testPrefDefaults(hud);
 
   let observer = new PrefObserver("");
   let toolbox = gDevTools.getToolbox(hud.target);
   let optionsPanel = yield toolbox.selectTool("options");
   yield togglePref(optionsPanel, observer);
   observer.destroy();
 
-  yield testChangedPref(outputEl);
+  yield testChangedPref(hud);
 
   Services.prefs.clearUserPref(PREF_MESSAGE_TIMESTAMP);
 });
 
-function testPrefDefaults(outputEl) {
+function* testPrefDefaults(hud) {
   let prefValue = Services.prefs.getBoolPref(PREF_MESSAGE_TIMESTAMP);
   ok(!prefValue, "Messages should have no timestamp by default (pref check)");
-  ok(outputEl.classList.contains("hideTimestamps"),
-     "Messages should have no timestamp (class name check)");
+  let message = yield waitFor(() => findMessage(hud, "simple text message"));
+  is(message.querySelectorAll(".timestamp").length, 0,
+     "Messages should have no timestamp by default (element check)");
 }
 
 function* togglePref(panel, observer) {
   info("Options panel opened");
 
   info("Changing pref");
   let prefChanged = observer.once(PREF_MESSAGE_TIMESTAMP, () => {});
   let checkbox = panel.panelDoc.getElementById("webconsole-timestamp-messages");
   checkbox.click();
 
   yield prefChanged;
 }
 
-function* testChangedPref(outputEl) {
+function* testChangedPref(hud) {
   let prefValue = Services.prefs.getBoolPref(PREF_MESSAGE_TIMESTAMP);
   ok(prefValue, "Messages should have timestamps (pref check)");
-  ok(!outputEl.classList.contains("hideTimestamps"),
-     "Messages should have timestamps (class name check)");
+  let message = yield waitFor(() => findMessage(hud, "simple text message"));
+  is(message.querySelectorAll(".timestamp").length, 1,
+     "Messages should have timestamp (element check)");
 }