blob: 80aaf3b444335cd51eb4abd26d70f82c4bc2c129 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (C) 2016 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-change-actions</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<script src="../../../scripts/util.js"></script>
<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="gr-change-actions.html">
<test-fixture id="basic">
<template>
<gr-change-actions></gr-change-actions>
</template>
</test-fixture>
<script>
suite('gr-change-actions tests', function() {
var element;
setup(function() {
stub('gr-rest-api-interface', {
getChangeRevisionActions: function() {
return Promise.resolve({
cherrypick: {
method: 'POST',
label: 'Cherry Pick',
title: 'Cherry pick change to a different branch',
enabled: true
},
rebase: {
method: 'POST',
label: 'Rebase',
title: 'Rebase onto tip of branch or parent change'
},
submit: {
method: 'POST',
label: 'Submit',
title: 'Submit patch set 1 into master',
enabled: true
}
});
},
send: function(method, url, payload) {
if (method !== 'POST') { return Promise.reject('bad method'); }
if (url === '/changes/42/revisions/2/submit') {
return Promise.resolve({
ok: true,
text: function() { return Promise.resolve(')]}\'\n{}'); },
});
} else if (url === '/changes/42/revisions/2/rebase') {
return Promise.resolve({
ok: true,
text: function() { return Promise.resolve(')]}\'\n{}'); },
});
}
return Promise.reject('bad url');
},
});
element = fixture('basic');
element.changeNum = '42';
element.patchNum = '2';
return element.reload();
});
test('submit, rebase, and cherry-pick buttons show', function(done) {
flush(function() {
var buttonEls = Polymer.dom(element.root).querySelectorAll('gr-button');
assert.equal(buttonEls.length, 3);
assert.isFalse(element.hidden);
done();
});
});
test('submit change', function(done) {
flush(function() {
var submitButton = element.$$('gr-button[data-action-key="submit"]');
assert.ok(submitButton);
MockInteractions.tap(submitButton);
// Upon success it should fire the reload-change event.
element.addEventListener('reload-change', function(e) {
done();
});
});
});
test('submit change with plugin hook', function(done) {
var canSubmitStub = sinon.stub(element, '_canSubmitChange',
function() { return false; });
var fireActionStub = sinon.stub(element, '_fireAction');
flush(function() {
var submitButton = element.$$('gr-button[data-action-key="submit"]');
assert.ok(submitButton);
MockInteractions.tap(submitButton);
assert.equal(fireActionStub.callCount, 0);
canSubmitStub.restore();
fireActionStub.restore();
done();
});
});
test('rebase change', function(done) {
var fireActionStub = sinon.stub(element, '_fireAction');
flush(function() {
var rebaseButton = element.$$('gr-button[data-action-key="rebase"]');
MockInteractions.tap(rebaseButton);
var rebaseAction = {
__key: 'rebase',
__type: 'revision',
__primary: false,
label: 'Rebase',
method: 'POST',
title: 'Rebase onto tip of branch or parent change',
};
element.$.confirmRebase.base = '1234';
element._handleRebaseConfirm();
assert.deepEqual(fireActionStub.lastCall.args,
['/rebase', rebaseAction, true, {base: '1234'}]);
element.$.confirmRebase.base = '';
element._handleRebaseConfirm();
assert.deepEqual(fireActionStub.lastCall.args,
['/rebase', rebaseAction, true, {}]);
element.$.confirmRebase.base = 'does not matter';
element.$.confirmRebase.clearParent = true;
element._handleRebaseConfirm();
assert.deepEqual(fireActionStub.lastCall.args,
['/rebase', rebaseAction, true, {base: ''}]);
fireActionStub.restore();
done();
});
});
suite('cherry-pick', function() {
var fireActionStub;
var alertStub;
setup(function() {
fireActionStub = sinon.stub(element, '_fireAction');
alertStub = sinon.stub(window, 'alert');
});
teardown(function() {
alertStub.restore();
fireActionStub.restore();
});
test('works', function() {
var rebaseButton = element.$$('gr-button[data-action-key="rebase"]');
MockInteractions.tap(rebaseButton);
var action = {
__key: 'cherrypick',
__type: 'revision',
__primary: false,
enabled: true,
label: 'Cherry Pick',
method: 'POST',
title: 'Cherry pick change to a different branch',
};
element._handleCherrypickConfirm();
assert.equal(fireActionStub.callCount, 0);
element.$.confirmCherrypick.branch = 'master';
element._handleCherrypickConfirm();
assert.equal(fireActionStub.callCount, 0); // Still needs a message.
element.$.confirmCherrypick.message = 'foo message';
element._handleCherrypickConfirm();
assert.deepEqual(fireActionStub.lastCall.args, [
'/cherrypick', action, true, {
destination: 'master',
message: 'foo message',
}
]);
});
});
test('custom actions', function(done) {
// Add a button with the same key as a server-based one to ensure
// collisions are taken care of.
var key = element.addActionButton(element.ActionType.REVISION, 'Bork!');
element.addEventListener(key + '-tap', function(e) {
assert.equal(e.detail.node.getAttribute('data-action-key'), key);
element.removeActionButton(key);
flush(function() {
assert.notOk(element.$$('[data-action-key="' + key + '"]'));
done();
});
});
flush(function() {
MockInteractions.tap(element.$$('[data-action-key="' + key + '"]'));
});
});
suite('revert change', function() {
var alertStub;
var fireActionStub;
setup(function() {
fireActionStub = sinon.stub(element, '_fireAction');
alertStub = sinon.stub(window, 'alert');
element.actions = {
revert: {
method: 'POST',
label: 'Revert',
title: 'Revert the change',
enabled: true
}
};
return element.reload();
});
teardown(function() {
alertStub.restore();
fireActionStub.restore();
});
test('revert change with plugin hook', function(done) {
var newRevertMsg = 'Modified revert msg';
var modifyRevertMsgStub = sinon.stub(element, '_modifyRevertMsg',
function() { return newRevertMsg; });
var populateRevertMsgStub = sinon.stub(
element.$.confirmRevertDialog, 'populateRevertMessage',
function() { return 'original msg'; });
flush(function() {
var revertButton = element.$$('gr-button[data-action-key="revert"]');
MockInteractions.tap(revertButton);
assert.equal(element.$.confirmRevertDialog.message, newRevertMsg);
populateRevertMsgStub.restore();
modifyRevertMsgStub.restore();
done();
});
});
test('works', function() {
var populateRevertMsgStub = sinon.stub(
element.$.confirmRevertDialog, 'populateRevertMessage',
function() { return 'original msg'; });
var revertButton = element.$$('gr-button[data-action-key="revert"]');
MockInteractions.tap(revertButton);
element.$.confirmRevertDialog.message = 'foo message';
element._handleRevertDialogConfirm();
assert.notOk(alertStub.called);
var action = {
__key: 'revert',
__type: 'change',
__primary: false,
enabled: true,
label: 'Revert',
method: 'POST',
title: 'Revert the change',
};
assert.deepEqual(fireActionStub.lastCall.args, [
'/revert', action, false, {
message: 'foo message',
}]);
populateRevertMsgStub.restore();
});
});
});
</script>