Wyatt Allen c61ba25b11 Coalesce request for drafts with login check
Because requests for a user's draft diff comments is always nested
within a check for whether a user is logged in, these can be combined
into the same REST API interface call. The updated call is given a JSDoc
to describe the additional functionality and call sites are simplified.

Change-Id: Idc0cc6298c7287579ba76e7cc35701e62142bca3
2017-08-01 14:18:39 -07:00

197 lines
6.7 KiB
HTML

<!DOCTYPE html>
<!--
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-comment-api</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="./gr-comment-api.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-comment-api></gr-comment-api>
</template>
</test-fixture>
<script>
suite('gr-comment-api tests', () => {
const PARENT = 'PARENT';
let element;
let sandbox;
setup(() => {
sandbox = sinon.sandbox.create();
element = fixture('basic');
});
teardown(() => { sandbox.restore(); });
test('loads logged-out', () => {
const changeNum = 1234;
sandbox.stub(element.$.restAPI, 'getLoggedIn')
.returns(Promise.resolve(false));
sandbox.stub(element.$.restAPI, 'getDiffComments')
.returns(Promise.resolve({
'foo.c': [{id: '123', message: 'foo bar', in_reply_to: '321'}],
}));
sandbox.stub(element.$.restAPI, 'getDiffRobotComments')
.returns(Promise.resolve({'foo.c': [{id: '321', message: 'done'}]}));
sandbox.stub(element.$.restAPI, 'getDiffDrafts')
.returns(Promise.resolve({}));
return element.loadAll(changeNum).then(() => {
assert.isTrue(element.$.restAPI.getDiffComments.calledWithExactly(
changeNum));
assert.isTrue(element.$.restAPI.getDiffRobotComments.calledWithExactly(
changeNum));
assert.isTrue(element.$.restAPI.getDiffDrafts.calledWithExactly(
changeNum));
assert.isOk(element._comments);
assert.isOk(element._robotComments);
assert.deepEqual(element._drafts, {});
});
});
test('loads logged-in', () => {
const changeNum = 1234;
sandbox.stub(element.$.restAPI, 'getLoggedIn')
.returns(Promise.resolve(true));
sandbox.stub(element.$.restAPI, 'getDiffComments')
.returns(Promise.resolve({
'foo.c': [{id: '123', message: 'foo bar', in_reply_to: '321'}],
}));
sandbox.stub(element.$.restAPI, 'getDiffRobotComments')
.returns(Promise.resolve({'foo.c': [{id: '321', message: 'done'}]}));
sandbox.stub(element.$.restAPI, 'getDiffDrafts')
.returns(Promise.resolve({'foo.c': [{id: '555', message: 'ack'}]}));
return element.loadAll(changeNum).then(() => {
assert.isTrue(element.$.restAPI.getDiffComments.calledWithExactly(
changeNum));
assert.isTrue(element.$.restAPI.getDiffRobotComments.calledWithExactly(
changeNum));
assert.isTrue(element.$.restAPI.getDiffDrafts.calledWithExactly(
changeNum));
assert.isOk(element._comments);
assert.isOk(element._robotComments);
assert.notDeepEqual(element._drafts, {});
});
});
test('_isInBaseOfPatchRange', () => {
const comment = {patch_set: 1};
const patchRange = {basePatchNum: 1, patchNum: 2};
assert.isTrue(element._isInBaseOfPatchRange(comment, patchRange));
patchRange.basePatchNum = PARENT;
assert.isFalse(element._isInBaseOfPatchRange(comment, patchRange));
comment.side = PARENT;
assert.isFalse(element._isInBaseOfPatchRange(comment, patchRange));
comment.patch_set = 2;
assert.isTrue(element._isInBaseOfPatchRange(comment, patchRange));
});
test('_isInRevisionOfPatchRange', () => {
const comment = {patch_set: 123};
const patchRange = {basePatchNum: 122, patchNum: 124};
assert.isFalse(element._isInRevisionOfPatchRange(comment, patchRange));
patchRange.patchNum = 123;
assert.isTrue(element._isInRevisionOfPatchRange(comment, patchRange));
comment.side = PARENT;
assert.isFalse(element._isInRevisionOfPatchRange(comment, patchRange));
});
suite('comment ranges and paths', () => {
setup(() => {
element._changeNum = 1234;
element._drafts = {};
element._robotComments = {};
element._comments = {
'file/one': [
{patch_set: 2, side: PARENT},
{patch_set: 2},
],
'file/two': [
{patch_set: 2},
{patch_set: 3},
],
'file/three': [
{patch_set: 2, side: PARENT},
{patch_set: 3},
],
};
});
test('getPaths', () => {
const patchRange = {basePatchNum: 1, patchNum: 4};
let paths = element.getPaths(patchRange);
assert.equal(Object.keys(paths).length, 0);
patchRange.basePatchNum = PARENT;
patchRange.patchNum = 3;
paths = element.getPaths(patchRange);
assert.notProperty(paths, 'file/one');
assert.property(paths, 'file/two');
assert.property(paths, 'file/three');
patchRange.patchNum = 2;
paths = element.getPaths(patchRange);
assert.property(paths, 'file/one');
assert.property(paths, 'file/two');
assert.property(paths, 'file/three');
});
test('getCommentsForPath', () => {
const patchRange = {basePatchNum: 1, patchNum: 3};
let path = 'file/one';
let comments = element.getCommentsForPath(path, patchRange);
assert.equal(comments.meta.changeNum, 1234);
assert.equal(comments.left.length, 0);
assert.equal(comments.right.length, 0);
path = 'file/two';
comments = element.getCommentsForPath(path, patchRange);
assert.equal(comments.left.length, 0);
assert.equal(comments.right.length, 1);
patchRange.basePatchNum = 2;
comments = element.getCommentsForPath(path, patchRange);
assert.equal(comments.left.length, 1);
assert.equal(comments.right.length, 1);
patchRange.basePatchNum = PARENT;
path = 'file/three';
comments = element.getCommentsForPath(path, patchRange);
assert.equal(comments.left.length, 0);
assert.equal(comments.right.length, 1);
});
});
});
</script>