Merge "ES6ify /gr-diff-view/*"

This commit is contained in:
Kasper Nilsson
2017-05-17 22:50:07 +00:00
committed by Gerrit Code Review
2 changed files with 225 additions and 224 deletions

View File

@@ -14,17 +14,17 @@
(function() { (function() {
'use strict'; 'use strict';
var COMMIT_MESSAGE_PATH = '/COMMIT_MSG'; const COMMIT_MESSAGE_PATH = '/COMMIT_MSG';
var MERGE_LIST_PATH = '/MERGE_LIST'; const MERGE_LIST_PATH = '/MERGE_LIST';
var COMMENT_SAVE = 'Try again when all comments have saved.'; const COMMENT_SAVE = 'Try again when all comments have saved.';
var DiffSides = { const DiffSides = {
LEFT: 'left', LEFT: 'left',
RIGHT: 'right', RIGHT: 'right',
}; };
var HASH_PATTERN = /^[ab]?\d+$/; const HASH_PATTERN = /^[ab]?\d+$/;
Polymer({ Polymer({
is: 'gr-diff-view', is: 'gr-diff-view',
@@ -51,12 +51,12 @@
}, },
keyEventTarget: { keyEventTarget: {
type: Object, type: Object,
value: function() { return document.body; }, value() { return document.body; },
}, },
changeViewState: { changeViewState: {
type: Object, type: Object,
notify: true, notify: true,
value: function() { return {}; }, value() { return {}; },
}, },
_patchRange: Object, _patchRange: Object,
@@ -65,7 +65,7 @@
_diff: Object, _diff: Object,
_fileList: { _fileList: {
type: Array, type: Array,
value: function() { return []; }, value() { return []; },
}, },
_path: { _path: {
type: String, type: String,
@@ -134,18 +134,18 @@
',': '_handleCommaKey', ',': '_handleCommaKey',
}, },
attached: function() { attached() {
this._getLoggedIn().then(function(loggedIn) { this._getLoggedIn().then(loggedIn => {
this._loggedIn = loggedIn; this._loggedIn = loggedIn;
if (loggedIn) { if (loggedIn) {
this._setReviewed(true); this._setReviewed(true);
} }
}.bind(this)); });
if (this.changeViewState.diffMode === null) { if (this.changeViewState.diffMode === null) {
// If screen size is small, always default to unified view. // If screen size is small, always default to unified view.
this.$.restAPI.getPreferences().then(function(prefs) { this.$.restAPI.getPreferences().then(prefs => {
this.set('changeViewState.diffMode', prefs.default_diff_view); this.set('changeViewState.diffMode', prefs.default_diff_view);
}.bind(this)); });
} }
if (this._path) { if (this._path) {
@@ -156,63 +156,63 @@
this.$.cursor.push('diffs', this.$.diff); this.$.cursor.push('diffs', this.$.diff);
}, },
_getLoggedIn: function() { _getLoggedIn() {
return this.$.restAPI.getLoggedIn(); return this.$.restAPI.getLoggedIn();
}, },
_getProjectConfig: function(project) { _getProjectConfig(project) {
return this.$.restAPI.getProjectConfig(project).then( return this.$.restAPI.getProjectConfig(project).then(
function(config) { config => {
this._projectConfig = config; this._projectConfig = config;
}.bind(this)); });
}, },
_getChangeDetail: function(changeNum) { _getChangeDetail(changeNum) {
return this.$.restAPI.getDiffChangeDetail(changeNum).then( return this.$.restAPI.getDiffChangeDetail(changeNum).then(
function(change) { change => {
this._change = change; this._change = change;
}.bind(this)); });
}, },
_getFiles: function(changeNum, patchRangeRecord) { _getFiles(changeNum, patchRangeRecord) {
var patchRange = patchRangeRecord.base; const patchRange = patchRangeRecord.base;
return this.$.restAPI.getChangeFilePathsAsSpeciallySortedArray( return this.$.restAPI.getChangeFilePathsAsSpeciallySortedArray(
changeNum, patchRange).then(function(files) { changeNum, patchRange).then(files => {
this._fileList = files; this._fileList = files;
}.bind(this)); });
}, },
_getDiffPreferences: function() { _getDiffPreferences() {
return this.$.restAPI.getDiffPreferences(); return this.$.restAPI.getDiffPreferences();
}, },
_getPreferences: function() { _getPreferences() {
return this.$.restAPI.getPreferences(); return this.$.restAPI.getPreferences();
}, },
_getWindowWidth: function() { _getWindowWidth() {
return window.innerWidth; return window.innerWidth;
}, },
_handleReviewedChange: function(e) { _handleReviewedChange(e) {
this._setReviewed(Polymer.dom(e).rootTarget.checked); this._setReviewed(Polymer.dom(e).rootTarget.checked);
}, },
_setReviewed: function(reviewed) { _setReviewed(reviewed) {
this.$.reviewed.checked = reviewed; this.$.reviewed.checked = reviewed;
this._saveReviewedState(reviewed).catch(function(err) { this._saveReviewedState(reviewed).catch(err => {
alert('Couldnt change file review status. Check the console ' + alert('Couldnt change file review status. Check the console ' +
'and contact the PolyGerrit team for assistance.'); 'and contact the PolyGerrit team for assistance.');
throw err; throw err;
}.bind(this)); });
}, },
_saveReviewedState: function(reviewed) { _saveReviewedState(reviewed) {
return this.$.restAPI.saveFileReviewed(this._changeNum, return this.$.restAPI.saveFileReviewed(this._changeNum,
this._patchRange.patchNum, this._path, reviewed); this._patchRange.patchNum, this._path, reviewed);
}, },
_handleEscKey: function(e) { _handleEscKey(e) {
if (this.shouldSuppressKeyboardShortcut(e) || if (this.shouldSuppressKeyboardShortcut(e) ||
this.modifierPressed(e)) { return; } this.modifierPressed(e)) { return; }
@@ -220,21 +220,21 @@
this.$.diff.displayLine = false; this.$.diff.displayLine = false;
}, },
_handleShiftLeftKey: function(e) { _handleShiftLeftKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
e.preventDefault(); e.preventDefault();
this.$.cursor.moveLeft(); this.$.cursor.moveLeft();
}, },
_handleShiftRightKey: function(e) { _handleShiftRightKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
e.preventDefault(); e.preventDefault();
this.$.cursor.moveRight(); this.$.cursor.moveRight();
}, },
_handleUpKey: function(e) { _handleUpKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
if (e.detail.keyboardEvent.shiftKey && if (e.detail.keyboardEvent.shiftKey &&
e.detail.keyboardEvent.keyCode === 75) { // 'K' e.detail.keyboardEvent.keyCode === 75) { // 'K'
@@ -248,7 +248,7 @@
this.$.cursor.moveUp(); this.$.cursor.moveUp();
}, },
_handleDownKey: function(e) { _handleDownKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
if (e.detail.keyboardEvent.shiftKey && if (e.detail.keyboardEvent.shiftKey &&
e.detail.keyboardEvent.keyCode === 74) { // 'J' e.detail.keyboardEvent.keyCode === 74) { // 'J'
@@ -262,33 +262,33 @@
this.$.cursor.moveDown(); this.$.cursor.moveDown();
}, },
_moveToPreviousFileWithComment: function() { _moveToPreviousFileWithComment() {
if (this._commentSkips && this._commentSkips.previous) { if (this._commentSkips && this._commentSkips.previous) {
page.show(this._getDiffURL(this._changeNum, this._patchRange, page.show(this._getDiffURL(this._changeNum, this._patchRange,
this._commentSkips.previous)); this._commentSkips.previous));
} }
}, },
_moveToNextFileWithComment: function() { _moveToNextFileWithComment() {
if (this._commentSkips && this._commentSkips.next) { if (this._commentSkips && this._commentSkips.next) {
page.show(this._getDiffURL(this._changeNum, this._patchRange, page.show(this._getDiffURL(this._changeNum, this._patchRange,
this._commentSkips.next)); this._commentSkips.next));
} }
}, },
_handleCKey: function(e) { _handleCKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
if (this.$.diff.isRangeSelected()) { return; } if (this.$.diff.isRangeSelected()) { return; }
if (this.modifierPressed(e)) { return; } if (this.modifierPressed(e)) { return; }
e.preventDefault(); e.preventDefault();
var line = this.$.cursor.getTargetLineElement(); const line = this.$.cursor.getTargetLineElement();
if (line) { if (line) {
this.$.diff.addDraftAtLine(line); this.$.diff.addDraftAtLine(line);
} }
}, },
_handleLeftBracketKey: function(e) { _handleLeftBracketKey(e) {
if (this.shouldSuppressKeyboardShortcut(e) || if (this.shouldSuppressKeyboardShortcut(e) ||
this.modifierPressed(e)) { return; } this.modifierPressed(e)) { return; }
@@ -296,7 +296,7 @@
this._navToFile(this._path, this._fileList, -1); this._navToFile(this._path, this._fileList, -1);
}, },
_handleRightBracketKey: function(e) { _handleRightBracketKey(e) {
if (this.shouldSuppressKeyboardShortcut(e) || if (this.shouldSuppressKeyboardShortcut(e) ||
this.modifierPressed(e)) { return; } this.modifierPressed(e)) { return; }
@@ -304,7 +304,7 @@
this._navToFile(this._path, this._fileList, 1); this._navToFile(this._path, this._fileList, 1);
}, },
_handleNKey: function(e) { _handleNKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
e.preventDefault(); e.preventDefault();
@@ -316,7 +316,7 @@
} }
}, },
_handlePKey: function(e) { _handlePKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
e.preventDefault(); e.preventDefault();
@@ -328,7 +328,7 @@
} }
}, },
_handleAKey: function(e) { _handleAKey(e) {
if (this.shouldSuppressKeyboardShortcut(e)) { return; } if (this.shouldSuppressKeyboardShortcut(e)) { return; }
if (e.detail.keyboardEvent.shiftKey) { // Hide left diff. if (e.detail.keyboardEvent.shiftKey) { // Hide left diff.
@@ -351,7 +351,7 @@
this._navToChangeView(); this._navToChangeView();
}, },
_handleUKey: function(e) { _handleUKey(e) {
if (this.shouldSuppressKeyboardShortcut(e) || if (this.shouldSuppressKeyboardShortcut(e) ||
this.modifierPressed(e)) { return; } this.modifierPressed(e)) { return; }
@@ -359,7 +359,7 @@
this._navToChangeView(); this._navToChangeView();
}, },
_handleCommaKey: function(e) { _handleCommaKey(e) {
if (this.shouldSuppressKeyboardShortcut(e) || if (this.shouldSuppressKeyboardShortcut(e) ||
this.modifierPressed(e)) { return; } this.modifierPressed(e)) { return; }
@@ -367,7 +367,7 @@
this.$.diffPreferences.open(); this.$.diffPreferences.open();
}, },
_navToChangeView: function() { _navToChangeView() {
if (!this._changeNum || !this._patchRange.patchNum) { return; } if (!this._changeNum || !this._patchRange.patchNum) { return; }
page.show(this._getChangePath( page.show(this._getChangePath(
@@ -376,15 +376,15 @@
this._change && this._change.revisions)); this._change && this._change.revisions));
}, },
_computeUpURL: function(changeNum, patchRange, change, changeRevisions) { _computeUpURL(changeNum, patchRange, change, changeRevisions) {
return this._getChangePath( return this._getChangePath(
changeNum, changeNum,
patchRange, patchRange,
change && changeRevisions); change && changeRevisions);
}, },
_navToFile: function(path, fileList, direction) { _navToFile(path, fileList, direction) {
var url = this._computeNavLinkURL(path, fileList, direction); const url = this._computeNavLinkURL(path, fileList, direction);
if (!url) { return; } if (!url) { return; }
page.show(this._computeNavLinkURL(path, fileList, direction)); page.show(this._computeNavLinkURL(path, fileList, direction));
@@ -401,12 +401,14 @@
* @return {?string} The next URL when proceeding in the specified * @return {?string} The next URL when proceeding in the specified
* direction. * direction.
*/ */
_computeNavLinkURL: function(path, fileList, direction, opt_noUp) { _computeNavLinkURL(path, fileList, direction, opt_noUp) {
if (!path || fileList.length === 0) { return null; } if (!path || fileList.length === 0) { return null; }
var idx = fileList.indexOf(path); let idx = fileList.indexOf(path);
if (idx === -1) { if (idx === -1) {
var file = direction > 0 ? fileList[0] : fileList[fileList.length - 1]; const file = direction > 0 ?
fileList[0] :
fileList[fileList.length - 1];
return this._getDiffURL(this._changeNum, this._patchRange, file); return this._getDiffURL(this._changeNum, this._patchRange, file);
} }
@@ -423,7 +425,7 @@
return this._getDiffURL(this._changeNum, this._patchRange, fileList[idx]); return this._getDiffURL(this._changeNum, this._patchRange, fileList[idx]);
}, },
_paramsChanged: function(value) { _paramsChanged(value) {
if (value.view != this.tagName.toLowerCase()) { return; } if (value.view != this.tagName.toLowerCase()) { return; }
this._loadHash(location.hash); this._loadHash(location.hash);
@@ -445,33 +447,33 @@
return; return;
} }
var promises = []; const promises = [];
this._localPrefs = this.$.storage.getPreferences(); this._localPrefs = this.$.storage.getPreferences();
promises.push(this._getDiffPreferences().then(function(prefs) { promises.push(this._getDiffPreferences().then(prefs => {
this._prefs = prefs; this._prefs = prefs;
}.bind(this))); }));
promises.push(this._getPreferences().then(function(prefs) { promises.push(this._getPreferences().then(prefs => {
this._userPrefs = prefs; this._userPrefs = prefs;
}.bind(this))); }));
promises.push(this._getChangeDetail(this._changeNum)); promises.push(this._getChangeDetail(this._changeNum));
Promise.all(promises).then(function() { Promise.all(promises).then(() => {
this._loading = false; this._loading = false;
this.$.diff.reload(); this.$.diff.reload();
}.bind(this)); });
this._loadCommentMap().then(function(commentMap) { this._loadCommentMap().then(commentMap => {
this._commentMap = commentMap; this._commentMap = commentMap;
}.bind(this)); });
}, },
/** /**
* If the URL hash is a diff address then configure the diff cursor. * If the URL hash is a diff address then configure the diff cursor.
*/ */
_loadHash: function(hash) { _loadHash(hash) {
hash = hash.replace(/^#/, ''); hash = hash.replace(/^#/, '');
if (!HASH_PATTERN.test(hash)) { return; } if (!HASH_PATTERN.test(hash)) { return; }
if (hash[0] === 'a' || hash[0] === 'b') { if (hash[0] === 'a' || hash[0] === 'b') {
@@ -483,7 +485,7 @@
this.$.cursor.initialLineNumber = parseInt(hash, 10); this.$.cursor.initialLineNumber = parseInt(hash, 10);
}, },
_pathChanged: function(path) { _pathChanged(path) {
if (this._fileList.length == 0) { return; } if (this._fileList.length == 0) { return; }
this.set('changeViewState.selectedFileIndex', this.set('changeViewState.selectedFileIndex',
@@ -494,17 +496,17 @@
} }
}, },
_getDiffURL: function(changeNum, patchRange, path) { _getDiffURL(changeNum, patchRange, path) {
return this.getBaseUrl() + '/c/' + changeNum + '/' + return this.getBaseUrl() + '/c/' + changeNum + '/' +
this._patchRangeStr(patchRange) + '/' + this.encodeURL(path, true); this._patchRangeStr(patchRange) + '/' + this.encodeURL(path, true);
}, },
_computeDiffURL: function(changeNum, patchRangeRecord, path) { _computeDiffURL(changeNum, patchRangeRecord, path) {
return this._getDiffURL(changeNum, patchRangeRecord.base, path); return this._getDiffURL(changeNum, patchRangeRecord.base, path);
}, },
_patchRangeStr: function(patchRange) { _patchRangeStr(patchRange) {
var patchStr = patchRange.patchNum; let patchStr = patchRange.patchNum;
if (patchRange.basePatchNum != null && if (patchRange.basePatchNum != null &&
patchRange.basePatchNum != 'PARENT') { patchRange.basePatchNum != 'PARENT') {
patchStr = patchRange.basePatchNum + '..' + patchRange.patchNum; patchStr = patchRange.basePatchNum + '..' + patchRange.patchNum;
@@ -512,25 +514,25 @@
return patchStr; return patchStr;
}, },
_computeAvailablePatches: function(revisions) { _computeAvailablePatches(revisions) {
var patchNums = []; const patchNums = [];
for (var rev in revisions) { for (const rev of Object.values(revisions)) {
patchNums.push(revisions[rev]._number); patchNums.push(rev._number);
} }
return patchNums.sort(function(a, b) { return a - b; }); return patchNums.sort((a, b) => { return a - b; });
}, },
_getChangePath: function(changeNum, patchRange, revisions) { _getChangePath(changeNum, patchRange, revisions) {
var base = this.getBaseUrl() + '/c/' + changeNum + '/'; const base = this.getBaseUrl() + '/c/' + changeNum + '/';
// The change may not have loaded yet, making revisions unavailable. // The change may not have loaded yet, making revisions unavailable.
if (!revisions) { if (!revisions) {
return base + this._patchRangeStr(patchRange); return base + this._patchRangeStr(patchRange);
} }
var latestPatchNum = -1; let latestPatchNum = -1;
for (var rev in revisions) { for (const rev of Object.values(revisions)) {
latestPatchNum = Math.max(latestPatchNum, revisions[rev]._number); latestPatchNum = Math.max(latestPatchNum, rev._number);
} }
if (patchRange.basePatchNum !== 'PARENT' || if (patchRange.basePatchNum !== 'PARENT' ||
parseInt(patchRange.patchNum, 10) !== latestPatchNum) { parseInt(patchRange.patchNum, 10) !== latestPatchNum) {
@@ -540,11 +542,11 @@
return base; return base;
}, },
_computeChangePath: function(changeNum, patchRangeRecord, revisions) { _computeChangePath(changeNum, patchRangeRecord, revisions) {
return this._getChangePath(changeNum, patchRangeRecord.base, revisions); return this._getChangePath(changeNum, patchRangeRecord.base, revisions);
}, },
_computeFileDisplayName: function(path) { _computeFileDisplayName(path) {
if (path === COMMIT_MESSAGE_PATH) { if (path === COMMIT_MESSAGE_PATH) {
return 'Commit message'; return 'Commit message';
} else if (path === MERGE_LIST_PATH) { } else if (path === MERGE_LIST_PATH) {
@@ -553,20 +555,20 @@
return path; return path;
}, },
_computeTruncatedFileDisplayName: function(path) { _computeTruncatedFileDisplayName(path) {
return util.truncatePath(this._computeFileDisplayName(path)); return util.truncatePath(this._computeFileDisplayName(path));
}, },
_computeFileSelected: function(path, currentPath) { _computeFileSelected(path, currentPath) {
return path == currentPath; return path == currentPath;
}, },
_computePrefsButtonHidden: function(prefs, loggedIn) { _computePrefsButtonHidden(prefs, loggedIn) {
return !loggedIn || !prefs; return !loggedIn || !prefs;
}, },
_computeKeyNav: function(path, selectedPath, fileList) { _computeKeyNav(path, selectedPath, fileList) {
var selectedIndex = fileList.indexOf(selectedPath); const selectedIndex = fileList.indexOf(selectedPath);
if (fileList.indexOf(path) == selectedIndex - 1) { if (fileList.indexOf(path) == selectedIndex - 1) {
return '['; return '[';
} }
@@ -576,41 +578,41 @@
return ''; return '';
}, },
_handleFileTap: function(e) { _handleFileTap(e) {
// async is needed so that that the click event is fired before the // async is needed so that that the click event is fired before the
// dropdown closes (This was a bug for touch devices). // dropdown closes (This was a bug for touch devices).
this.async(function() { this.async(() => {
this.$.dropdown.close(); this.$.dropdown.close();
}, 1); }, 1);
}, },
_handleMobileSelectChange: function(e) { _handleMobileSelectChange(e) {
var path = Polymer.dom(e).rootTarget.value; const path = Polymer.dom(e).rootTarget.value;
page.show(this._getDiffURL(this._changeNum, this._patchRange, path)); page.show(this._getDiffURL(this._changeNum, this._patchRange, path));
}, },
_showDropdownTapHandler: function(e) { _showDropdownTapHandler(e) {
this.$.dropdown.open(); this.$.dropdown.open();
}, },
_handlePrefsTap: function(e) { _handlePrefsTap(e) {
e.preventDefault(); e.preventDefault();
this.$.diffPreferences.open(); this.$.diffPreferences.open();
}, },
_handlePrefsSave: function(e) { _handlePrefsSave(e) {
e.stopPropagation(); e.stopPropagation();
var el = Polymer.dom(e).rootTarget; const el = Polymer.dom(e).rootTarget;
el.disabled = true; el.disabled = true;
this.$.storage.savePreferences(this._localPrefs); this.$.storage.savePreferences(this._localPrefs);
this._saveDiffPreferences().then(function(response) { this._saveDiffPreferences().then(response => {
el.disabled = false; el.disabled = false;
if (!response.ok) { return response; } if (!response.ok) { return response; }
this.$.prefsOverlay.close(); this.$.prefsOverlay.close();
}.bind(this)).catch(function(err) { }).catch(err => {
el.disabled = false; el.disabled = false;
}.bind(this)); });
}, },
/** /**
@@ -627,7 +629,7 @@
* *
* @return {String} * @return {String}
*/ */
_getDiffViewMode: function() { _getDiffViewMode() {
if (this.changeViewState.diffMode) { if (this.changeViewState.diffMode) {
return this.changeViewState.diffMode; return this.changeViewState.diffMode;
} else if (this._userPrefs) { } else if (this._userPrefs) {
@@ -638,17 +640,17 @@
} }
}, },
_computeModeSelectHidden: function() { _computeModeSelectHidden() {
return this._isImageDiff; return this._isImageDiff;
}, },
_onLineSelected: function(e, detail) { _onLineSelected(e, detail) {
this.$.cursor.moveToLineNumber(detail.number, detail.side); this.$.cursor.moveToLineNumber(detail.number, detail.side);
history.replaceState(null, null, '#' + this.$.cursor.getAddress()); history.replaceState(null, null, '#' + this.$.cursor.getAddress());
}, },
_computeDownloadLink: function(changeNum, patchRange, path) { _computeDownloadLink(changeNum, patchRange, path) {
var url = this.changeBaseURL(changeNum, patchRange.patchNum); let url = this.changeBaseURL(changeNum, patchRange.patchNum);
url += '/patch?zip&path=' + encodeURIComponent(path); url += '/patch?zip&path=' + encodeURIComponent(path);
return url; return url;
}, },
@@ -659,9 +661,9 @@
* current patch range. * current patch range.
* @return {Promise} A promise that yields a comment map object. * @return {Promise} A promise that yields a comment map object.
*/ */
_loadCommentMap: function() { _loadCommentMap() {
function filterByRange(comment) { const filterByRange = comment => {
var patchNum = comment.patch_set + ''; const patchNum = comment.patch_set + '';
return patchNum === this._patchRange.patchNum || return patchNum === this._patchRange.patchNum ||
patchNum === this._patchRange.basePatchNum; patchNum === this._patchRange.basePatchNum;
}; };
@@ -670,34 +672,34 @@
this.$.restAPI.getDiffComments(this._changeNum), this.$.restAPI.getDiffComments(this._changeNum),
this._getDiffDrafts(), this._getDiffDrafts(),
this.$.restAPI.getDiffRobotComments(this._changeNum), this.$.restAPI.getDiffRobotComments(this._changeNum),
]).then(function(results) { ]).then(results => {
var commentMap = {}; const commentMap = {};
results.forEach(function(response) { for (const response of results) {
for (var path in response) { for (const path in response) {
if (response.hasOwnProperty(path) && if (response.hasOwnProperty(path) &&
response[path].filter(filterByRange.bind(this)).length) { response[path].filter(filterByRange).length) {
commentMap[path] = true; commentMap[path] = true;
} }
} }
}.bind(this)); }
return commentMap; return commentMap;
}.bind(this)); });
}, },
_getDiffDrafts: function() { _getDiffDrafts() {
return this._getLoggedIn().then(function(loggedIn) { return this._getLoggedIn().then(loggedIn => {
if (!loggedIn) { return Promise.resolve({}); } if (!loggedIn) { return Promise.resolve({}); }
return this.$.restAPI.getDiffDrafts(this._changeNum); return this.$.restAPI.getDiffDrafts(this._changeNum);
}.bind(this)); });
}, },
_computeCommentSkips: function(commentMap, fileList, path) { _computeCommentSkips(commentMap, fileList, path) {
var skips = {previous: null, next: null}; const skips = {previous: null, next: null};
if (!fileList.length) { return skips; } if (!fileList.length) { return skips; }
var pathIndex = fileList.indexOf(path); const pathIndex = fileList.indexOf(path);
// Scan backward for the previous file. // Scan backward for the previous file.
for (var i = pathIndex - 1; i >= 0; i--) { for (let i = pathIndex - 1; i >= 0; i--) {
if (commentMap[fileList[i]]) { if (commentMap[fileList[i]]) {
skips.previous = fileList[i]; skips.previous = fileList[i];
break; break;
@@ -705,7 +707,7 @@
} }
// Scan forward for the next file. // Scan forward for the next file.
for (i = pathIndex + 1; i < fileList.length; i++) { for (let i = pathIndex + 1; i < fileList.length; i++) {
if (commentMap[fileList[i]]) { if (commentMap[fileList[i]]) {
skips.next = fileList[i]; skips.next = fileList[i];
break; break;

View File

@@ -41,34 +41,34 @@ limitations under the License.
</test-fixture> </test-fixture>
<script> <script>
suite('gr-diff-view tests', function() { suite('gr-diff-view tests', () => {
var element; let element;
var sandbox; let sandbox;
setup(function() { setup(() => {
sandbox = sinon.sandbox.create(); sandbox = sinon.sandbox.create();
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getLoggedIn: function() { return Promise.resolve(false); }, getLoggedIn() { return Promise.resolve(false); },
getProjectConfig: function() { return Promise.resolve({}); }, getProjectConfig() { return Promise.resolve({}); },
getDiffChangeDetail: function() { return Promise.resolve(null); }, getDiffChangeDetail() { return Promise.resolve(null); },
getChangeFiles: function() { return Promise.resolve({}); }, getChangeFiles() { return Promise.resolve({}); },
saveFileReviewed: function() { return Promise.resolve(); }, saveFileReviewed() { return Promise.resolve(); },
}); });
element = fixture('basic'); element = fixture('basic');
}); });
teardown(function() { teardown(() => {
sandbox.restore(); sandbox.restore();
}); });
test('toggle left diff with a hotkey', function() { test('toggle left diff with a hotkey', () => {
var toggleLeftDiffStub = sandbox.stub(element.$.diff, 'toggleLeftDiff'); const toggleLeftDiffStub = sandbox.stub(element.$.diff, 'toggleLeftDiff');
MockInteractions.pressAndReleaseKeyOn(element, 65, 'shift', 'a'); MockInteractions.pressAndReleaseKeyOn(element, 65, 'shift', 'a');
assert.isTrue(toggleLeftDiffStub.calledOnce); assert.isTrue(toggleLeftDiffStub.calledOnce);
}); });
test('keyboard shortcuts', function() { test('keyboard shortcuts', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: 'PARENT', basePatchNum: 'PARENT',
@@ -83,7 +83,7 @@ limitations under the License.
element._path = 'glados.txt'; element._path = 'glados.txt';
element.changeViewState.selectedFileIndex = 1; element.changeViewState.selectedFileIndex = 1;
var showStub = sandbox.stub(page, 'show'); const showStub = sandbox.stub(page, 'show');
MockInteractions.pressAndReleaseKeyOn(element, 85, null, 'u'); MockInteractions.pressAndReleaseKeyOn(element, 85, null, 'u');
assert(showStub.lastCall.calledWithExactly('/c/42/'), assert(showStub.lastCall.calledWithExactly('/c/42/'),
'Should navigate to /c/42/'); 'Should navigate to /c/42/');
@@ -111,13 +111,14 @@ limitations under the License.
'Should navigate to /c/42/'); 'Should navigate to /c/42/');
assert.equal(element.changeViewState.selectedFileIndex, 0); assert.equal(element.changeViewState.selectedFileIndex, 0);
var showPrefsStub = sandbox.stub(element.$.diffPreferences.$.prefsOverlay, const showPrefsStub =
'open', function() { return Promise.resolve({}); }); sandbox.stub(element.$.diffPreferences.$.prefsOverlay, 'open',
() => Promise.resolve());
MockInteractions.pressAndReleaseKeyOn(element, 188, null, ','); MockInteractions.pressAndReleaseKeyOn(element, 188, null, ',');
assert(showPrefsStub.calledOnce); assert(showPrefsStub.calledOnce);
var scrollStub = sandbox.stub(element.$.cursor, 'moveToNextChunk'); let scrollStub = sandbox.stub(element.$.cursor, 'moveToNextChunk');
MockInteractions.pressAndReleaseKeyOn(element, 78, null, 'n'); MockInteractions.pressAndReleaseKeyOn(element, 78, null, 'n');
assert(scrollStub.calledOnce); assert(scrollStub.calledOnce);
@@ -134,7 +135,7 @@ limitations under the License.
MockInteractions.pressAndReleaseKeyOn(element, 80, 'shift', 'p'); MockInteractions.pressAndReleaseKeyOn(element, 80, 'shift', 'p');
assert(scrollStub.calledOnce); assert(scrollStub.calledOnce);
var computeContainerClassStub = sandbox.stub(element.$.diff, const computeContainerClassStub = sandbox.stub(element.$.diff,
'_computeContainerClass'); '_computeContainerClass');
MockInteractions.pressAndReleaseKeyOn(element, 74, null, 'j'); MockInteractions.pressAndReleaseKeyOn(element, 74, null, 'j');
assert(computeContainerClassStub.lastCall.calledWithExactly( assert(computeContainerClassStub.lastCall.calledWithExactly(
@@ -145,7 +146,7 @@ limitations under the License.
false, 'SIDE_BY_SIDE', false)); false, 'SIDE_BY_SIDE', false));
}); });
test('keyboard shortcuts with patch range', function() { test('keyboard shortcuts with patch range', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: '5', basePatchNum: '5',
@@ -159,7 +160,7 @@ limitations under the License.
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md']; element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt'; element._path = 'glados.txt';
var showStub = sandbox.stub(page, 'show'); const showStub = sandbox.stub(page, 'show');
MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a'); MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' + assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' +
@@ -198,7 +199,7 @@ limitations under the License.
'Should navigate to /c/42/5..10'); 'Should navigate to /c/42/5..10');
}); });
test('keyboard shortcuts with old patch number', function() { test('keyboard shortcuts with old patch number', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: 'PARENT', basePatchNum: 'PARENT',
@@ -213,7 +214,7 @@ limitations under the License.
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md']; element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt'; element._path = 'glados.txt';
var showStub = sandbox.stub(page, 'show'); const showStub = sandbox.stub(page, 'show');
MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a'); MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' + assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' +
@@ -252,8 +253,7 @@ limitations under the License.
'Should navigate to /c/42/1'); 'Should navigate to /c/42/1');
}); });
test('Diff preferences hidden when no prefs or logged out', test('Diff preferences hidden when no prefs or logged out', () => {
function() {
element._loggedIn = false; element._loggedIn = false;
flushAsynchronousOperations(); flushAsynchronousOperations();
assert.isTrue(element.$.diffPrefsContainer.hidden); assert.isTrue(element.$.diffPrefsContainer.hidden);
@@ -263,7 +263,7 @@ limitations under the License.
assert.isTrue(element.$.diffPrefsContainer.hidden); assert.isTrue(element.$.diffPrefsContainer.hidden);
element._loggedIn = false; element._loggedIn = false;
element._prefs = {'font_size': '12'}; element._prefs = {font_size: '12'};
flushAsynchronousOperations(); flushAsynchronousOperations();
assert.isTrue(element.$.diffPrefsContainer.hidden); assert.isTrue(element.$.diffPrefsContainer.hidden);
@@ -272,11 +272,12 @@ limitations under the License.
assert.isFalse(element.$.diffPrefsContainer.hidden); assert.isFalse(element.$.diffPrefsContainer.hidden);
}); });
test('prefsButton opens gr-diff-preferences', function() { test('prefsButton opens gr-diff-preferences', () => {
var handlePrefsTapSpy = sandbox.spy(element, '_handlePrefsTap'); const handlePrefsTapSpy = sandbox.spy(element, '_handlePrefsTap');
var overlayOpenStub = sandbox.stub(element.$.diffPreferences, const overlayOpenStub = sandbox.stub(element.$.diffPreferences,
'open'); 'open');
var prefsButton = Polymer.dom(element.root).querySelector('.prefsButton'); const prefsButton =
Polymer.dom(element.root).querySelector('.prefsButton');
MockInteractions.tap(prefsButton); MockInteractions.tap(prefsButton);
@@ -284,7 +285,7 @@ limitations under the License.
assert.isTrue(overlayOpenStub.called); assert.isTrue(overlayOpenStub.called);
}); });
test('go up to change via kb without change loaded', function() { test('go up to change via kb without change loaded', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: 'PARENT', basePatchNum: 'PARENT',
@@ -294,7 +295,7 @@ limitations under the License.
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md']; element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt'; element._path = 'glados.txt';
var showStub = sandbox.stub(page, 'show'); const showStub = sandbox.stub(page, 'show');
MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a'); MockInteractions.pressAndReleaseKeyOn(element, 65, null, 'a');
assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' + assert.isTrue(showStub.notCalled, 'The `a` keyboard shortcut should ' +
@@ -333,7 +334,7 @@ limitations under the License.
'Should navigate to /c/42/1'); 'Should navigate to /c/42/1');
}); });
test('jump to file dropdown', function() { test('jump to file dropdown', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: 'PARENT', basePatchNum: 'PARENT',
@@ -342,7 +343,7 @@ limitations under the License.
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md']; element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt'; element._path = 'glados.txt';
flushAsynchronousOperations(); flushAsynchronousOperations();
var linkEls = const linkEls =
Polymer.dom(element.root).querySelectorAll('.dropdown-content > a'); Polymer.dom(element.root).querySelectorAll('.dropdown-content > a');
assert.equal(linkEls.length, 3); assert.equal(linkEls.length, 3);
assert.isFalse(linkEls[0].hasAttribute('selected')); assert.isFalse(linkEls[0].hasAttribute('selected'));
@@ -363,7 +364,7 @@ limitations under the License.
'Merge list'); 'Merge list');
}); });
test('jump to file dropdown with patch range', function() { test('jump to file dropdown with patch range', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: '5', basePatchNum: '5',
@@ -372,7 +373,7 @@ limitations under the License.
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md']; element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt'; element._path = 'glados.txt';
flushAsynchronousOperations(); flushAsynchronousOperations();
var linkEls = const linkEls =
Polymer.dom(element.root).querySelectorAll('.dropdown-content > a'); Polymer.dom(element.root).querySelectorAll('.dropdown-content > a');
assert.equal(linkEls.length, 3); assert.equal(linkEls.length, 3);
assert.isFalse(linkEls[0].hasAttribute('selected')); assert.isFalse(linkEls[0].hasAttribute('selected'));
@@ -386,7 +387,7 @@ limitations under the License.
assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/wheatley.md'); assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/wheatley.md');
}); });
test('prev/up/next links', function() { test('prev/up/next links', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: 'PARENT', basePatchNum: 'PARENT',
@@ -400,7 +401,7 @@ limitations under the License.
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md']; element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt'; element._path = 'glados.txt';
flushAsynchronousOperations(); flushAsynchronousOperations();
var linkEls = Polymer.dom(element.root).querySelectorAll('.navLink'); const linkEls = Polymer.dom(element.root).querySelectorAll('.navLink');
assert.equal(linkEls.length, 3); assert.equal(linkEls.length, 3);
assert.equal(linkEls[0].getAttribute('href'), '/c/42/10/chell.go'); assert.equal(linkEls[0].getAttribute('href'), '/c/42/10/chell.go');
assert.equal(linkEls[1].getAttribute('href'), '/c/42/'); assert.equal(linkEls[1].getAttribute('href'), '/c/42/');
@@ -422,7 +423,7 @@ limitations under the License.
assert.equal(linkEls[2].getAttribute('href'), '/c/42/10/chell.go'); assert.equal(linkEls[2].getAttribute('href'), '/c/42/10/chell.go');
}); });
test('download link', function() { test('download link', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: 'PARENT', basePatchNum: 'PARENT',
@@ -435,7 +436,7 @@ limitations under the License.
'/changes/42/revisions/10/patch?zip&path=glados.txt'); '/changes/42/revisions/10/patch?zip&path=glados.txt');
}); });
test('prev/up/next links with patch range', function() { test('prev/up/next links with patch range', () => {
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
basePatchNum: '5', basePatchNum: '5',
@@ -450,7 +451,7 @@ limitations under the License.
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md']; element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt'; element._path = 'glados.txt';
flushAsynchronousOperations(); flushAsynchronousOperations();
var linkEls = Polymer.dom(element.root).querySelectorAll('.navLink'); const linkEls = Polymer.dom(element.root).querySelectorAll('.navLink');
assert.equal(linkEls.length, 3); assert.equal(linkEls.length, 3);
assert.equal(linkEls[0].getAttribute('href'), '/c/42/5..10/chell.go'); assert.equal(linkEls[0].getAttribute('href'), '/c/42/5..10/chell.go');
assert.equal(linkEls[1].getAttribute('href'), '/c/42/5..10'); assert.equal(linkEls[1].getAttribute('href'), '/c/42/5..10');
@@ -467,7 +468,7 @@ limitations under the License.
assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/glados.txt'); assert.equal(linkEls[2].getAttribute('href'), '/c/42/5..10/glados.txt');
}); });
test('file review status', function(done) { test('file review status', done => {
element._loggedIn = true; element._loggedIn = true;
element._changeNum = '42'; element._changeNum = '42';
element._patchRange = { element._patchRange = {
@@ -476,11 +477,11 @@ limitations under the License.
}; };
element._fileList = ['/COMMIT_MSG']; element._fileList = ['/COMMIT_MSG'];
element._path = '/COMMIT_MSG'; element._path = '/COMMIT_MSG';
var saveReviewedStub = sandbox.stub(element, '_saveReviewedState', const saveReviewedStub = sandbox.stub(element, '_saveReviewedState',
function() { return Promise.resolve(); }); () => Promise.resolve());
flush(function() { flush(() => {
var commitMsg = Polymer.dom(element.root).querySelector( const commitMsg = Polymer.dom(element.root).querySelector(
'input[type="checkbox"]'); 'input[type="checkbox"]');
assert.isTrue(commitMsg.checked); assert.isTrue(commitMsg.checked);
@@ -496,9 +497,9 @@ limitations under the License.
}); });
}); });
test('diff mode selector correctly toggles the diff', function() { test('diff mode selector correctly toggles the diff', () => {
var select = element.$.modeSelect; const select = element.$.modeSelect;
var diffDisplay = element.$.diff; const diffDisplay = element.$.diff;
element._userPrefs = {default_diff_view: 'SIDE_BY_SIDE'}; element._userPrefs = {default_diff_view: 'SIDE_BY_SIDE'};
// The mode selected in the view state reflects the selected option. // The mode selected in the view state reflects the selected option.
@@ -509,7 +510,7 @@ limitations under the License.
assert.equal(select.value, diffDisplay.viewMode); assert.equal(select.value, diffDisplay.viewMode);
// We will simulate a user change of the selected mode. // We will simulate a user change of the selected mode.
var newMode = 'UNIFIED_DIFF'; const newMode = 'UNIFIED_DIFF';
// Set the actual value of the select, and simulate the change event. // Set the actual value of the select, and simulate the change event.
select.value = newMode; select.value = newMode;
element.fire('change', {}, {node: select}); element.fire('change', {}, {node: select});
@@ -520,17 +521,16 @@ limitations under the License.
assert.equal(element._getDiffViewMode(), diffDisplay.viewMode); assert.equal(element._getDiffViewMode(), diffDisplay.viewMode);
}); });
test('diff mode selector initializes from preferences', function() { test('diff mode selector initializes from preferences', () => {
var resolvePrefs; let resolvePrefs;
var prefsPromise = new Promise(function(resolve) { const prefsPromise = new Promise(resolve => {
resolvePrefs = resolve; resolvePrefs = resolve;
}); });
var getPreferencesStub = sandbox.stub(element.$.restAPI, 'getPreferences', sandbox.stub(element.$.restAPI, 'getPreferences', () => prefsPromise);
function() { return prefsPromise; });
// Attach a new gr-diff-view so we can intercept the preferences fetch. // Attach a new gr-diff-view so we can intercept the preferences fetch.
var view = document.createElement('gr-diff-view'); const view = document.createElement('gr-diff-view');
var select = view.$.modeSelect; const select = view.$.modeSelect;
fixture('blank').appendChild(view); fixture('blank').appendChild(view);
flushAsynchronousOperations(); flushAsynchronousOperations();
@@ -543,7 +543,7 @@ limitations under the License.
assert.equal(select.value, 'SIDE_BY_SIDE'); assert.equal(select.value, 'SIDE_BY_SIDE');
}); });
test('_loadHash', function() { test('_loadHash', () => {
assert.isNotOk(element.$.cursor.initialLineNumber); assert.isNotOk(element.$.cursor.initialLineNumber);
// Ignores invalid hashes: // Ignores invalid hashes:
@@ -566,32 +566,31 @@ limitations under the License.
assert.equal(element.$.cursor.side, 'left'); assert.equal(element.$.cursor.side, 'left');
}); });
test('_shortenPath with long path should add ellipsis', function() { test('_shortenPath with long path should add ellipsis', () => {
var path = let path = 'level1/level2/level3/level4/file.js';
'level1/level2/level3/level4/file.js'; let shortenedPath = util.truncatePath(path);
var shortenedPath = util.truncatePath(path);
// The expected path is truncated with an ellipsis. // The expected path is truncated with an ellipsis.
var expectedPath = '\u2026/file.js'; const expectedPath = '\u2026/file.js';
assert.equal(shortenedPath, expectedPath); assert.equal(shortenedPath, expectedPath);
var path = 'level2/file.js'; path = 'level2/file.js';
var shortenedPath = util.truncatePath(path); shortenedPath = util.truncatePath(path);
assert.equal(shortenedPath, expectedPath); assert.equal(shortenedPath, expectedPath);
}); });
test('_shortenPath with short path should not add ellipsis', function() { test('_shortenPath with short path should not add ellipsis', () => {
var path = 'file.js'; const path = 'file.js';
var expectedPath = 'file.js'; const expectedPath = 'file.js';
var shortenedPath = util.truncatePath(path); const shortenedPath = util.truncatePath(path);
assert.equal(shortenedPath, expectedPath); assert.equal(shortenedPath, expectedPath);
}); });
test('_onLineSelected', function() { test('_onLineSelected', () => {
var replaceStateStub = sandbox.stub(history, 'replaceState'); const replaceStateStub = sandbox.stub(history, 'replaceState');
var moveStub = sandbox.stub(element.$.cursor, 'moveToLineNumber'); const moveStub = sandbox.stub(element.$.cursor, 'moveToLineNumber');
var e = {}; const e = {};
var detail = {number: 123, side: 'right'}; const detail = {number: 123, side: 'right'};
element._onLineSelected(e, detail); element._onLineSelected(e, detail);
@@ -602,15 +601,15 @@ limitations under the License.
assert.isTrue(replaceStateStub.called); assert.isTrue(replaceStateStub.called);
}); });
test('_getDiffURL encodes special characters', function() { test('_getDiffURL encodes special characters', () => {
var changeNum = 123; const changeNum = 123;
var patchRange = {basePatchNum: 123, patchNum: 456}; const patchRange = {basePatchNum: 123, patchNum: 456};
var path = 'c++/cpp.cpp'; const path = 'c++/cpp.cpp';
assert.equal(element._getDiffURL(changeNum, patchRange, path), assert.equal(element._getDiffURL(changeNum, patchRange, path),
'/c/123/123..456/c%252B%252B/cpp.cpp'); '/c/123/123..456/c%252B%252B/cpp.cpp');
}); });
test('_getDiffViewMode', function() { test('_getDiffViewMode', () => {
// No user prefs or change view state set. // No user prefs or change view state set.
assert.equal(element._getDiffViewMode(), 'SIDE_BY_SIDE'); assert.equal(element._getDiffViewMode(), 'SIDE_BY_SIDE');
@@ -623,22 +622,22 @@ limitations under the License.
assert.equal(element._getDiffViewMode(), 'SIDE_BY_SIDE'); assert.equal(element._getDiffViewMode(), 'SIDE_BY_SIDE');
}); });
suite('_loadCommentMap', function() { suite('_loadCommentMap', () => {
test('empty', function(done) { test('empty', done => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getDiffRobotComments: function() { return Promise.resolve({}); }, getDiffRobotComments() { return Promise.resolve({}); },
getDiffComments: function() { return Promise.resolve({}); }, getDiffComments() { return Promise.resolve({}); },
}); });
element._loadCommentMap().then(function(map) { element._loadCommentMap().then(map => {
assert.equal(Object.keys(map).length, 0); assert.equal(Object.keys(map).length, 0);
done(); done();
}); });
}); });
test('paths in patch range', function(done) { test('paths in patch range', done => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getDiffRobotComments: function() { return Promise.resolve({}); }, getDiffRobotComments() { return Promise.resolve({}); },
getDiffComments: function() { getDiffComments() {
return Promise.resolve({ return Promise.resolve({
'path/to/file/one.cpp': [{patch_set: 3, message: 'lorem'}], 'path/to/file/one.cpp': [{patch_set: 3, message: 'lorem'}],
'path-to/file/two.py': [{patch_set: 5, message: 'ipsum'}], 'path-to/file/two.py': [{patch_set: 5, message: 'ipsum'}],
@@ -650,17 +649,17 @@ limitations under the License.
basePatchNum: '3', basePatchNum: '3',
patchNum: '5', patchNum: '5',
}; };
element._loadCommentMap().then(function(map) { element._loadCommentMap().then(map => {
assert.deepEqual(Object.keys(map), assert.deepEqual(Object.keys(map),
['path/to/file/one.cpp', 'path-to/file/two.py']); ['path/to/file/one.cpp', 'path-to/file/two.py']);
done(); done();
}); });
}); });
test('empty for paths outside patch range', function(done) { test('empty for paths outside patch range', done => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getDiffRobotComments: function() { return Promise.resolve({}); }, getDiffRobotComments() { return Promise.resolve({}); },
getDiffComments: function() { getDiffComments() {
return Promise.resolve({ return Promise.resolve({
'path/to/file/one.cpp': [{patch_set: 'PARENT', message: 'lorem'}], 'path/to/file/one.cpp': [{patch_set: 'PARENT', message: 'lorem'}],
'path-to/file/two.py': [{patch_set: 2, message: 'ipsum'}], 'path-to/file/two.py': [{patch_set: 2, message: 'ipsum'}],
@@ -672,35 +671,35 @@ limitations under the License.
basePatchNum: '3', basePatchNum: '3',
patchNum: '5', patchNum: '5',
}; };
element._loadCommentMap().then(function(map) { element._loadCommentMap().then(map => {
assert.equal(Object.keys(map).length, 0); assert.equal(Object.keys(map).length, 0);
done(); done();
}); });
}); });
}); });
suite('_computeCommentSkips', function() { suite('_computeCommentSkips', () => {
test('empty file list', function() { test('empty file list', () => {
var commentMap = { const commentMap = {
'path/one.jpg': true, 'path/one.jpg': true,
'path/three.wav': true, 'path/three.wav': true,
}; };
var path = 'path/two.m4v'; const path = 'path/two.m4v';
var fileList = []; const fileList = [];
var result = element._computeCommentSkips(commentMap, fileList, path); const result = element._computeCommentSkips(commentMap, fileList, path);
assert.isNull(result.previous); assert.isNull(result.previous);
assert.isNull(result.next); assert.isNull(result.next);
}); });
test('finds skips', function() { test('finds skips', () => {
var fileList = ['path/one.jpg', 'path/two.m4v', 'path/three.wav']; const fileList = ['path/one.jpg', 'path/two.m4v', 'path/three.wav'];
var path = fileList[1]; let path = fileList[1];
var commentMap = {}; const commentMap = {};
commentMap[fileList[0]] = true; commentMap[fileList[0]] = true;
commentMap[fileList[1]] = false; commentMap[fileList[1]] = false;
commentMap[fileList[2]] = true; commentMap[fileList[2]] = true;
var result = element._computeCommentSkips(commentMap, fileList, path); let result = element._computeCommentSkips(commentMap, fileList, path);
assert.equal(result.previous, fileList[0]); assert.equal(result.previous, fileList[0]);
assert.equal(result.next, fileList[2]); assert.equal(result.next, fileList[2]);