blob: 5621f9ccd74b2399f01a6f528d2d9516a65afa74 [file] [log] [blame]
<!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-dashboard-view</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-dashboard-view.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-dashboard-view></gr-dashboard-view>
</template>
</test-fixture>
<script>
suite('gr-dashboard-view tests', () => {
let element;
let sandbox;
let paramsChangedPromise;
setup(() => {
stub('gr-rest-api-interface', {
getLoggedIn() { return Promise.resolve(false); },
getAccountDetails() { return Promise.resolve({}); },
getAccountStatus() { return Promise.resolve(false); },
});
element = fixture('basic');
sandbox = sinon.sandbox.create();
getChangesStub = sandbox.stub(element.$.restAPI, 'getChanges',
() => Promise.resolve([]));
let resolver;
paramsChangedPromise = new Promise(resolve => {
resolver = resolve;
});
const paramsChanged = element._paramsChanged.bind(element);
sandbox.stub(element, '_paramsChanged', params => {
paramsChanged(params).then(resolver());
});
});
teardown(() => {
sandbox.restore();
});
test('_computeTitle', () => {
assert.equal(element._computeTitle('self'), 'My Reviews');
assert.equal(element._computeTitle('not self'), 'Dashboard for not self');
});
suite('_isViewActive', () => {
test('nothing happens when user param is falsy', () => {
element.params = {};
flushAsynchronousOperations();
assert.equal(getChangesStub.callCount, 0);
element.params = {user: ''};
flushAsynchronousOperations();
assert.equal(getChangesStub.callCount, 0);
});
test('content is refreshed when user param is updated', () => {
element.params = {
view: Gerrit.Nav.View.DASHBOARD,
user: 'self',
};
return paramsChangedPromise.then(() => {
assert.equal(getChangesStub.callCount, 1);
});
});
});
suite('selfOnly sections', () => {
test('viewing self dashboard includes selfOnly sections', () => {
element.params = {
view: Gerrit.Nav.View.DASHBOARD,
sections: [
{query: '1'},
{query: '2', selfOnly: true},
],
user: 'user',
};
return paramsChangedPromise.then(() => {
assert.isTrue(
getChangesStub.calledWith(
null, ['1'], null, element.options));
});
});
test('viewing another user\'s dashboard omits selfOnly sections', () => {
element.params = {
view: Gerrit.Nav.View.DASHBOARD,
sections: [
{query: '1'},
{query: '2', selfOnly: true},
],
user: 'self',
};
return paramsChangedPromise.then(() => {
assert.isTrue(
getChangesStub.calledWith(
null, ['1', '2'], null, element.options));
});
});
});
test('suffixForDashboard is included in getChanges query', () => {
element.params = {
view: Gerrit.Nav.View.DASHBOARD,
sections: [
{query: '1'},
{query: '2', suffixForDashboard: 'suffix'},
],
};
return paramsChangedPromise.then(() => {
assert.isTrue(getChangesStub.calledOnce);
assert.deepEqual(
getChangesStub.firstCall.args,
[null, ['1', '2 suffix'], null, element.options]);
});
});
suite('_getProjectDashboard', () => {
test('dashboard with foreach', () => {
sandbox.stub(element.$.restAPI, 'getDashboard', () => Promise.resolve({
title: 'title',
// Note: ${project} should not be resolved in foreach!
foreach: 'foreach for ${project}',
sections: [
{name: 'section 1', query: 'query 1'},
{name: 'section 2', query: '${project} query 2'},
],
}));
return element._getProjectDashboard('project', '').then(dashboard => {
assert.deepEqual(
dashboard,
{
title: 'title',
sections: [
{name: 'section 1', query: 'query 1 foreach for ${project}'},
{
name: 'section 2',
query: 'project query 2 foreach for ${project}',
},
],
});
});
});
test('dashboard without foreach', () => {
sandbox.stub(element.$.restAPI, 'getDashboard', () => Promise.resolve({
title: 'title',
sections: [
{name: 'section 1', query: 'query 1'},
{name: 'section 2', query: '${project} query 2'},
],
}));
return element._getProjectDashboard('project', '').then(dashboard => {
assert.deepEqual(
dashboard,
{
title: 'title',
sections: [
{name: 'section 1', query: 'query 1'},
{name: 'section 2', query: 'project query 2'},
],
});
});
});
});
suite('_getUserDashboard', () => {
const sections = [
{name: 'section 1', query: 'query 1'},
{name: 'section 2', query: 'query 2 for ${user}'},
{name: 'section 3', query: 'self only query', selfOnly: true},
{name: 'section 4', query: 'query 4', suffixForDashboard: 'suffix'},
];
test('dashboard for self', () => {
return element._getUserDashboard('self', sections, 'title')
.then(dashboard => {
assert.deepEqual(
dashboard,
{
title: 'title',
sections: [
{name: 'section 1', query: 'query 1'},
{name: 'section 2', query: 'query 2 for self'},
{name: 'section 3', query: 'self only query'},
{
name: 'section 4',
query: 'query 4',
suffixForDashboard: 'suffix',
},
],
});
});
});
test('dashboard for other user', () => {
return element._getUserDashboard('user', sections, 'title')
.then(dashboard => {
assert.deepEqual(
dashboard,
{
title: 'title',
sections: [
{name: 'section 1', query: 'query 1'},
{name: 'section 2', query: 'query 2 for user'},
{
name: 'section 4',
query: 'query 4',
suffixForDashboard: 'suffix',
},
],
});
});
});
});
test('_computeUserHeaderClass', () => {
assert.equal(element._computeUserHeaderClass(undefined), '');
assert.equal(element._computeUserHeaderClass(''), '');
assert.equal(element._computeUserHeaderClass('self'), 'hide');
assert.equal(element._computeUserHeaderClass('user'), '');
});
test('404 page', done => {
const response = {status: 404};
sandbox.stub(
element.$.restAPI, 'getDashboard', (project, dashboard, errFn) => {
errFn(response);
});
element.addEventListener('page-error', e => {
assert.strictEqual(e.detail.response, response);
done();
});
element.params = {
view: Gerrit.Nav.View.DASHBOARD,
project: 'project',
dashboard: 'dashboard',
};
});
});
</script>