ES6ify /gr-diff/*
Bug: Issue 6179 Change-Id: I198da73316e0c5cb7cda91cf845a174667eee2bb
This commit is contained in:
@@ -44,7 +44,7 @@
|
||||
GrDiffGroup.prototype.addLine = function(line) {
|
||||
this.lines.push(line);
|
||||
|
||||
var notDelta = (this.type === GrDiffGroup.Type.BOTH ||
|
||||
const notDelta = (this.type === GrDiffGroup.Type.BOTH ||
|
||||
this.type === GrDiffGroup.Type.CONTEXT_CONTROL);
|
||||
if (notDelta && (line.type === GrDiffLine.Type.ADD ||
|
||||
line.type === GrDiffLine.Type.REMOVE)) {
|
||||
@@ -62,7 +62,7 @@
|
||||
GrDiffGroup.prototype.getSideBySidePairs = function() {
|
||||
if (this.type === GrDiffGroup.Type.BOTH ||
|
||||
this.type === GrDiffGroup.Type.CONTEXT_CONTROL) {
|
||||
return this.lines.map(function(line) {
|
||||
return this.lines.map(line => {
|
||||
return {
|
||||
left: line,
|
||||
right: line,
|
||||
@@ -70,9 +70,9 @@
|
||||
});
|
||||
}
|
||||
|
||||
var pairs = [];
|
||||
var i = 0;
|
||||
var j = 0;
|
||||
const pairs = [];
|
||||
let i = 0;
|
||||
let j = 0;
|
||||
while (i < this.removes.length || j < this.adds.length) {
|
||||
pairs.push({
|
||||
left: this.removes[i] || GrDiffLine.BLANK_LINE,
|
||||
|
||||
@@ -23,13 +23,12 @@ limitations under the License.
|
||||
<script src="gr-diff-group.js"></script>
|
||||
|
||||
<script>
|
||||
suite('gr-diff-group tests', function() {
|
||||
|
||||
test('delta line pairs', function() {
|
||||
var group = new GrDiffGroup(GrDiffGroup.Type.DELTA);
|
||||
var l1 = new GrDiffLine(GrDiffLine.Type.ADD);
|
||||
var l2 = new GrDiffLine(GrDiffLine.Type.ADD);
|
||||
var l3 = new GrDiffLine(GrDiffLine.Type.REMOVE);
|
||||
suite('gr-diff-group tests', () => {
|
||||
test('delta line pairs', () => {
|
||||
let group = new GrDiffGroup(GrDiffGroup.Type.DELTA);
|
||||
const l1 = new GrDiffLine(GrDiffLine.Type.ADD);
|
||||
const l2 = new GrDiffLine(GrDiffLine.Type.ADD);
|
||||
const l3 = new GrDiffLine(GrDiffLine.Type.REMOVE);
|
||||
l1.afterNumber = 128;
|
||||
l2.afterNumber = 129;
|
||||
l3.beforeNumber = 64;
|
||||
@@ -44,7 +43,7 @@ limitations under the License.
|
||||
right: {start: 128, end: 129},
|
||||
});
|
||||
|
||||
var pairs = group.getSideBySidePairs();
|
||||
let pairs = group.getSideBySidePairs();
|
||||
assert.deepEqual(pairs, [
|
||||
{left: l3, right: l1},
|
||||
{left: GrDiffLine.BLANK_LINE, right: l2},
|
||||
@@ -62,20 +61,20 @@ limitations under the License.
|
||||
]);
|
||||
});
|
||||
|
||||
test('group/header line pairs', function() {
|
||||
var l1 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
test('group/header line pairs', () => {
|
||||
const l1 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
l1.beforeNumber = 64;
|
||||
l1.afterNumber = 128;
|
||||
|
||||
var l2 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
const l2 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
l2.beforeNumber = 65;
|
||||
l2.afterNumber = 129;
|
||||
|
||||
var l3 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
const l3 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
l3.beforeNumber = 66;
|
||||
l3.afterNumber = 130;
|
||||
|
||||
var group = new GrDiffGroup(GrDiffGroup.Type.BOTH, [l1, l2, l3]);
|
||||
let group = new GrDiffGroup(GrDiffGroup.Type.BOTH, [l1, l2, l3]);
|
||||
|
||||
assert.deepEqual(group.lines, [l1, l2, l3]);
|
||||
assert.deepEqual(group.adds, []);
|
||||
@@ -86,7 +85,7 @@ limitations under the License.
|
||||
right: {start: 128, end: 130},
|
||||
});
|
||||
|
||||
var pairs = group.getSideBySidePairs();
|
||||
let pairs = group.getSideBySidePairs();
|
||||
assert.deepEqual(pairs, [
|
||||
{left: l1, right: l1},
|
||||
{left: l2, right: l2},
|
||||
@@ -106,12 +105,12 @@ limitations under the License.
|
||||
]);
|
||||
});
|
||||
|
||||
test('adding delta lines to non-delta group', function() {
|
||||
var l1 = new GrDiffLine(GrDiffLine.Type.ADD);
|
||||
var l2 = new GrDiffLine(GrDiffLine.Type.REMOVE);
|
||||
var l3 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
test('adding delta lines to non-delta group', () => {
|
||||
const l1 = new GrDiffLine(GrDiffLine.Type.ADD);
|
||||
const l2 = new GrDiffLine(GrDiffLine.Type.REMOVE);
|
||||
const l3 = new GrDiffLine(GrDiffLine.Type.BOTH);
|
||||
|
||||
var group = new GrDiffGroup(GrDiffGroup.Type.BOTH);
|
||||
let group = new GrDiffGroup(GrDiffGroup.Type.BOTH);
|
||||
assert.throws(group.addLine.bind(group, l1));
|
||||
assert.throws(group.addLine.bind(group, l2));
|
||||
assert.doesNotThrow(group.addLine.bind(group, l3));
|
||||
|
||||
@@ -43,5 +43,4 @@
|
||||
GrDiffLine.BLANK_LINE = new GrDiffLine(GrDiffLine.Type.BLANK);
|
||||
|
||||
window.GrDiffLine = GrDiffLine;
|
||||
|
||||
})(window);
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
var DiffViewMode = {
|
||||
const DiffViewMode = {
|
||||
SIDE_BY_SIDE: 'SIDE_BY_SIDE',
|
||||
UNIFIED: 'UNIFIED_DIFF',
|
||||
};
|
||||
|
||||
var DiffSide = {
|
||||
const DiffSide = {
|
||||
LEFT: 'left',
|
||||
RIGHT: 'right',
|
||||
};
|
||||
@@ -65,7 +65,7 @@
|
||||
},
|
||||
filesWeblinks: {
|
||||
type: Object,
|
||||
value: function() { return {}; },
|
||||
value() { return {}; },
|
||||
notify: true,
|
||||
},
|
||||
hidden: {
|
||||
@@ -110,41 +110,41 @@
|
||||
'create-comment': '_handleCreateComment',
|
||||
},
|
||||
|
||||
attached: function() {
|
||||
this._getLoggedIn().then(function(loggedIn) {
|
||||
attached() {
|
||||
this._getLoggedIn().then(loggedIn => {
|
||||
this._loggedIn = loggedIn;
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
ready: function() {
|
||||
ready() {
|
||||
if (this._canRender()) {
|
||||
this.reload();
|
||||
}
|
||||
},
|
||||
|
||||
reload: function() {
|
||||
reload() {
|
||||
this._clearDiffContent();
|
||||
|
||||
var promises = [];
|
||||
const promises = [];
|
||||
|
||||
promises.push(this._getDiff().then(function(diff) {
|
||||
promises.push(this._getDiff().then(diff => {
|
||||
this._diff = diff;
|
||||
return this._loadDiffAssets();
|
||||
}.bind(this)));
|
||||
}));
|
||||
|
||||
promises.push(this._getDiffCommentsAndDrafts().then(function(comments) {
|
||||
promises.push(this._getDiffCommentsAndDrafts().then(comments => {
|
||||
this._comments = comments;
|
||||
}.bind(this)));
|
||||
}));
|
||||
|
||||
return Promise.all(promises).then(function() {
|
||||
return Promise.all(promises).then(() => {
|
||||
if (this.prefs) {
|
||||
return this._renderDiffTable();
|
||||
}
|
||||
return Promise.resolve();
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
getCursorStops: function() {
|
||||
getCursorStops() {
|
||||
if (this.hidden && this.noAutoRender) {
|
||||
return [];
|
||||
}
|
||||
@@ -152,46 +152,46 @@
|
||||
return Polymer.dom(this.root).querySelectorAll('.diff-row');
|
||||
},
|
||||
|
||||
addDraftAtLine: function(el) {
|
||||
addDraftAtLine(el) {
|
||||
this._selectLine(el);
|
||||
this._getLoggedIn().then(function(loggedIn) {
|
||||
this._getLoggedIn().then(loggedIn => {
|
||||
if (!loggedIn) {
|
||||
this.fire('show-auth-required');
|
||||
return;
|
||||
}
|
||||
|
||||
var value = el.getAttribute('data-value');
|
||||
const value = el.getAttribute('data-value');
|
||||
if (value === GrDiffLine.FILE) {
|
||||
this._addDraft(el);
|
||||
return;
|
||||
}
|
||||
var lineNum = parseInt(value, 10);
|
||||
const lineNum = parseInt(value, 10);
|
||||
if (isNaN(lineNum)) {
|
||||
throw Error('Invalid line number: ' + value);
|
||||
}
|
||||
this._addDraft(el, lineNum);
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
isRangeSelected: function() {
|
||||
isRangeSelected() {
|
||||
return this.$.highlights.isRangeSelected();
|
||||
},
|
||||
|
||||
toggleLeftDiff: function() {
|
||||
toggleLeftDiff() {
|
||||
this.toggleClass('no-left');
|
||||
},
|
||||
|
||||
_canRender: function() {
|
||||
_canRender() {
|
||||
return this.changeNum && this.patchRange && this.path &&
|
||||
!this.noAutoRender;
|
||||
},
|
||||
|
||||
_getCommentThreads: function() {
|
||||
_getCommentThreads() {
|
||||
return Polymer.dom(this.root).querySelectorAll('gr-diff-comment-thread');
|
||||
},
|
||||
|
||||
_computeContainerClass: function(loggedIn, viewMode, displayLine) {
|
||||
var classes = ['diffContainer'];
|
||||
_computeContainerClass(loggedIn, viewMode, displayLine) {
|
||||
const classes = ['diffContainer'];
|
||||
switch (viewMode) {
|
||||
case DiffViewMode.UNIFIED:
|
||||
classes.push('unified');
|
||||
@@ -214,8 +214,8 @@
|
||||
return classes.join(' ');
|
||||
},
|
||||
|
||||
_handleTap: function(e) {
|
||||
var el = Polymer.dom(e).rootTarget;
|
||||
_handleTap(e) {
|
||||
const el = Polymer.dom(e).rootTarget;
|
||||
|
||||
if (el.classList.contains('showContext')) {
|
||||
this.$.diffBuilder.showContext(e.detail.groups, e.detail.section);
|
||||
@@ -224,12 +224,12 @@
|
||||
} else if (el.tagName === 'HL' ||
|
||||
el.classList.contains('content') ||
|
||||
el.classList.contains('contentText')) {
|
||||
var target = this.$.diffBuilder.getLineElByChild(el);
|
||||
const target = this.$.diffBuilder.getLineElByChild(el);
|
||||
if (target) { this._selectLine(target); }
|
||||
}
|
||||
},
|
||||
|
||||
_selectLine: function(el) {
|
||||
_selectLine(el) {
|
||||
this.fire('line-selected', {
|
||||
side: el.classList.contains('left') ? DiffSide.LEFT : DiffSide.RIGHT,
|
||||
number: el.getAttribute('data-value'),
|
||||
@@ -237,46 +237,47 @@
|
||||
});
|
||||
},
|
||||
|
||||
_handleCreateComment: function(e) {
|
||||
var range = e.detail.range;
|
||||
var diffSide = e.detail.side;
|
||||
var line = range.endLine;
|
||||
var lineEl = this.$.diffBuilder.getLineElByNumber(line, diffSide);
|
||||
var contentText = this.$.diffBuilder.getContentByLineEl(lineEl);
|
||||
var contentEl = contentText.parentElement;
|
||||
var patchNum = this._getPatchNumByLineAndContent(lineEl, contentEl);
|
||||
var isOnParent =
|
||||
_handleCreateComment(e) {
|
||||
const range = e.detail.range;
|
||||
const diffSide = e.detail.side;
|
||||
const line = range.endLine;
|
||||
const lineEl = this.$.diffBuilder.getLineElByNumber(line, diffSide);
|
||||
const contentText = this.$.diffBuilder.getContentByLineEl(lineEl);
|
||||
const contentEl = contentText.parentElement;
|
||||
const patchNum = this._getPatchNumByLineAndContent(lineEl, contentEl);
|
||||
const isOnParent =
|
||||
this._getIsParentCommentByLineAndContent(lineEl, contentEl);
|
||||
var threadEl = this._getOrCreateThreadAtLineRange(contentEl, patchNum,
|
||||
const threadEl = this._getOrCreateThreadAtLineRange(contentEl, patchNum,
|
||||
diffSide, isOnParent, range);
|
||||
|
||||
threadEl.addOrEditDraft(line, range);
|
||||
},
|
||||
|
||||
_addDraft: function(lineEl, opt_lineNum) {
|
||||
var contentText = this.$.diffBuilder.getContentByLineEl(lineEl);
|
||||
var contentEl = contentText.parentElement;
|
||||
var patchNum = this._getPatchNumByLineAndContent(lineEl, contentEl);
|
||||
var commentSide = this._getCommentSideByLineAndContent(lineEl, contentEl);
|
||||
var isOnParent =
|
||||
_addDraft(lineEl, opt_lineNum) {
|
||||
const contentText = this.$.diffBuilder.getContentByLineEl(lineEl);
|
||||
const contentEl = contentText.parentElement;
|
||||
const patchNum = this._getPatchNumByLineAndContent(lineEl, contentEl);
|
||||
const commentSide =
|
||||
this._getCommentSideByLineAndContent(lineEl, contentEl);
|
||||
const isOnParent =
|
||||
this._getIsParentCommentByLineAndContent(lineEl, contentEl);
|
||||
var threadEl = this._getOrCreateThreadAtLineRange(contentEl, patchNum,
|
||||
const threadEl = this._getOrCreateThreadAtLineRange(contentEl, patchNum,
|
||||
commentSide, isOnParent);
|
||||
|
||||
threadEl.addOrEditDraft(opt_lineNum);
|
||||
},
|
||||
|
||||
_getThreadForRange: function(threadGroupEl, rangeToCheck) {
|
||||
_getThreadForRange(threadGroupEl, rangeToCheck) {
|
||||
return threadGroupEl.getThreadForRange(rangeToCheck);
|
||||
},
|
||||
|
||||
_getThreadGroupForLine: function(contentEl) {
|
||||
_getThreadGroupForLine(contentEl) {
|
||||
return contentEl.querySelector('gr-diff-comment-thread-group');
|
||||
},
|
||||
|
||||
_getOrCreateThreadAtLineRange:
|
||||
function(contentEl, patchNum, commentSide, isOnParent, range) {
|
||||
var rangeToCheck = range ?
|
||||
_getOrCreateThreadAtLineRange(contentEl, patchNum, commentSide,
|
||||
isOnParent, range) {
|
||||
const rangeToCheck = range ?
|
||||
'range-' +
|
||||
range.startLine + '-' +
|
||||
range.startChar + '-' +
|
||||
@@ -285,15 +286,15 @@
|
||||
commentSide : 'line-' + commentSide;
|
||||
|
||||
// Check if thread group exists.
|
||||
var threadGroupEl = this._getThreadGroupForLine(contentEl);
|
||||
let threadGroupEl = this._getThreadGroupForLine(contentEl);
|
||||
if (!threadGroupEl) {
|
||||
threadGroupEl = this.$.diffBuilder.createCommentThreadGroup(
|
||||
this.changeNum, patchNum, this.path, isOnParent,
|
||||
this.projectConfig);
|
||||
this.changeNum, patchNum, this.path, isOnParent,
|
||||
this.projectConfig);
|
||||
contentEl.appendChild(threadGroupEl);
|
||||
}
|
||||
|
||||
var threadEl = this._getThreadForRange(threadGroupEl, rangeToCheck);
|
||||
let threadEl = this._getThreadForRange(threadGroupEl, rangeToCheck);
|
||||
|
||||
if (!threadEl) {
|
||||
threadGroupEl.addNewThread(rangeToCheck, commentSide);
|
||||
@@ -304,8 +305,8 @@
|
||||
return threadEl;
|
||||
},
|
||||
|
||||
_getPatchNumByLineAndContent: function(lineEl, contentEl) {
|
||||
var patchNum = this.patchRange.patchNum;
|
||||
_getPatchNumByLineAndContent(lineEl, contentEl) {
|
||||
let patchNum = this.patchRange.patchNum;
|
||||
if ((lineEl.classList.contains(DiffSide.LEFT) ||
|
||||
contentEl.classList.contains('remove')) &&
|
||||
this.patchRange.basePatchNum !== 'PARENT') {
|
||||
@@ -314,8 +315,8 @@
|
||||
return patchNum;
|
||||
},
|
||||
|
||||
_getIsParentCommentByLineAndContent: function(lineEl, contentEl) {
|
||||
var isOnParent = false;
|
||||
_getIsParentCommentByLineAndContent(lineEl, contentEl) {
|
||||
let isOnParent = false;
|
||||
if ((lineEl.classList.contains(DiffSide.LEFT) ||
|
||||
contentEl.classList.contains('remove')) &&
|
||||
this.patchRange.basePatchNum === 'PARENT') {
|
||||
@@ -324,8 +325,8 @@
|
||||
return isOnParent;
|
||||
},
|
||||
|
||||
_getCommentSideByLineAndContent: function(lineEl, contentEl) {
|
||||
var side = 'right';
|
||||
_getCommentSideByLineAndContent(lineEl, contentEl) {
|
||||
let side = 'right';
|
||||
if (lineEl.classList.contains(DiffSide.LEFT) ||
|
||||
contentEl.classList.contains('remove')) {
|
||||
side = 'left';
|
||||
@@ -333,32 +334,32 @@
|
||||
return side;
|
||||
},
|
||||
|
||||
_handleThreadDiscard: function(e) {
|
||||
var el = Polymer.dom(e).rootTarget;
|
||||
_handleThreadDiscard(e) {
|
||||
const el = Polymer.dom(e).rootTarget;
|
||||
el.parentNode.removeThread(el.locationRange);
|
||||
},
|
||||
|
||||
_handleCommentDiscard: function(e) {
|
||||
var comment = e.detail.comment;
|
||||
_handleCommentDiscard(e) {
|
||||
const comment = e.detail.comment;
|
||||
this._removeComment(comment, e.detail.patchNum);
|
||||
},
|
||||
|
||||
_removeComment: function(comment) {
|
||||
var side = comment.__commentSide;
|
||||
_removeComment(comment) {
|
||||
const side = comment.__commentSide;
|
||||
this._removeCommentFromSide(comment, side);
|
||||
},
|
||||
|
||||
_handleCommentSave: function(e) {
|
||||
var comment = e.detail.comment;
|
||||
var side = e.detail.comment.__commentSide;
|
||||
var idx = this._findDraftIndex(comment, side);
|
||||
_handleCommentSave(e) {
|
||||
const comment = e.detail.comment;
|
||||
const side = e.detail.comment.__commentSide;
|
||||
const idx = this._findDraftIndex(comment, side);
|
||||
this.set(['_comments', side, idx], comment);
|
||||
},
|
||||
|
||||
_handleCommentUpdate: function(e) {
|
||||
var comment = e.detail.comment;
|
||||
var side = e.detail.comment.__commentSide;
|
||||
var idx = this._findCommentIndex(comment, side);
|
||||
_handleCommentUpdate(e) {
|
||||
const comment = e.detail.comment;
|
||||
const side = e.detail.comment.__commentSide;
|
||||
let idx = this._findCommentIndex(comment, side);
|
||||
if (idx === -1) {
|
||||
idx = this._findDraftIndex(comment, side);
|
||||
}
|
||||
@@ -369,8 +370,8 @@
|
||||
}
|
||||
},
|
||||
|
||||
_removeCommentFromSide: function(comment, side) {
|
||||
var idx = this._findCommentIndex(comment, side);
|
||||
_removeCommentFromSide(comment, side) {
|
||||
let idx = this._findCommentIndex(comment, side);
|
||||
if (idx === -1) {
|
||||
idx = this._findDraftIndex(comment, side);
|
||||
}
|
||||
@@ -379,29 +380,29 @@
|
||||
}
|
||||
},
|
||||
|
||||
_findCommentIndex: function(comment, side) {
|
||||
_findCommentIndex(comment, side) {
|
||||
if (!comment.id || !this._comments[side]) {
|
||||
return -1;
|
||||
}
|
||||
return this._comments[side].findIndex(function(item) {
|
||||
return this._comments[side].findIndex(item => {
|
||||
return item.id === comment.id;
|
||||
});
|
||||
},
|
||||
|
||||
_findDraftIndex: function(comment, side) {
|
||||
_findDraftIndex(comment, side) {
|
||||
if (!comment.__draftID || !this._comments[side]) {
|
||||
return -1;
|
||||
}
|
||||
return this._comments[side].findIndex(function(item) {
|
||||
return this._comments[side].findIndex(item => {
|
||||
return item.__draftID === comment.__draftID;
|
||||
});
|
||||
},
|
||||
|
||||
_prefsObserver: function(newPrefs, oldPrefs) {
|
||||
_prefsObserver(newPrefs, oldPrefs) {
|
||||
// Scan the preference objects one level deep to see if they differ.
|
||||
var differ = !oldPrefs;
|
||||
let differ = !oldPrefs;
|
||||
if (newPrefs && oldPrefs) {
|
||||
for (var key in newPrefs) {
|
||||
for (const key in newPrefs) {
|
||||
if (newPrefs[key] !== oldPrefs[key]) {
|
||||
differ = true;
|
||||
}
|
||||
@@ -413,15 +414,15 @@
|
||||
}
|
||||
},
|
||||
|
||||
_viewModeObserver: function() {
|
||||
_viewModeObserver() {
|
||||
this._prefsChanged(this.prefs);
|
||||
},
|
||||
|
||||
_lineWrappingObserver: function() {
|
||||
_lineWrappingObserver() {
|
||||
this._prefsChanged(this.prefs);
|
||||
},
|
||||
|
||||
_prefsChanged: function(prefs) {
|
||||
_prefsChanged(prefs) {
|
||||
if (!prefs) { return; }
|
||||
if (prefs.line_wrapping) {
|
||||
this._diffTableClass = 'full-width';
|
||||
@@ -433,7 +434,7 @@
|
||||
this.customStyle['--content-width'] = prefs.line_length + 'ch';
|
||||
}
|
||||
|
||||
if (!!prefs.font_size) {
|
||||
if (prefs.font_size) {
|
||||
this.customStyle['--font-size'] = prefs.font_size + 'px';
|
||||
}
|
||||
|
||||
@@ -444,40 +445,40 @@
|
||||
}
|
||||
},
|
||||
|
||||
_renderDiffTable: function() {
|
||||
_renderDiffTable() {
|
||||
return this.$.diffBuilder.render(this._comments, this.prefs);
|
||||
},
|
||||
|
||||
_clearDiffContent: function() {
|
||||
_clearDiffContent() {
|
||||
this.$.diffTable.innerHTML = null;
|
||||
},
|
||||
|
||||
_handleGetDiffError: function(response) {
|
||||
_handleGetDiffError(response) {
|
||||
// Loading the diff may respond with 409 if the file is too large. In this
|
||||
// case, use a toast error..
|
||||
if (response.status === 409) {
|
||||
this.fire('server-error', {response: response});
|
||||
this.fire('server-error', {response});
|
||||
return;
|
||||
}
|
||||
this.fire('page-error', {response: response});
|
||||
this.fire('page-error', {response});
|
||||
},
|
||||
|
||||
_getDiff: function() {
|
||||
_getDiff() {
|
||||
return this.$.restAPI.getDiff(
|
||||
this.changeNum,
|
||||
this.patchRange.basePatchNum,
|
||||
this.patchRange.patchNum,
|
||||
this.path,
|
||||
this._handleGetDiffError.bind(this)).then(function(diff) {
|
||||
this._handleGetDiffError.bind(this)).then(diff => {
|
||||
this.filesWeblinks = {
|
||||
meta_a: diff && diff.meta_a && diff.meta_a.web_links,
|
||||
meta_b: diff && diff.meta_b && diff.meta_b.web_links,
|
||||
};
|
||||
return diff;
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
_getDiffComments: function() {
|
||||
_getDiffComments() {
|
||||
return this.$.restAPI.getDiffComments(
|
||||
this.changeNum,
|
||||
this.patchRange.basePatchNum,
|
||||
@@ -485,8 +486,8 @@
|
||||
this.path);
|
||||
},
|
||||
|
||||
_getDiffDrafts: function() {
|
||||
return this._getLoggedIn().then(function(loggedIn) {
|
||||
_getDiffDrafts() {
|
||||
return this._getLoggedIn().then(loggedIn => {
|
||||
if (!loggedIn) {
|
||||
return Promise.resolve({baseComments: [], comments: []});
|
||||
}
|
||||
@@ -495,10 +496,10 @@
|
||||
this.patchRange.basePatchNum,
|
||||
this.patchRange.patchNum,
|
||||
this.path);
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
_getDiffRobotComments: function() {
|
||||
_getDiffRobotComments() {
|
||||
return this.$.restAPI.getDiffRobotComments(
|
||||
this.changeNum,
|
||||
this.patchRange.basePatchNum,
|
||||
@@ -506,12 +507,12 @@
|
||||
this.path);
|
||||
},
|
||||
|
||||
_getDiffCommentsAndDrafts: function() {
|
||||
var promises = [];
|
||||
_getDiffCommentsAndDrafts() {
|
||||
const promises = [];
|
||||
promises.push(this._getDiffComments());
|
||||
promises.push(this._getDiffDrafts());
|
||||
promises.push(this._getDiffRobotComments());
|
||||
return Promise.all(promises).then(function(results) {
|
||||
return Promise.all(promises).then(results => {
|
||||
return Promise.resolve({
|
||||
comments: results[0],
|
||||
drafts: results[1],
|
||||
@@ -520,16 +521,16 @@
|
||||
}).then(this._normalizeDiffCommentsAndDrafts.bind(this));
|
||||
},
|
||||
|
||||
_normalizeDiffCommentsAndDrafts: function(results) {
|
||||
_normalizeDiffCommentsAndDrafts(results) {
|
||||
function markAsDraft(d) {
|
||||
d.__draft = true;
|
||||
return d;
|
||||
}
|
||||
var baseDrafts = results.drafts.baseComments.map(markAsDraft);
|
||||
var drafts = results.drafts.comments.map(markAsDraft);
|
||||
const baseDrafts = results.drafts.baseComments.map(markAsDraft);
|
||||
const drafts = results.drafts.comments.map(markAsDraft);
|
||||
|
||||
var baseRobotComments = results.robotComments.baseComments;
|
||||
var robotComments = results.robotComments.comments;
|
||||
const baseRobotComments = results.robotComments.baseComments;
|
||||
const robotComments = results.robotComments.comments;
|
||||
return Promise.resolve({
|
||||
meta: {
|
||||
path: this.path,
|
||||
@@ -544,27 +545,27 @@
|
||||
});
|
||||
},
|
||||
|
||||
_getLoggedIn: function() {
|
||||
_getLoggedIn() {
|
||||
return this.$.restAPI.getLoggedIn();
|
||||
},
|
||||
|
||||
_computeIsImageDiff: function() {
|
||||
_computeIsImageDiff() {
|
||||
if (!this._diff) { return false; }
|
||||
|
||||
var isA = this._diff.meta_a &&
|
||||
this._diff.meta_a.content_type.indexOf('image/') === 0;
|
||||
var isB = this._diff.meta_b &&
|
||||
this._diff.meta_b.content_type.indexOf('image/') === 0;
|
||||
const isA = this._diff.meta_a &&
|
||||
this._diff.meta_a.content_type.startsWith('image/');
|
||||
const isB = this._diff.meta_b &&
|
||||
this._diff.meta_b.content_type.startsWith('image/');
|
||||
|
||||
return this._diff.binary && (isA || isB);
|
||||
},
|
||||
|
||||
_loadDiffAssets: function() {
|
||||
_loadDiffAssets() {
|
||||
if (this.isImageDiff) {
|
||||
return this._getImages().then(function(images) {
|
||||
return this._getImages().then(images => {
|
||||
this._baseImage = images.baseImage;
|
||||
this._revisionImage = images.revisionImage;
|
||||
}.bind(this));
|
||||
});
|
||||
} else {
|
||||
this._baseImage = null;
|
||||
this._revisionImage = null;
|
||||
@@ -572,30 +573,30 @@
|
||||
}
|
||||
},
|
||||
|
||||
_getImages: function() {
|
||||
_getImages() {
|
||||
return this.$.restAPI.getImagesForDiff(this.changeNum, this._diff,
|
||||
this.patchRange);
|
||||
},
|
||||
|
||||
_projectConfigChanged: function(projectConfig) {
|
||||
var threadEls = this._getCommentThreads();
|
||||
for (var i = 0; i < threadEls.length; i++) {
|
||||
_projectConfigChanged(projectConfig) {
|
||||
const threadEls = this._getCommentThreads();
|
||||
for (let i = 0; i < threadEls.length; i++) {
|
||||
threadEls[i].projectConfig = projectConfig;
|
||||
}
|
||||
},
|
||||
|
||||
_computeDiffHeaderItems: function(diffInfoRecord) {
|
||||
var diffInfo = diffInfoRecord.base;
|
||||
_computeDiffHeaderItems(diffInfoRecord) {
|
||||
const diffInfo = diffInfoRecord.base;
|
||||
if (!diffInfo || !diffInfo.diff_header || diffInfo.binary) { return []; }
|
||||
return diffInfo.diff_header.filter(function(item) {
|
||||
return !(item.indexOf('diff --git ') === 0 ||
|
||||
item.indexOf('index ') === 0 ||
|
||||
item.indexOf('+++ ') === 0 ||
|
||||
item.indexOf('--- ') === 0);
|
||||
return diffInfo.diff_header.filter(item => {
|
||||
return !(item.startsWith('diff --git ') ||
|
||||
item.startsWith('index ') ||
|
||||
item.startsWith('+++ ') ||
|
||||
item.startsWith('--- '));
|
||||
});
|
||||
},
|
||||
|
||||
_computeDiffHeaderHidden: function(items) {
|
||||
_computeDiffHeaderHidden(items) {
|
||||
return items.length === 0;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -35,83 +35,82 @@ limitations under the License.
|
||||
</test-fixture>
|
||||
|
||||
<script>
|
||||
suite('gr-diff tests', function() {
|
||||
var element;
|
||||
var sandbox;
|
||||
suite('gr-diff tests', () => {
|
||||
let element;
|
||||
let sandbox;
|
||||
|
||||
setup(function() {
|
||||
setup(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
teardown(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
suite('not logged in', function() {
|
||||
setup(function() {
|
||||
suite('not logged in', () => {
|
||||
setup(() => {
|
||||
stub('gr-rest-api-interface', {
|
||||
getLoggedIn: function() { return Promise.resolve(false); },
|
||||
getLoggedIn() { return Promise.resolve(false); },
|
||||
});
|
||||
element = fixture('basic');
|
||||
});
|
||||
|
||||
test('toggleLeftDiff', function() {
|
||||
test('toggleLeftDiff', () => {
|
||||
element.toggleLeftDiff();
|
||||
assert.isTrue(element.classList.contains('no-left'));
|
||||
element.toggleLeftDiff();
|
||||
assert.isFalse(element.classList.contains('no-left'));
|
||||
});
|
||||
|
||||
test('addDraftAtLine', function(done) {
|
||||
test('addDraftAtLine', done => {
|
||||
sandbox.stub(element, '_selectLine');
|
||||
var loggedInErrorSpy = sandbox.spy();
|
||||
const loggedInErrorSpy = sandbox.spy();
|
||||
element.addEventListener('show-auth-required', loggedInErrorSpy);
|
||||
element.addDraftAtLine();
|
||||
flush(function() {
|
||||
flush(() => {
|
||||
assert.isTrue(loggedInErrorSpy.called);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('view does not start with displayLine classList', function() {
|
||||
test('view does not start with displayLine classList', () => {
|
||||
assert.isFalse(
|
||||
element.$$('.diffContainer').classList.contains('displayLine'));
|
||||
});
|
||||
|
||||
test('displayLine class added called when displayLine is true',
|
||||
function() {
|
||||
var spy = sandbox.spy(element, '_computeContainerClass');
|
||||
test('displayLine class added called when displayLine is true', () => {
|
||||
const spy = sandbox.spy(element, '_computeContainerClass');
|
||||
element.displayLine = true;
|
||||
assert.isTrue(spy.called);
|
||||
assert.isTrue(
|
||||
element.$$('.diffContainer').classList.contains('displayLine'));
|
||||
});
|
||||
|
||||
test('get drafts', function(done) {
|
||||
test('get drafts', done => {
|
||||
element.patchRange = {basePatchNum: 0, patchNum: 0};
|
||||
|
||||
var getDraftsStub = sandbox.stub(element.$.restAPI, 'getDiffDrafts');
|
||||
element._getDiffDrafts().then(function(result) {
|
||||
const getDraftsStub = sandbox.stub(element.$.restAPI, 'getDiffDrafts');
|
||||
element._getDiffDrafts().then(result => {
|
||||
assert.deepEqual(result, {baseComments: [], comments: []});
|
||||
sinon.assert.notCalled(getDraftsStub);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('get robot comments', function(done) {
|
||||
test('get robot comments', done => {
|
||||
element.patchRange = {basePatchNum: 0, patchNum: 0};
|
||||
|
||||
var getDraftsStub = sandbox.stub(element.$.restAPI,
|
||||
const getDraftsStub = sandbox.stub(element.$.restAPI,
|
||||
'getDiffRobotComments');
|
||||
element._getDiffDrafts().then(function(result) {
|
||||
element._getDiffDrafts().then(result => {
|
||||
assert.deepEqual(result, {baseComments: [], comments: []});
|
||||
sinon.assert.notCalled(getDraftsStub);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('loads files weblinks', function(done) {
|
||||
var diffStub = sandbox.stub(element.$.restAPI, 'getDiff').returns(
|
||||
test('loads files weblinks', done => {
|
||||
sandbox.stub(element.$.restAPI, 'getDiff').returns(
|
||||
Promise.resolve({
|
||||
meta_a: {
|
||||
web_links: 'foo',
|
||||
@@ -121,7 +120,7 @@ limitations under the License.
|
||||
},
|
||||
}));
|
||||
element.patchRange = {};
|
||||
element._getDiff().then(function() {
|
||||
element._getDiff().then(() => {
|
||||
assert.deepEqual(element.filesWeblinks, {
|
||||
meta_a: 'foo',
|
||||
meta_b: 'bar',
|
||||
@@ -130,7 +129,7 @@ limitations under the License.
|
||||
});
|
||||
});
|
||||
|
||||
test('remove comment', function() {
|
||||
test('remove comment', () => {
|
||||
element._comments = {
|
||||
meta: {
|
||||
changeNum: '42',
|
||||
@@ -183,7 +182,7 @@ limitations under the License.
|
||||
}));
|
||||
|
||||
element._removeComment({id: 'bc2', side: 'PARENT',
|
||||
__commentSide: 'left'});
|
||||
__commentSide: 'left'});
|
||||
assert.deepEqual(element._comments, {
|
||||
meta: {
|
||||
changeNum: '42',
|
||||
@@ -231,13 +230,12 @@ limitations under the License.
|
||||
});
|
||||
});
|
||||
|
||||
test('thread groups', function() {
|
||||
var contentEl = document.createElement('div');
|
||||
var rangeToCheck = 'line-left';
|
||||
var commentSide = 'left';
|
||||
var patchNum = 1;
|
||||
var side = 'PARENT';
|
||||
var range = {
|
||||
test('thread groups', () => {
|
||||
const contentEl = document.createElement('div');
|
||||
const commentSide = 'left';
|
||||
const patchNum = 1;
|
||||
const side = 'PARENT';
|
||||
let range = {
|
||||
startLine: 1,
|
||||
startChar: 1,
|
||||
endLine: 1,
|
||||
@@ -248,10 +246,9 @@ limitations under the License.
|
||||
element.patchRange = {basePatchNum: 1, patchNum: 2};
|
||||
element.path = 'file.txt';
|
||||
|
||||
sandbox.stub(element.$.diffBuilder, 'createCommentThreadGroup',
|
||||
function() {
|
||||
var threadGroup =
|
||||
document.createElement('gr-diff-comment-thread-group');
|
||||
sandbox.stub(element.$.diffBuilder, 'createCommentThreadGroup', () => {
|
||||
const threadGroup =
|
||||
document.createElement('gr-diff-comment-thread-group');
|
||||
threadGroup.patchForNewThreads = 1;
|
||||
return threadGroup;
|
||||
});
|
||||
@@ -279,18 +276,18 @@ limitations under the License.
|
||||
assert.equal(contentEl.querySelectorAll(
|
||||
'gr-diff-comment-thread-group').length, 1);
|
||||
|
||||
var threadGroup = contentEl.querySelector(
|
||||
const threadGroup = contentEl.querySelector(
|
||||
'gr-diff-comment-thread-group');
|
||||
var threadLength = Polymer.dom(threadGroup.root).
|
||||
const threadLength = Polymer.dom(threadGroup.root).
|
||||
querySelectorAll('gr-diff-comment-thread').length;
|
||||
assert.equal(threadLength, 2);
|
||||
});
|
||||
|
||||
suite('image diffs', function() {
|
||||
var mockFile1;
|
||||
var mockFile2;
|
||||
var stubs = [];
|
||||
setup(function() {
|
||||
suite('image diffs', () => {
|
||||
let mockFile1;
|
||||
let mockFile2;
|
||||
const stubs = [];
|
||||
setup(() => {
|
||||
mockFile1 = {
|
||||
body: 'Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BACAAAAAAAAAAAAATCwAAE' +
|
||||
'wsAAAAAAAAAAAAAAAAA/w==',
|
||||
@@ -299,9 +296,9 @@ limitations under the License.
|
||||
mockFile2 = {
|
||||
body: 'Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BACAAAAAAAAAAAAATCwAAE' +
|
||||
'wsAAAAAAAAAAAAA/////w==',
|
||||
type: 'image/bmp'
|
||||
type: 'image/bmp',
|
||||
};
|
||||
var mockCommit = {
|
||||
const mockCommit = {
|
||||
commit: '9a1a1d10baece5efbba10bc4ccf808a67a50ac0a',
|
||||
parents: [{
|
||||
commit: '7338aa9adfe57909f1fdaf88975cdea467d3382f',
|
||||
@@ -322,29 +319,29 @@ limitations under the License.
|
||||
subject: 'Updated the carrot',
|
||||
message: 'Updated the carrot\n\nChange-Id: Iabcd123\n',
|
||||
};
|
||||
var mockComments = {baseComments: [], comments: []};
|
||||
const mockComments = {baseComments: [], comments: []};
|
||||
|
||||
stubs.push(sandbox.stub(element.$.restAPI, 'getCommitInfo',
|
||||
function() { return Promise.resolve(mockCommit); }));
|
||||
() => Promise.resolve(mockCommit)));
|
||||
stubs.push(sandbox.stub(element.$.restAPI,
|
||||
'getChangeFileContents',
|
||||
function(changeId, patchNum, path, opt_parentIndex) {
|
||||
(changeId, patchNum, path, opt_parentIndex) => {
|
||||
return Promise.resolve(opt_parentIndex === 1 ? mockFile1 :
|
||||
mockFile2);
|
||||
}));
|
||||
stubs.push(sandbox.stub(element.$.restAPI, '_getDiffComments',
|
||||
function() { return Promise.resolve(mockComments); }));
|
||||
() => Promise.resolve(mockComments)));
|
||||
stubs.push(sandbox.stub(element.$.restAPI, 'getDiffDrafts',
|
||||
function() { return Promise.resolve(mockComments); }));
|
||||
() => Promise.resolve(mockComments)));
|
||||
|
||||
element.patchRange = {basePatchNum: 'PARENT', patchNum: 1};
|
||||
});
|
||||
|
||||
test('renders image diffs with same file name', function(done) {
|
||||
var mockDiff = {
|
||||
test('renders image diffs with same file name', done => {
|
||||
const mockDiff = {
|
||||
meta_a: {name: 'carrot.jpg', content_type: 'image/jpeg', lines: 66},
|
||||
meta_b: {name: 'carrot.jpg', content_type: 'image/jpeg',
|
||||
lines: 560},
|
||||
lines: 560},
|
||||
intraline_status: 'OK',
|
||||
change_type: 'MODIFIED',
|
||||
diff_header: [
|
||||
@@ -358,33 +355,35 @@ limitations under the License.
|
||||
binary: true,
|
||||
};
|
||||
stubs.push(sandbox.stub(element, '_getDiff',
|
||||
function() { return Promise.resolve(mockDiff); }));
|
||||
() => Promise.resolve(mockDiff)));
|
||||
|
||||
var rendered = function() {
|
||||
const rendered = () => {
|
||||
// Recognizes that it should be an image diff.
|
||||
assert.isTrue(element.isImageDiff);
|
||||
assert.instanceOf(
|
||||
element.$.diffBuilder._builder, GrDiffBuilderImage);
|
||||
element.$.diffBuilder._builder, GrDiffBuilderImage);
|
||||
|
||||
// Left image rendered with the parent commit's version of the file.
|
||||
var leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
var leftLabel = element.$.diffTable.querySelector('td.left label');
|
||||
var leftLabelContent = leftLabel.querySelector('.label');
|
||||
var leftLabelName = leftLabel.querySelector('.name');
|
||||
const leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
const leftLabel =
|
||||
element.$.diffTable.querySelector('td.left label');
|
||||
const leftLabelContent = leftLabel.querySelector('.label');
|
||||
const leftLabelName = leftLabel.querySelector('.name');
|
||||
|
||||
var rightImage = element.$.diffTable.querySelector('td.right img');
|
||||
var rightLabel = element.$.diffTable.querySelector(
|
||||
'td.right label');
|
||||
var rightLabelContent = rightLabel.querySelector('.label');
|
||||
var rightLabelName = rightLabel.querySelector('.name');
|
||||
const rightImage =
|
||||
element.$.diffTable.querySelector('td.right img');
|
||||
const rightLabel = element.$.diffTable.querySelector(
|
||||
'td.right label');
|
||||
const rightLabelContent = rightLabel.querySelector('.label');
|
||||
const rightLabelName = rightLabel.querySelector('.name');
|
||||
|
||||
assert.isNotOk(rightLabelName);
|
||||
assert.isNotOk(leftLabelName);
|
||||
|
||||
var leftLoaded = false;
|
||||
var rightLoaded = false;
|
||||
let leftLoaded = false;
|
||||
let rightLoaded = false;
|
||||
|
||||
leftImage.addEventListener('load', function() {
|
||||
leftImage.addEventListener('load', () => {
|
||||
assert.isOk(leftImage);
|
||||
assert.equal(leftImage.getAttribute('src'),
|
||||
'data:image/bmp;base64, ' + mockFile1.body);
|
||||
@@ -396,7 +395,7 @@ limitations under the License.
|
||||
}
|
||||
});
|
||||
|
||||
rightImage.addEventListener('load', function() {
|
||||
rightImage.addEventListener('load', () => {
|
||||
assert.isOk(rightImage);
|
||||
assert.equal(rightImage.getAttribute('src'),
|
||||
'data:image/bmp;base64, ' + mockFile2.body);
|
||||
@@ -408,22 +407,21 @@ limitations under the License.
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
element.addEventListener('render', rendered);
|
||||
|
||||
element.$.restAPI.getDiffPreferences().then(function(prefs) {
|
||||
element.$.restAPI.getDiffPreferences().then(prefs => {
|
||||
element.prefs = prefs;
|
||||
element.reload();
|
||||
});
|
||||
});
|
||||
|
||||
test('renders image diffs with a different file name', function(done) {
|
||||
var mockDiff = {
|
||||
test('renders image diffs with a different file name', done => {
|
||||
const mockDiff = {
|
||||
meta_a: {name: 'carrot.jpg', content_type: 'image/jpeg', lines: 66},
|
||||
meta_b: {name: 'carrot2.jpg', content_type: 'image/jpeg',
|
||||
lines: 560},
|
||||
lines: 560},
|
||||
intraline_status: 'OK',
|
||||
change_type: 'MODIFIED',
|
||||
diff_header: [
|
||||
@@ -437,35 +435,37 @@ limitations under the License.
|
||||
binary: true,
|
||||
};
|
||||
stubs.push(sandbox.stub(element, '_getDiff',
|
||||
function() { return Promise.resolve(mockDiff); }));
|
||||
() => Promise.resolve(mockDiff)));
|
||||
|
||||
var rendered = function() {
|
||||
const rendered = () => {
|
||||
// Recognizes that it should be an image diff.
|
||||
assert.isTrue(element.isImageDiff);
|
||||
assert.instanceOf(
|
||||
element.$.diffBuilder._builder, GrDiffBuilderImage);
|
||||
element.$.diffBuilder._builder, GrDiffBuilderImage);
|
||||
|
||||
// Left image rendered with the parent commit's version of the file.
|
||||
var leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
var leftLabel = element.$.diffTable.querySelector('td.left label');
|
||||
var leftLabelContent = leftLabel.querySelector('.label');
|
||||
var leftLabelName = leftLabel.querySelector('.name');
|
||||
const leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
const leftLabel =
|
||||
element.$.diffTable.querySelector('td.left label');
|
||||
const leftLabelContent = leftLabel.querySelector('.label');
|
||||
const leftLabelName = leftLabel.querySelector('.name');
|
||||
|
||||
var rightImage = element.$.diffTable.querySelector('td.right img');
|
||||
var rightLabel = element.$.diffTable.querySelector(
|
||||
'td.right label');
|
||||
var rightLabelContent = rightLabel.querySelector('.label');
|
||||
var rightLabelName = rightLabel.querySelector('.name');
|
||||
const rightImage =
|
||||
element.$.diffTable.querySelector('td.right img');
|
||||
const rightLabel = element.$.diffTable.querySelector(
|
||||
'td.right label');
|
||||
const rightLabelContent = rightLabel.querySelector('.label');
|
||||
const rightLabelName = rightLabel.querySelector('.name');
|
||||
|
||||
assert.isOk(rightLabelName);
|
||||
assert.isOk(leftLabelName);
|
||||
assert.equal(leftLabelName.textContent, mockDiff.meta_a.name);
|
||||
assert.equal(rightLabelName.textContent, mockDiff.meta_b.name);
|
||||
|
||||
var leftLoaded = false;
|
||||
var rightLoaded = false;
|
||||
let leftLoaded = false;
|
||||
let rightLoaded = false;
|
||||
|
||||
leftImage.addEventListener('load', function() {
|
||||
leftImage.addEventListener('load', () => {
|
||||
assert.isOk(leftImage);
|
||||
assert.equal(leftImage.getAttribute('src'),
|
||||
'data:image/bmp;base64, ' + mockFile1.body);
|
||||
@@ -477,7 +477,7 @@ limitations under the License.
|
||||
}
|
||||
});
|
||||
|
||||
rightImage.addEventListener('load', function() {
|
||||
rightImage.addEventListener('load', () => {
|
||||
assert.isOk(rightImage);
|
||||
assert.equal(rightImage.getAttribute('src'),
|
||||
'data:image/bmp;base64, ' + mockFile2.body);
|
||||
@@ -489,21 +489,20 @@ limitations under the License.
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
element.addEventListener('render', rendered);
|
||||
|
||||
element.$.restAPI.getDiffPreferences().then(function(prefs) {
|
||||
element.$.restAPI.getDiffPreferences().then(prefs => {
|
||||
element.prefs = prefs;
|
||||
element.reload();
|
||||
});
|
||||
});
|
||||
|
||||
test('renders added image', function(done) {
|
||||
var mockDiff = {
|
||||
test('renders added image', done => {
|
||||
const mockDiff = {
|
||||
meta_b: {name: 'carrot.jpg', content_type: 'image/jpeg',
|
||||
lines: 560},
|
||||
lines: 560},
|
||||
intraline_status: 'OK',
|
||||
change_type: 'ADDED',
|
||||
diff_header: [
|
||||
@@ -517,32 +516,32 @@ limitations under the License.
|
||||
binary: true,
|
||||
};
|
||||
stubs.push(sandbox.stub(element, '_getDiff',
|
||||
function() { return Promise.resolve(mockDiff); }));
|
||||
() => Promise.resolve(mockDiff)));
|
||||
|
||||
element.addEventListener('render', function() {
|
||||
element.addEventListener('render', () => {
|
||||
// Recognizes that it should be an image diff.
|
||||
assert.isTrue(element.isImageDiff);
|
||||
assert.instanceOf(
|
||||
element.$.diffBuilder._builder, GrDiffBuilderImage);
|
||||
|
||||
var leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
var rightImage = element.$.diffTable.querySelector('td.right img');
|
||||
const leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
const rightImage = element.$.diffTable.querySelector('td.right img');
|
||||
|
||||
assert.isNotOk(leftImage);
|
||||
assert.isOk(rightImage);
|
||||
done();
|
||||
});
|
||||
|
||||
element.$.restAPI.getDiffPreferences().then(function(prefs) {
|
||||
element.$.restAPI.getDiffPreferences().then(prefs => {
|
||||
element.prefs = prefs;
|
||||
element.reload();
|
||||
});
|
||||
});
|
||||
|
||||
test('renders removed image', function(done) {
|
||||
var mockDiff = {
|
||||
test('renders removed image', done => {
|
||||
const mockDiff = {
|
||||
meta_a: {name: 'carrot.jpg', content_type: 'image/jpeg',
|
||||
lines: 560},
|
||||
lines: 560},
|
||||
intraline_status: 'OK',
|
||||
change_type: 'DELETED',
|
||||
diff_header: [
|
||||
@@ -556,32 +555,32 @@ limitations under the License.
|
||||
binary: true,
|
||||
};
|
||||
stubs.push(sandbox.stub(element, '_getDiff',
|
||||
function() { return Promise.resolve(mockDiff); }));
|
||||
() => Promise.resolve(mockDiff)));
|
||||
|
||||
element.addEventListener('render', function() {
|
||||
element.addEventListener('render', () => {
|
||||
// Recognizes that it should be an image diff.
|
||||
assert.isTrue(element.isImageDiff);
|
||||
assert.instanceOf(
|
||||
element.$.diffBuilder._builder, GrDiffBuilderImage);
|
||||
|
||||
var leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
var rightImage = element.$.diffTable.querySelector('td.right img');
|
||||
const leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
const rightImage = element.$.diffTable.querySelector('td.right img');
|
||||
|
||||
assert.isOk(leftImage);
|
||||
assert.isNotOk(rightImage);
|
||||
done();
|
||||
});
|
||||
|
||||
element.$.restAPI.getDiffPreferences().then(function(prefs) {
|
||||
element.$.restAPI.getDiffPreferences().then(prefs => {
|
||||
element.prefs = prefs;
|
||||
element.reload();
|
||||
});
|
||||
});
|
||||
|
||||
test('does not render disallowed image type', function(done) {
|
||||
var mockDiff = {
|
||||
test('does not render disallowed image type', done => {
|
||||
const mockDiff = {
|
||||
meta_a: {name: 'carrot.jpg', content_type: 'image/jpeg-evil',
|
||||
lines: 560},
|
||||
lines: 560},
|
||||
intraline_status: 'OK',
|
||||
change_type: 'DELETED',
|
||||
diff_header: [
|
||||
@@ -597,30 +596,30 @@ limitations under the License.
|
||||
mockFile1.type = 'image/jpeg-evil';
|
||||
|
||||
stubs.push(sandbox.stub(element, '_getDiff',
|
||||
function() { return Promise.resolve(mockDiff); }));
|
||||
() => Promise.resolve(mockDiff)));
|
||||
|
||||
element.addEventListener('render', function() {
|
||||
element.addEventListener('render', () => {
|
||||
// Recognizes that it should be an image diff.
|
||||
assert.isTrue(element.isImageDiff);
|
||||
assert.instanceOf(
|
||||
element.$.diffBuilder._builder, GrDiffBuilderImage);
|
||||
var leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
const leftImage = element.$.diffTable.querySelector('td.left img');
|
||||
assert.isNotOk(leftImage);
|
||||
done();
|
||||
});
|
||||
|
||||
element.$.restAPI.getDiffPreferences().then(function(prefs) {
|
||||
element.$.restAPI.getDiffPreferences().then(prefs => {
|
||||
element.prefs = prefs;
|
||||
element.reload();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('_handleTap lineNum', function(done) {
|
||||
var addDraftStub = sinon.stub(element, 'addDraftAtLine');
|
||||
var el = document.createElement('div');
|
||||
test('_handleTap lineNum', done => {
|
||||
const addDraftStub = sandbox.stub(element, 'addDraftAtLine');
|
||||
const el = document.createElement('div');
|
||||
el.className = 'lineNum';
|
||||
el.addEventListener('click', function(e) {
|
||||
el.addEventListener('click', e => {
|
||||
element._handleTap(e);
|
||||
assert.isTrue(addDraftStub.called);
|
||||
assert.equal(addDraftStub.lastCall.args[0], el);
|
||||
@@ -629,11 +628,12 @@ limitations under the License.
|
||||
el.click();
|
||||
});
|
||||
|
||||
test('_handleTap context', function(done) {
|
||||
var showContextStub = sinon.stub(element.$.diffBuilder, 'showContext');
|
||||
var el = document.createElement('div');
|
||||
test('_handleTap context', done => {
|
||||
const showContextStub =
|
||||
sandbox.stub(element.$.diffBuilder, 'showContext');
|
||||
const el = document.createElement('div');
|
||||
el.className = 'showContext';
|
||||
el.addEventListener('click', function(e) {
|
||||
el.addEventListener('click', e => {
|
||||
element._handleTap(e);
|
||||
assert.isTrue(showContextStub.called);
|
||||
done();
|
||||
@@ -641,16 +641,15 @@ limitations under the License.
|
||||
el.click();
|
||||
});
|
||||
|
||||
test('_handleTap content', function(done) {
|
||||
var content = document.createElement('div');
|
||||
var lineEl = document.createElement('div');
|
||||
test('_handleTap content', done => {
|
||||
const content = document.createElement('div');
|
||||
const lineEl = document.createElement('div');
|
||||
|
||||
var selectStub = sandbox.stub(element, '_selectLine');
|
||||
var getLineStub = sandbox.stub(element.$.diffBuilder,
|
||||
'getLineElByChild', function() { return lineEl; });
|
||||
const selectStub = sandbox.stub(element, '_selectLine');
|
||||
sandbox.stub(element.$.diffBuilder, 'getLineElByChild', () => lineEl);
|
||||
|
||||
content.className = 'content';
|
||||
content.addEventListener('click', function(e) {
|
||||
content.addEventListener('click', e => {
|
||||
element._handleTap(e);
|
||||
assert.isTrue(selectStub.called);
|
||||
assert.equal(selectStub.lastCall.args[0], lineEl);
|
||||
@@ -659,9 +658,9 @@ limitations under the License.
|
||||
content.click();
|
||||
});
|
||||
|
||||
test('_getDiff handles null diff responses', function(done) {
|
||||
test('_getDiff handles null diff responses', done => {
|
||||
stub('gr-rest-api-interface', {
|
||||
getDiff: function() { return Promise.resolve(null); },
|
||||
getDiff() { return Promise.resolve(null); },
|
||||
});
|
||||
element.changeNum = 123;
|
||||
element.patchRange = {basePatchNum: 1, patchNum: 2};
|
||||
@@ -669,10 +668,9 @@ limitations under the License.
|
||||
element._getDiff().then(done);
|
||||
});
|
||||
|
||||
suite('getCursorStops', function() {
|
||||
|
||||
var setupDiff = function() {
|
||||
var mock = document.createElement('mock-diff-response');
|
||||
suite('getCursorStops', () => {
|
||||
const setupDiff = function() {
|
||||
const mock = document.createElement('mock-diff-response');
|
||||
element._diff = mock.diffResponse;
|
||||
element._comments = {
|
||||
left: [],
|
||||
@@ -699,54 +697,53 @@ limitations under the License.
|
||||
flushAsynchronousOperations();
|
||||
};
|
||||
|
||||
test('getCursorStops returns [] when hidden and noAutoRender are true',
|
||||
function() {
|
||||
test('getCursorStops returns [] when hidden and noAutoRender', () => {
|
||||
element.noAutoRender = true;
|
||||
setupDiff();
|
||||
element.hidden = true;
|
||||
assert.equal(element.getCursorStops().length, 0);
|
||||
});
|
||||
|
||||
test('getCursorStops', function() {
|
||||
test('getCursorStops', () => {
|
||||
setupDiff();
|
||||
assert.equal(element.getCursorStops().length, 50);
|
||||
});
|
||||
});
|
||||
|
||||
test('adds .hiddenscroll', function() {
|
||||
test('adds .hiddenscroll', () => {
|
||||
Gerrit.hiddenscroll = true;
|
||||
element.displayLine = true;
|
||||
assert.include(element.$$('.diffContainer').className, 'hiddenscroll');
|
||||
});
|
||||
});
|
||||
|
||||
suite('logged in', function() {
|
||||
setup(function() {
|
||||
suite('logged in', () => {
|
||||
setup(() => {
|
||||
stub('gr-rest-api-interface', {
|
||||
getLoggedIn: function() { return Promise.resolve(true); },
|
||||
getPreferences: function() {
|
||||
getLoggedIn() { return Promise.resolve(true); },
|
||||
getPreferences() {
|
||||
return Promise.resolve({time_format: 'HHMM_12'});
|
||||
},
|
||||
});
|
||||
element = fixture('basic');
|
||||
});
|
||||
|
||||
test('get drafts', function(done) {
|
||||
test('get drafts', done => {
|
||||
element.patchRange = {basePatchNum: 0, patchNum: 0};
|
||||
var draftsResponse = {
|
||||
const draftsResponse = {
|
||||
baseComments: [{id: 'foo'}],
|
||||
comments: [{id: 'bar'}],
|
||||
};
|
||||
var getDraftsStub = sandbox.stub(element.$.restAPI, 'getDiffDrafts',
|
||||
function() { return Promise.resolve(draftsResponse); });
|
||||
element._getDiffDrafts().then(function(result) {
|
||||
sandbox.stub(element.$.restAPI, 'getDiffDrafts',
|
||||
() => Promise.resolve(draftsResponse));
|
||||
element._getDiffDrafts().then(result => {
|
||||
assert.deepEqual(result, draftsResponse);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('get comments and drafts', function(done) {
|
||||
var comments = {
|
||||
test('get comments and drafts', done => {
|
||||
const comments = {
|
||||
baseComments: [
|
||||
{id: 'bc1', __commentSide: 'left'},
|
||||
{id: 'bc2', __commentSide: 'left'},
|
||||
@@ -756,10 +753,10 @@ limitations under the License.
|
||||
{id: 'c2', __commentSide: 'right'},
|
||||
],
|
||||
};
|
||||
var diffCommentsStub = sandbox.stub(element, '_getDiffComments',
|
||||
function() { return Promise.resolve(comments); });
|
||||
sandbox.stub(element, '_getDiffComments',
|
||||
() => Promise.resolve(comments));
|
||||
|
||||
var drafts = {
|
||||
const drafts = {
|
||||
baseComments: [
|
||||
{id: 'bd1', __commentSide: 'left'},
|
||||
{id: 'bd2', __commentSide: 'left'},
|
||||
@@ -770,10 +767,9 @@ limitations under the License.
|
||||
],
|
||||
};
|
||||
|
||||
var diffDraftsStub = sandbox.stub(element, '_getDiffDrafts',
|
||||
function() { return Promise.resolve(drafts); });
|
||||
sandbox.stub(element, '_getDiffDrafts', () => Promise.resolve(drafts));
|
||||
|
||||
var robotComments = {
|
||||
const robotComments = {
|
||||
baseComments: [
|
||||
{id: 'br1', __commentSide: 'left'},
|
||||
{id: 'br2', __commentSide: 'left'},
|
||||
@@ -784,9 +780,8 @@ limitations under the License.
|
||||
],
|
||||
};
|
||||
|
||||
var diffRobotCommentStub = sandbox.stub(element,
|
||||
'_getDiffRobotComments', function() {
|
||||
return Promise.resolve(robotComments); });
|
||||
sandbox.stub(element,
|
||||
'_getDiffRobotComments', () => Promise.resolve(robotComments));
|
||||
|
||||
element.changeNum = '42';
|
||||
element.patchRange = {
|
||||
@@ -796,7 +791,7 @@ limitations under the License.
|
||||
element.path = '/path/to/foo';
|
||||
element.projectConfig = {foo: 'bar'};
|
||||
|
||||
element._getDiffCommentsAndDrafts().then(function(result) {
|
||||
element._getDiffCommentsAndDrafts().then(result => {
|
||||
assert.deepEqual(result, {
|
||||
meta: {
|
||||
changeNum: '42',
|
||||
@@ -829,26 +824,26 @@ limitations under the License.
|
||||
});
|
||||
});
|
||||
|
||||
test('addDraftAtLine', function(done) {
|
||||
var fakeLineEl = {getAttribute: sandbox.stub().returns(42)};
|
||||
test('addDraftAtLine', done => {
|
||||
const fakeLineEl = {getAttribute: sandbox.stub().returns(42)};
|
||||
sandbox.stub(element, '_selectLine');
|
||||
sandbox.stub(element, '_addDraft');
|
||||
var loggedInErrorSpy = sandbox.spy();
|
||||
const loggedInErrorSpy = sandbox.spy();
|
||||
element.addEventListener('show-auth-required', loggedInErrorSpy);
|
||||
element.addDraftAtLine(fakeLineEl);
|
||||
flush(function() {
|
||||
flush(() => {
|
||||
assert.isFalse(loggedInErrorSpy.called);
|
||||
assert.isTrue(element._addDraft.calledWithExactly(fakeLineEl, 42));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
suite('change in preferences', function() {
|
||||
setup(function() {
|
||||
suite('change in preferences', () => {
|
||||
setup(() => {
|
||||
element._diff = {
|
||||
meta_a: {name: 'carrot.jpg', content_type: 'image/jpeg', lines: 66},
|
||||
meta_b: {name: 'carrot.jpg', content_type: 'image/jpeg',
|
||||
lines: 560},
|
||||
lines: 560},
|
||||
diff_header: [],
|
||||
intraline_status: 'OK',
|
||||
change_type: 'MODIFIED',
|
||||
@@ -879,7 +874,7 @@ limitations under the License.
|
||||
};
|
||||
});
|
||||
|
||||
test('change in preferences re-renders diff', function() {
|
||||
test('change in preferences re-renders diff', () => {
|
||||
sandbox.stub(element, '_renderDiffTable');
|
||||
element.prefs = {};
|
||||
element.prefs = {time_format: 'HHMM_12'};
|
||||
@@ -887,7 +882,7 @@ limitations under the License.
|
||||
});
|
||||
|
||||
test('change in preferences does not re-renders diff with ' +
|
||||
'noRenderOnPrefsChange', function() {
|
||||
'noRenderOnPrefsChange', () => {
|
||||
sandbox.stub(element, '_renderDiffTable');
|
||||
element.noRenderOnPrefsChange = true;
|
||||
element.prefs = {};
|
||||
@@ -896,9 +891,8 @@ limitations under the License.
|
||||
});
|
||||
});
|
||||
|
||||
suite('handle comment-update', function() {
|
||||
|
||||
setup(function() {
|
||||
suite('handle comment-update', () => {
|
||||
setup(() => {
|
||||
element._comments = {
|
||||
meta: {
|
||||
changeNum: '42',
|
||||
@@ -924,24 +918,26 @@ limitations under the License.
|
||||
};
|
||||
});
|
||||
|
||||
test('creating a draft', function() {
|
||||
var comment = {__draft: true, __draftID: 'tempID', side: 'PARENT',
|
||||
__commentSide: 'left'};
|
||||
element.fire('comment-update', {comment: comment});
|
||||
test('creating a draft', () => {
|
||||
const comment = {__draft: true, __draftID: 'tempID', side: 'PARENT',
|
||||
__commentSide: 'left'};
|
||||
element.fire('comment-update', {comment});
|
||||
assert.include(element._comments.left, comment);
|
||||
});
|
||||
|
||||
test('saving a draft', function() {
|
||||
var draftID = 'tempID';
|
||||
var id = 'savedID';
|
||||
element._comments.left.push(
|
||||
{__draft: true, __draftID: draftID, side: 'PARENT',
|
||||
__commentSide: 'left'});
|
||||
element.fire('comment-update', {comment:
|
||||
{id: id, __draft: true, __draftID: draftID, side: 'PARENT',
|
||||
__commentSide: 'left'},
|
||||
});
|
||||
var drafts = element._comments.left.filter(function(item) {
|
||||
test('saving a draft', () => {
|
||||
const draftID = 'tempID';
|
||||
const id = 'savedID';
|
||||
let comment = {
|
||||
__draft: true,
|
||||
__draftID: draftID,
|
||||
side: 'PARENT',
|
||||
__commentSide: 'left',
|
||||
};
|
||||
element._comments.left.push(comment);
|
||||
comment.id = id;
|
||||
element.fire('comment-update', {comment});
|
||||
const drafts = element._comments.left.filter(item => {
|
||||
return item.__draftID === draftID;
|
||||
});
|
||||
assert.equal(drafts.length, 1);
|
||||
@@ -950,20 +946,20 @@ limitations under the License.
|
||||
});
|
||||
});
|
||||
|
||||
suite('diff header', function() {
|
||||
setup(function() {
|
||||
suite('diff header', () => {
|
||||
setup(() => {
|
||||
element._diff = {
|
||||
meta_a: {name: 'carrot.jpg', content_type: 'image/jpeg', lines: 66},
|
||||
meta_b: {name: 'carrot.jpg', content_type: 'image/jpeg',
|
||||
lines: 560},
|
||||
diff_header: [],
|
||||
intraline_status: 'OK',
|
||||
change_type: 'MODIFIED',
|
||||
content: [{skip: 66}],
|
||||
};
|
||||
meta_a: {name: 'carrot.jpg', content_type: 'image/jpeg', lines: 66},
|
||||
meta_b: {name: 'carrot.jpg', content_type: 'image/jpeg',
|
||||
lines: 560},
|
||||
diff_header: [],
|
||||
intraline_status: 'OK',
|
||||
change_type: 'MODIFIED',
|
||||
content: [{skip: 66}],
|
||||
};
|
||||
});
|
||||
|
||||
test('hidden', function() {
|
||||
test('hidden', () => {
|
||||
assert.equal(element._diffHeaderItems.length, 0);
|
||||
element.push('_diff.diff_header', 'diff --git a/test.jpg b/test.jpg');
|
||||
assert.equal(element._diffHeaderItems.length, 0);
|
||||
|
||||
Reference in New Issue
Block a user