Files
gerrit/polygerrit-ui/app/elements/change/gr-file-list-header/gr-file-list-header_test.html
Becky Siegel 89fe0501e1 Utilize gr-patch-range-select in the change view
Slight refactoring required in order to satisfy both use cases:
- Fire an event when patch range changes and let parent element
  handle it.
- Support comment strings
- availablePatches becomes an array of objects instead of an
  array of integers.

Change-Id: Ia8da9296f41eb2d45c9358d03fbec3940273ad9d
2017-09-14 13:47:38 -07:00

284 lines
9.2 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-file-list-header</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"/>
<script src="../../../bower_components/page/page.js"></script>
<link rel="import" href="gr-file-list-header.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-file-list-header></gr-file-list-header>
</template>
</test-fixture>
<test-fixture id="blank">
<template>
<div></div>
</template>
</test-fixture>
<script>
suite('gr-file-list-header tests', () => {
let element;
let sandbox;
let navigateToChangeStub;
setup(() => {
sandbox = sinon.sandbox.create();
navigateToChangeStub = sandbox.stub(Gerrit.Nav, 'navigateToChange');
stub('gr-rest-api-interface', {
getConfig() { return Promise.resolve({test: 'config'}); },
getAccount() { return Promise.resolve(null); },
_fetchSharedCacheURL() { return Promise.resolve({}); },
});
element = fixture('basic');
});
teardown(done => {
flush(() => {
sandbox.restore();
done();
});
});
test('Diff preferences hidden when no prefs or logged out', () => {
element.loggedIn = false;
flushAsynchronousOperations();
assert.isTrue(element.$.diffPrefsContainer.hidden);
element.loggedIn = true;
flushAsynchronousOperations();
assert.isTrue(element.$.diffPrefsContainer.hidden);
element.loggedIn = false;
element.diffPrefs = {font_size: '12'};
flushAsynchronousOperations();
assert.isTrue(element.$.diffPrefsContainer.hidden);
element.loggedIn = true;
flushAsynchronousOperations();
assert.isFalse(element.$.diffPrefsContainer.hidden);
});
test('_computeDescriptionReadOnly', () => {
assert.equal(element._computeDescriptionReadOnly(false,
{owner: {_account_id: 1}}, {_account_id: 1}), true);
assert.equal(element._computeDescriptionReadOnly(true,
{owner: {_account_id: 0}}, {_account_id: 1}), true);
assert.equal(element._computeDescriptionReadOnly(true,
{owner: {_account_id: 1}}, {_account_id: 1}), false);
});
test('_computeDescriptionPlaceholder', () => {
assert.equal(element._computeDescriptionPlaceholder(true),
'No patch set description');
assert.equal(element._computeDescriptionPlaceholder(false),
'Add a patch set description');
});
test('_computePatchSetDisabled', () => {
element.revisions = [
{_number: 1},
{_number: 2},
{_number: element.EDIT_NAME, basePatchNum: 2},
{_number: 3},
];
let basePatchNum = 'PARENT';
let patchNum = 1;
assert.equal(element._computePatchSetDisabled(patchNum, basePatchNum),
false);
basePatchNum = 1;
assert.equal(element._computePatchSetDisabled(patchNum, basePatchNum),
true);
patchNum = 2;
assert.equal(element._computePatchSetDisabled(patchNum, basePatchNum),
false);
basePatchNum = element.EDIT_NAME;
assert.equal(element._computePatchSetDisabled(patchNum, basePatchNum),
true);
patchNum = '3';
assert.equal(element._computePatchSetDisabled(patchNum, basePatchNum),
false);
});
test('_handleDescriptionChanged', () => {
const putDescStub = sandbox.stub(element.$.restAPI, 'setDescription')
.returns(Promise.resolve({ok: true}));
sandbox.stub(element, '_computeDescriptionReadOnly');
element.changeNum = '42';
element.patchRange = {
basePatchNum: 'PARENT',
patchNum: 1,
};
element.change = {
change_id: 'Iad9dc96274af6946f3632be53b106ef80f7ba6ca',
revisions: {
rev1: {_number: 1, description: 'test', commit: {commit: 'rev1'}},
},
current_revision: 'rev1',
status: 'NEW',
labels: {},
actions: {},
owner: {_account_id: 1},
};
element.account = {_account_id: 1};
element.loggedIn = true;
flushAsynchronousOperations();
const label = element.$.descriptionLabel;
assert.equal(label.value, 'test');
label.editing = true;
label._inputText = 'test2';
label._save();
flushAsynchronousOperations();
assert.isTrue(putDescStub.called);
assert.equal(putDescStub.args[0][2], 'test2');
});
test('expandAllDiffs called when expand button clicked', () => {
element.shownFileCount = 1;
flushAsynchronousOperations();
sandbox.stub(element, '_expandAllDiffs');
MockInteractions.tap(Polymer.dom(element.root).querySelector(
'#expandBtn'));
assert.isTrue(element._expandAllDiffs.called);
});
test('collapseAllDiffs called when expand button clicked', () => {
element.shownFileCount = 1;
flushAsynchronousOperations();
sandbox.stub(element, '_collapseAllDiffs');
MockInteractions.tap(Polymer.dom(element.root).querySelector(
'#collapseBtn'));
assert.isTrue(element._collapseAllDiffs.called);
});
test('show/hide diffs disabled for large amounts of files', done => {
const computeSpy = sandbox.spy(element, '_fileListActionsVisible');
element._files = [];
element.changeNum = '42';
element.patchRange = {
basePatchNum: 'PARENT',
patchNum: '2',
};
element.shownFileCount = 1;
flush(() => {
assert.isTrue(computeSpy.lastCall.returnValue);
_.times(element._maxFilesForBulkActions + 1, () => {
element.shownFileCount = element.shownFileCount + 1;
});
assert.isFalse(computeSpy.lastCall.returnValue);
done();
});
});
test('diff mode selector is set correctly', () => {
const select = element.$.modeSelect;
element.diffViewMode = 'SIDE_BY_SIDE';
flushAsynchronousOperations();
assert.equal(select.nativeSelect.value, 'SIDE_BY_SIDE');
element.diffViewMode = 'UNIFIED_DIFF';
flushAsynchronousOperations();
assert.equal(select.nativeSelect.value, 'UNIFIED_DIFF');
});
test('_changePatchNum called when range select changes', () => {
const leftPatch = 1;
const rightPatch = 2;
sandbox.stub(element, '_changePatchNum');
element.$.rangeSelect.fire('patch-range-change', {leftPatch, rightPatch});
assert.isTrue(element._changePatchNum.lastCall
.calledWithExactly(1, 2, true));
});
test('include base patch when not parent', () => {
element.changeNum = '42';
element.patchRange = {
basePatchNum: '2',
patchNum: '3',
};
element.change = {
change_id: 'Iad9dc96274af6946f3632be53b106ef80f7ba6ca',
revisions: {
rev2: {_number: 2},
rev1: {_number: 1},
rev13: {_number: 13},
rev3: {_number: 3},
},
status: 'NEW',
labels: {},
};
element._changePatchNum(2, 13);
assert.isTrue(navigateToChangeStub.lastCall.calledWithExactly(
element.change, 13, 2));
element.patchRange.basePatchNum = 'PARENT';
element._changePatchNum('PARENT', 3);
assert.isTrue(navigateToChangeStub.lastCall.calledWithExactly(
element.change, 3, 'PARENT'));
});
test('class is applied to file list on old patch set', () => {
const allPatchSets = [{num: 1}, {num: 2}, {num: 4}];
assert.equal(element._computePatchInfoClass('1', allPatchSets),
'patchInfoOldPatchSet');
assert.equal(element._computePatchInfoClass('2', allPatchSets),
'patchInfoOldPatchSet');
assert.equal(element._computePatchInfoClass('4', allPatchSets), '');
});
suite('editLoaded behavior', () => {
setup(() => {
element.loggedIn = true;
element.diffPrefs = {};
});
const isVisible = el => {
assert.ok(el);
return getComputedStyle(el).getPropertyValue('display') !== 'none';
};
test('patch specific elements', () => {
element.editLoaded = true;
sandbox.stub(element, 'computeLatestPatchNum').returns('2');
flushAsynchronousOperations();
assert.isFalse(isVisible(element.$.diffPrefsContainer));
assert.isFalse(isVisible(element.$$('.descriptionContainer')));
element.editLoaded = false;
flushAsynchronousOperations();
assert.isTrue(isVisible(element.$$('.descriptionContainer')));
assert.isTrue(isVisible(element.$.diffPrefsContainer));
});
});
});
</script>