Bug 1463676 - Don't add an evaluated input to the history if it is the same as the previous evaluation; r=nchevobbe draft
authorJan Odvarko <odvarko@gmail.com>
Mon, 09 Jul 2018 11:02:11 +0200
changeset 815538 fe01390005d814c822038cb6734772b5f645e199
parent 815516 ffb7b5015fc331bdc4c5e6ab52b9de669faa8864
push id115538
push userjodvarko@mozilla.com
push dateMon, 09 Jul 2018 09:03:23 +0000
reviewersnchevobbe
bugs1463676
milestone63.0a1
Bug 1463676 - Don't add an evaluated input to the history if it is the same as the previous evaluation; r=nchevobbe MozReview-Commit-ID: DgtYwyPyTL9
devtools/client/webconsole/reducers/history.js
devtools/client/webconsole/test/mochitest/browser_jsterm_add_edited_input_to_history.js
--- a/devtools/client/webconsole/reducers/history.js
+++ b/devtools/client/webconsole/reducers/history.js
@@ -48,18 +48,26 @@ function history(state = getInitialState
   return state;
 }
 
 function appendToHistory(state, prefsState, expression) {
   // Clone state
   state = {...state};
   state.entries = [...state.entries];
 
-  // Append new expression
-  state.entries[state.index++] = expression;
+  // Append new expression only if it isn't the same as
+  // the one recently added.
+  // If it's the same don't forget to remove the current
+  // input value that has been appended in updatePlaceholder.
+  if (expression.trim() != state.entries[state.index - 1]) {
+    state.entries[state.index++] = expression;
+  } else if (state.index < state.entries.length) {
+    state.entries.pop();
+  }
+
   state.placeHolder = state.entries.length;
 
   // Remove entries if the limit is reached
   if (state.entries.length > prefsState.historyCount) {
     state.entries.splice(0, state.entries.length - prefsState.historyCount);
     state.index = state.placeHolder = state.entries.length;
   }
 
--- a/devtools/client/webconsole/test/mochitest/browser_jsterm_add_edited_input_to_history.js
+++ b/devtools/client/webconsole/test/mochitest/browser_jsterm_add_edited_input_to_history.js
@@ -47,9 +47,21 @@ async function testEditedInputHistory(hu
   is(jsterm.getInputValue(), '"second item"', "test history up");
   EventUtils.synthesizeKey("KEY_ArrowUp");
   is(jsterm.getInputValue(), '"first item"', "test history up");
   EventUtils.synthesizeKey("KEY_ArrowDown");
   is(jsterm.getInputValue(), '"second item"', "test history down");
   EventUtils.synthesizeKey("KEY_ArrowDown");
   is(jsterm.getInputValue(), '"editing input 2"',
      "test history down restores new in-progress input again");
+
+  // Appending the same value again should not impact the history.
+  // Let's also use same spaces around to check that the input value
+  // is properly trimmed.
+  await jsterm.execute('"second item"');
+  await jsterm.execute('  "second item"    ');
+  EventUtils.synthesizeKey("KEY_ArrowUp");
+  is(jsterm.getInputValue(), '"second item"',
+    "test history up reaches duplicated entry just once");
+  EventUtils.synthesizeKey("KEY_ArrowUp");
+  is(jsterm.getInputValue(), '"first item"',
+    "test history up reaches the previous value");
 }