Move file list fetch logic into rest API interface
Consolidating the logic for which API request to make in order to get a fully populated file list into the API interface simplifies things. Making UI components like the file list patchset-agnostic follows a pattern set in previous edit related changes and centralizes fetching logic. Also removes some spurious/redundant tests. Bug: Issue 4437 Change-Id: I21361ef3af8ab50c085248292b43bd4f8325afab
This commit is contained in:
@@ -408,10 +408,6 @@
|
||||
},
|
||||
|
||||
_getFiles() {
|
||||
if (this.editLoaded) {
|
||||
return this.$.restAPI.getChangeEditFilesAsSpeciallySortedArray(
|
||||
this.changeNum, this.patchRange);
|
||||
}
|
||||
return this.$.restAPI.getChangeFilesAsSpeciallySortedArray(
|
||||
this.changeNum, this.patchRange);
|
||||
},
|
||||
|
@@ -105,82 +105,6 @@ limitations under the License.
|
||||
element.numFilesShown);
|
||||
});
|
||||
|
||||
test('get file list', done => {
|
||||
const getChangeFilesStub = sandbox.stub(element.$.restAPI, 'getChangeFiles',
|
||||
() => {
|
||||
return Promise.resolve({
|
||||
'/COMMIT_MSG': {lines_inserted: 9},
|
||||
'tags.html': {lines_deleted: 123},
|
||||
'about.txt': {},
|
||||
});
|
||||
});
|
||||
|
||||
element._getFiles().then(files => {
|
||||
const filenames = files.map(f => { return f.__path; });
|
||||
assert.deepEqual(filenames, ['/COMMIT_MSG', 'about.txt', 'tags.html']);
|
||||
assert.deepEqual(files[0], {
|
||||
lines_inserted: 9,
|
||||
lines_deleted: 0,
|
||||
__path: '/COMMIT_MSG',
|
||||
});
|
||||
assert.deepEqual(files[1], {
|
||||
lines_inserted: 0,
|
||||
lines_deleted: 0,
|
||||
__path: 'about.txt',
|
||||
});
|
||||
assert.deepEqual(files[2], {
|
||||
lines_inserted: 0,
|
||||
lines_deleted: 123,
|
||||
__path: 'tags.html',
|
||||
});
|
||||
|
||||
getChangeFilesStub.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('get file list with change edit', done => {
|
||||
element.editLoaded = true;
|
||||
|
||||
sandbox.stub(element.$.restAPI,
|
||||
'getChangeEditFiles', () => {
|
||||
return Promise.resolve({
|
||||
commit: {},
|
||||
files: {
|
||||
'/COMMIT_MSG': {
|
||||
lines_inserted: 9,
|
||||
},
|
||||
'tags.html': {
|
||||
lines_deleted: 123,
|
||||
},
|
||||
'about.txt': {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
element._getFiles().then(files => {
|
||||
const filenames = files.map(f => { return f.__path; });
|
||||
assert.deepEqual(filenames, ['/COMMIT_MSG', 'about.txt', 'tags.html']);
|
||||
assert.deepEqual(files[0], {
|
||||
lines_inserted: 9,
|
||||
lines_deleted: 0,
|
||||
__path: '/COMMIT_MSG',
|
||||
});
|
||||
assert.deepEqual(files[1], {
|
||||
lines_inserted: 0,
|
||||
lines_deleted: 0,
|
||||
__path: 'about.txt',
|
||||
});
|
||||
assert.deepEqual(files[2], {
|
||||
lines_inserted: 0,
|
||||
lines_deleted: 123,
|
||||
__path: 'tags.html',
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('calculate totals for patch number', () => {
|
||||
element._files = [
|
||||
{__path: '/COMMIT_MSG', lines_inserted: 9},
|
||||
|
@@ -1002,12 +1002,12 @@
|
||||
|
||||
/**
|
||||
* @param {number|string} changeNum
|
||||
* @param {!Promise<?Object>} patchRange
|
||||
* @param {Defs.patchRange} patchRange
|
||||
*/
|
||||
getChangeEditFiles(changeNum, patchRange) {
|
||||
let endpoint = '/edit?list';
|
||||
if (patchRange.basePatchNum !== 'PARENT') {
|
||||
endpoint += '&base=' + encodeURIComponent(patchRange.basePatchNum);
|
||||
endpoint += '&base=' + encodeURIComponent(patchRange.basePatchNum + '');
|
||||
}
|
||||
return this._getChangeURLAndFetch(changeNum, endpoint);
|
||||
},
|
||||
@@ -1029,15 +1029,14 @@
|
||||
* @return {!Promise<!Array<!Object>>}
|
||||
*/
|
||||
getChangeFilesAsSpeciallySortedArray(changeNum, patchRange) {
|
||||
if (this.patchNumEquals(patchRange.patchNum, this.EDIT_NAME)) {
|
||||
return this.getChangeEditFiles(changeNum, patchRange).then(res =>
|
||||
this._normalizeChangeFilesResponse(res.files));
|
||||
}
|
||||
return this.getChangeFiles(changeNum, patchRange).then(
|
||||
this._normalizeChangeFilesResponse.bind(this));
|
||||
},
|
||||
|
||||
getChangeEditFilesAsSpeciallySortedArray(changeNum, patchRange) {
|
||||
return this.getChangeEditFiles(changeNum, patchRange).then(files =>
|
||||
this._normalizeChangeFilesResponse(files.files));
|
||||
},
|
||||
|
||||
/**
|
||||
* The closure compiler doesn't realize this.specialFilePathCompare is
|
||||
* valid.
|
||||
|
@@ -1276,5 +1276,22 @@ limitations under the License.
|
||||
|
||||
return Promise.all([edit, normal]);
|
||||
});
|
||||
|
||||
test('getChangeFilesAsSpeciallySortedArray is edit-sensitive', () => {
|
||||
const fn = element.getChangeFilesAsSpeciallySortedArray.bind(element);
|
||||
const getChangeFilesStub = sandbox.stub(element, 'getChangeFiles')
|
||||
.returns(Promise.resolve({}));
|
||||
const getChangeEditFilesStub = sandbox.stub(element, 'getChangeEditFiles')
|
||||
.returns(Promise.resolve({}));
|
||||
|
||||
return fn('1', {patchNum: 'edit'}).then(() => {
|
||||
assert.isTrue(getChangeEditFilesStub.calledOnce);
|
||||
assert.isFalse(getChangeFilesStub.called);
|
||||
return fn('1', {patchNum: '1'}).then(() => {
|
||||
assert.isTrue(getChangeEditFilesStub.calledOnce);
|
||||
assert.isTrue(getChangeFilesStub.calledOnce);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user