ES6ify /gr-settings-view/*

Bug: Issue 6179
Change-Id: I1e937aad7ef5d9f14543287ae4b6b5c1fdc7fedb
This commit is contained in:
Kasper Nilsson
2017-05-15 17:44:27 -07:00
parent 00363faafc
commit d5bc5bd94a
2 changed files with 143 additions and 144 deletions

View File

@@ -14,7 +14,7 @@
(function() {
'use strict';
var PREFS_SECTION_FIELDS = [
const PREFS_SECTION_FIELDS = [
'changes_per_page',
'date_format',
'time_format',
@@ -43,11 +43,11 @@
properties: {
prefs: {
type: Object,
value: function() { return {}; },
value() { return {}; },
},
params: {
type: Object,
value: function() { return {}; },
value() { return {}; },
},
_accountNameMutable: Boolean,
_accountInfoChanged: Boolean,
@@ -55,15 +55,15 @@
_changeTableColumnsNotDisplayed: Array,
_localPrefs: {
type: Object,
value: function() { return {}; },
value() { return {}; },
},
_localChangeTableColumns: {
type: Array,
value: function() { return []; },
value() { return []; },
},
_localMenu: {
type: Array,
value: function() { return []; },
value() { return []; },
},
_loading: {
type: Boolean,
@@ -122,68 +122,68 @@
'_handleChangeTableChanged(_localChangeTableColumns)',
],
attached: function() {
attached() {
this.fire('title-change', {title: 'Settings'});
var promises = [
const promises = [
this.$.accountInfo.loadData(),
this.$.watchedProjectsEditor.loadData(),
this.$.groupList.loadData(),
this.$.httpPass.loadData(),
];
promises.push(this.$.restAPI.getPreferences().then(function(prefs) {
promises.push(this.$.restAPI.getPreferences().then(prefs => {
this.prefs = prefs;
this._copyPrefs('_localPrefs', 'prefs');
this._cloneMenu();
this._cloneChangeTableColumns();
}.bind(this)));
}));
promises.push(this.$.restAPI.getDiffPreferences().then(function(prefs) {
promises.push(this.$.restAPI.getDiffPreferences().then(prefs => {
this._diffPrefs = prefs;
}.bind(this)));
}));
promises.push(this.$.restAPI.getConfig().then(function(config) {
promises.push(this.$.restAPI.getConfig().then(config => {
this._serverConfig = config;
if (this._serverConfig.sshd) {
return this.$.sshEditor.loadData();
}
}.bind(this)));
}));
if (this.params.emailToken) {
promises.push(this.$.restAPI.confirmEmail(this.params.emailToken).then(
function(message) {
if (message) {
this.fire('show-alert', {message: message});
}
this.$.emailEditor.loadData();
}.bind(this)));
message => {
if (message) {
this.fire('show-alert', {message});
}
this.$.emailEditor.loadData();
}));
} else {
promises.push(this.$.emailEditor.loadData());
}
this._loadingPromise = Promise.all(promises).then(function() {
this._loadingPromise = Promise.all(promises).then(() => {
this._loading = false;
}.bind(this));
});
this.listen(window, 'scroll', '_handleBodyScroll');
},
detached: function() {
detached() {
this.unlisten(window, 'scroll', '_handleBodyScroll');
},
reloadAccountDetail: function() {
reloadAccountDetail() {
Promise.all([
this.$.accountInfo.loadData(),
this.$.emailEditor.loadData(),
]);
},
_handleBodyScroll: function(e) {
_handleBodyScroll(e) {
if (this._headerHeight === undefined) {
var top = this.$.settingsNav.offsetTop;
for (var offsetParent = this.$.settingsNav.offsetParent;
let top = this.$.settingsNav.offsetTop;
for (let offsetParent = this.$.settingsNav.offsetParent;
offsetParent;
offsetParent = offsetParent.offsetParent) {
top += offsetParent.offsetTop;
@@ -195,163 +195,163 @@
window.scrollY >= this._headerHeight);
},
_isLoading: function() {
_isLoading() {
return this._loading || this._loading === undefined;
},
_copyPrefs: function(to, from) {
for (var i = 0; i < PREFS_SECTION_FIELDS.length; i++) {
_copyPrefs(to, from) {
for (let i = 0; i < PREFS_SECTION_FIELDS.length; i++) {
this.set([to, PREFS_SECTION_FIELDS[i]],
this[from][PREFS_SECTION_FIELDS[i]]);
}
},
_cloneMenu: function() {
var menu = [];
this.prefs.my.forEach(function(item) {
_cloneMenu() {
const menu = [];
for (const item of this.prefs.my) {
menu.push({
name: item.name,
url: item.url,
target: item.target,
});
});
}
this._localMenu = menu;
},
_cloneChangeTableColumns: function() {
var columns = this.prefs.change_table;
_cloneChangeTableColumns() {
let columns = this.prefs.change_table;
if (columns.length === 0) {
columns = this.columnNames;
this._changeTableColumnsNotDisplayed = [];
} else {
this._changeTableColumnsNotDisplayed = this.getComplementColumns(
this.prefs.change_table);
this.prefs.change_table);
}
this._localChangeTableColumns = columns;
},
_formatChangeTableColumns: function(changeTableArray) {
return changeTableArray.map(function(item) {
_formatChangeTableColumns(changeTableArray) {
return changeTableArray.map(item => {
return {column: item};
});
},
_handleChangeTableChanged: function() {
_handleChangeTableChanged() {
if (this._isLoading()) { return; }
this._changeTableChanged = true;
},
_handlePrefsChanged: function(prefs) {
_handlePrefsChanged(prefs) {
if (this._isLoading()) { return; }
this._prefsChanged = true;
},
_handleDiffPrefsChanged: function() {
_handleDiffPrefsChanged() {
if (this._isLoading()) { return; }
this._diffPrefsChanged = true;
},
_handleExpandInlineDiffsChanged: function() {
_handleExpandInlineDiffsChanged() {
this.set('_localPrefs.expand_inline_diffs',
this.$.expandInlineDiffs.checked);
},
_handlePublishCommentsOnPushChanged: function() {
_handlePublishCommentsOnPushChanged() {
this.set('_localPrefs.publish_comments_on_push',
this.$.publishCommentsOnPush.checked);
},
_handleMenuChanged: function() {
_handleMenuChanged() {
if (this._isLoading()) { return; }
this._menuChanged = true;
},
_handleSaveAccountInfo: function() {
_handleSaveAccountInfo() {
this.$.accountInfo.save();
},
_handleSavePreferences: function() {
_handleSavePreferences() {
this._copyPrefs('prefs', '_localPrefs');
return this.$.restAPI.savePreferences(this.prefs).then(function() {
return this.$.restAPI.savePreferences(this.prefs).then(() => {
this._prefsChanged = false;
}.bind(this));
});
},
_handleLineWrappingChanged: function() {
_handleLineWrappingChanged() {
this.set('_diffPrefs.line_wrapping', this.$.lineWrapping.checked);
},
_handleShowTabsChanged: function() {
_handleShowTabsChanged() {
this.set('_diffPrefs.show_tabs', this.$.showTabs.checked);
},
_handleShowTrailingWhitespaceChanged: function() {
_handleShowTrailingWhitespaceChanged() {
this.set('_diffPrefs.show_whitespace_errors',
this.$.showTrailingWhitespace.checked);
},
_handleSyntaxHighlightingChanged: function() {
_handleSyntaxHighlightingChanged() {
this.set('_diffPrefs.syntax_highlighting',
this.$.syntaxHighlighting.checked);
},
_handleSaveChangeTable: function() {
_handleSaveChangeTable() {
this.set('prefs.change_table', this._localChangeTableColumns);
this._cloneChangeTableColumns();
return this.$.restAPI.savePreferences(this.prefs).then(function() {
return this.$.restAPI.savePreferences(this.prefs).then(() => {
this._changeTableChanged = false;
}.bind(this));
});
},
_handleSaveDiffPreferences: function() {
_handleSaveDiffPreferences() {
return this.$.restAPI.saveDiffPreferences(this._diffPrefs)
.then(function() {
.then(() => {
this._diffPrefsChanged = false;
}.bind(this));
});
},
_handleSaveMenu: function() {
_handleSaveMenu() {
this.set('prefs.my', this._localMenu);
this._cloneMenu();
return this.$.restAPI.savePreferences(this.prefs).then(function() {
return this.$.restAPI.savePreferences(this.prefs).then(() => {
this._menuChanged = false;
}.bind(this));
});
},
_handleSaveWatchedProjects: function() {
_handleSaveWatchedProjects() {
this.$.watchedProjectsEditor.save();
},
_computeHeaderClass: function(changed) {
_computeHeaderClass(changed) {
return changed ? 'edited' : '';
},
_handleSaveEmails: function() {
_handleSaveEmails() {
this.$.emailEditor.save();
},
_handleNewEmailKeydown: function(e) {
_handleNewEmailKeydown(e) {
if (e.keyCode === 13) { // Enter
e.stopPropagation();
this._handleAddEmailButton();
}
},
_isNewEmailValid: function(newEmail) {
return newEmail.indexOf('@') !== -1;
_isNewEmailValid(newEmail) {
return newEmail.includes('@');
},
_computeAddEmailButtonEnabled: function(newEmail, addingEmail) {
_computeAddEmailButtonEnabled(newEmail, addingEmail) {
return this._isNewEmailValid(newEmail) && !addingEmail;
},
_handleAddEmailButton: function() {
_handleAddEmailButton() {
if (!this._isNewEmailValid(this._newEmail)) { return; }
this._addingEmail = true;
this.$.restAPI.addAccountEmail(this._newEmail).then(function(response) {
this.$.restAPI.addAccountEmail(this._newEmail).then(response => {
this._addingEmail = false;
// If it was unsuccessful.
@@ -359,7 +359,7 @@
this._lastSentVerificationEmail = this._newEmail;
this._newEmail = '';
}.bind(this));
});
},
});
})();

View File

@@ -39,18 +39,18 @@ limitations under the License.
</test-fixture>
<script>
suite('gr-settings-view tests', function() {
var element;
var account;
var preferences;
var diffPreferences;
var config;
var sandbox;
suite('gr-settings-view tests', () => {
let element;
let account;
let preferences;
let diffPreferences;
let config;
let sandbox;
function valueOf(title, fieldsetid) {
var sections = element.$[fieldsetid].querySelectorAll('section');
var titleEl;
for (var i = 0; i < sections.length; i++) {
const sections = element.$[fieldsetid].querySelectorAll('section');
let titleEl;
for (let i = 0; i < sections.length; i++) {
titleEl = sections[i].querySelector('.title');
if (titleEl.textContent === title) {
return sections[i].querySelector('.value');
@@ -61,7 +61,7 @@ limitations under the License.
// Because deepEqual isn't behaving in Safari.
function assertMenusEqual(actual, expected) {
assert.equal(actual.length, expected.length);
for (var i = 0; i < actual.length; i++) {
for (let i = 0; i < actual.length; i++) {
assert.equal(actual[i].name, expected[i].name);
assert.equal(actual[i].url, expected[i].url);
}
@@ -69,10 +69,10 @@ limitations under the License.
function stubAddAccountEmail(statusCode) {
return sandbox.stub(element.$.restAPI, 'addAccountEmail',
function() { return Promise.resolve({status: statusCode}); });
() => { return Promise.resolve({status: statusCode}); });
}
setup(function(done) {
setup(done => {
sandbox = sinon.sandbox.create();
account = {
_account_id: 123,
@@ -109,23 +109,23 @@ limitations under the License.
syntax_highlighting: true,
auto_hide_diff_table_header: true,
theme: 'DEFAULT',
ignore_whitespace: 'IGNORE_NONE'
ignore_whitespace: 'IGNORE_NONE',
};
config = {auth: {editable_account_fields: []}};
stub('gr-rest-api-interface', {
getLoggedIn: function() { return Promise.resolve(true); },
getAccount: function() { return Promise.resolve(account); },
getPreferences: function() { return Promise.resolve(preferences); },
getDiffPreferences: function() {
getLoggedIn() { return Promise.resolve(true); },
getAccount() { return Promise.resolve(account); },
getPreferences() { return Promise.resolve(preferences); },
getDiffPreferences() {
return Promise.resolve(diffPreferences);
},
getWatchedProjects: function() {
getWatchedProjects() {
return Promise.resolve([]);
},
getAccountEmails: function() { return Promise.resolve(); },
getConfig: function() { return Promise.resolve(config); },
getAccountGroups: function() { return Promise.resolve([]); },
getAccountEmails() { return Promise.resolve(); },
getConfig() { return Promise.resolve(config); },
getAccountGroups() { return Promise.resolve([]); },
});
element = fixture('basic');
@@ -133,19 +133,19 @@ limitations under the License.
element._loadingPromise.then(done);
});
teardown(function() {
teardown(() => {
sandbox.restore();
});
test('calls the title-change event', function() {
var titleChangedStub = sandbox.stub();
test('calls the title-change event', () => {
const titleChangedStub = sandbox.stub();
// Create a new view.
var newElement = document.createElement('gr-settings-view');
const newElement = document.createElement('gr-settings-view');
newElement.addEventListener('title-change', titleChangedStub);
// Attach it to the fixture.
var blank = fixture('blank');
const blank = fixture('blank');
blank.appendChild(newElement);
Polymer.dom.flush();
@@ -155,7 +155,7 @@ limitations under the License.
'Settings');
});
test('user preferences', function(done) {
test('user preferences', done => {
// Rendered with the expected preferences selected.
assert.equal(valueOf('Changes per page', 'preferences')
.firstElementChild.bindValue, preferences.changes_per_page);
@@ -178,10 +178,10 @@ limitations under the License.
assert.isFalse(element._menuChanged);
// Change the diff view element.
var diffSelect = valueOf('Diff view', 'preferences').firstElementChild;
const diffSelect = valueOf('Diff view', 'preferences').firstElementChild;
diffSelect.bindValue = 'SIDE_BY_SIDE';
var expandInlineDiffs =
const expandInlineDiffs =
valueOf('Expand inline diffs', 'preferences').firstElementChild;
diffSelect.fire('change');
@@ -191,24 +191,24 @@ limitations under the License.
assert.isFalse(element._menuChanged);
stub('gr-rest-api-interface', {
savePreferences: function(prefs) {
savePreferences(prefs) {
assert.equal(prefs.diff_view, 'SIDE_BY_SIDE');
assertMenusEqual(prefs.my, preferences.my);
assert.equal(prefs.expand_inline_diffs, true);
return Promise.resolve();
}
},
});
// Save the change.
element._handleSavePreferences().then(function() {
element._handleSavePreferences().then(() => {
assert.isFalse(element._prefsChanged);
assert.isFalse(element._menuChanged);
done();
});
});
test('publish comments on push', function(done) {
var publishCommentsOnPush =
test('publish comments on push', done => {
const publishCommentsOnPush =
valueOf('Publish comments on push', 'preferences').firstElementChild;
MockInteractions.tap(publishCommentsOnPush);
@@ -216,21 +216,21 @@ limitations under the License.
assert.isTrue(element._prefsChanged);
stub('gr-rest-api-interface', {
savePreferences: function(prefs) {
savePreferences(prefs) {
assert.equal(prefs.publish_comments_on_push, true);
return Promise.resolve();
}
},
});
// Save the change.
element._handleSavePreferences().then(function() {
element._handleSavePreferences().then(() => {
assert.isFalse(element._prefsChanged);
assert.isFalse(element._menuChanged);
done();
});
});
test('diff preferences', function(done) {
test('diff preferences', done => {
// Rendered with the expected preferences selected.
assert.equal(valueOf('Context', 'diffPreferences')
.firstElementChild.bindValue, diffPreferences.context);
@@ -249,7 +249,7 @@ limitations under the License.
assert.isFalse(element._diffPrefsChanged);
var showTabsCheckbox = valueOf('Show tabs', 'diffPreferences')
const showTabsCheckbox = valueOf('Show tabs', 'diffPreferences')
.firstElementChild;
showTabsCheckbox.checked = false;
element._handleShowTabsChanged();
@@ -257,20 +257,20 @@ limitations under the License.
assert.isTrue(element._diffPrefsChanged);
stub('gr-rest-api-interface', {
saveDiffPreferences: function(prefs) {
saveDiffPreferences(prefs) {
assert.equal(prefs.show_tabs, false);
return Promise.resolve();
}
},
});
// Save the change.
element._handleSaveDiffPreferences().then(function() {
element._handleSaveDiffPreferences().then(() => {
assert.isFalse(element._diffPrefsChanged);
done();
});
});
test('columns input is hidden with fit to scsreen is selected', function() {
test('columns input is hidden with fit to scsreen is selected', () => {
assert.isFalse(element.$.columnsPref.hidden);
MockInteractions.tap(element.$.lineWrapping);
@@ -280,14 +280,14 @@ limitations under the License.
assert.isFalse(element.$.columnsPref.hidden);
});
test('menu', function(done) {
test('menu', done => {
assert.isFalse(element._menuChanged);
assert.isFalse(element._prefsChanged);
assertMenusEqual(element._localMenu, preferences.my);
var menu = element.$.menu.firstElementChild;
var tableRows = Polymer.dom(menu.root).querySelectorAll('tbody tr');
const menu = element.$.menu.firstElementChild;
let tableRows = Polymer.dom(menu.root).querySelectorAll('tbody tr');
assert.equal(tableRows.length, preferences.my.length);
// Add a menu item:
@@ -301,13 +301,13 @@ limitations under the License.
assert.isFalse(element._prefsChanged);
stub('gr-rest-api-interface', {
savePreferences: function(prefs) {
savePreferences(prefs) {
assertMenusEqual(prefs.my, element._localMenu);
return Promise.resolve();
}
},
});
element._handleSaveMenu().then(function() {
element._handleSaveMenu().then(() => {
assert.isFalse(element._menuChanged);
assert.isFalse(element._prefsChanged);
assertMenusEqual(element.prefs.my, element._localMenu);
@@ -315,7 +315,7 @@ limitations under the License.
});
});
test('add email validation', function() {
test('add email validation', () => {
assert.isFalse(element._isNewEmailValid('invalid email'));
assert.isTrue(element._isNewEmailValid('vaguely@valid.email'));
@@ -327,8 +327,8 @@ limitations under the License.
element._computeAddEmailButtonEnabled('vaguely@valid.email', false));
});
test('add email does not save invalid', function() {
var addEmailStub = stubAddAccountEmail(201);
test('add email does not save invalid', () => {
const addEmailStub = stubAddAccountEmail(201);
assert.isFalse(element._addingEmail);
assert.isNotOk(element._lastSentVerificationEmail);
@@ -343,8 +343,8 @@ limitations under the License.
assert.isFalse(addEmailStub.called);
});
test('add email does save valid', function(done) {
var addEmailStub = stubAddAccountEmail(201);
test('add email does save valid', done => {
const addEmailStub = stubAddAccountEmail(201);
assert.isFalse(element._addingEmail);
assert.isNotOk(element._lastSentVerificationEmail);
@@ -356,14 +356,14 @@ limitations under the License.
assert.isTrue(addEmailStub.called);
assert.isTrue(addEmailStub.called);
addEmailStub.lastCall.returnValue.then(function() {
addEmailStub.lastCall.returnValue.then(() => {
assert.isOk(element._lastSentVerificationEmail);
done();
});
});
test('add email does not set last-email if error', function(done) {
var addEmailStub = stubAddAccountEmail(500);
test('add email does not set last-email if error', done => {
const addEmailStub = stubAddAccountEmail(500);
assert.isNotOk(element._lastSentVerificationEmail);
element._newEmail = 'valid@email.com';
@@ -371,58 +371,57 @@ limitations under the License.
element._handleAddEmailButton();
assert.isTrue(addEmailStub.called);
addEmailStub.lastCall.returnValue.then(function() {
addEmailStub.lastCall.returnValue.then(() => {
assert.isNotOk(element._lastSentVerificationEmail);
done();
});
});
test('emails are loaded without emailToken', function() {
test('emails are loaded without emailToken', () => {
sandbox.stub(element.$.emailEditor, 'loadData');
element.params = {};
element.attached();
assert.isTrue(element.$.emailEditor.loadData.calledOnce);
});
suite('when email verification token is provided', function() {
var resolveConfirm;
suite('when email verification token is provided', () => {
let resolveConfirm;
setup(function() {
setup(() => {
sandbox.stub(element.$.emailEditor, 'loadData');
sandbox.stub(element.$.restAPI, 'confirmEmail', function() {
return new Promise(function(resolve) { resolveConfirm = resolve; });
sandbox.stub(element.$.restAPI, 'confirmEmail', () => {
return new Promise(resolve => { resolveConfirm = resolve; });
});
element.params = {emailToken: 'foo'};
element.attached();
});
test('it is used to confirm email via rest API', function() {
test('it is used to confirm email via rest API', () => {
assert.isTrue(element.$.restAPI.confirmEmail.calledOnce);
assert.isTrue(element.$.restAPI.confirmEmail.calledWith('foo'));
});
test('emails are not loaded initially', function() {
test('emails are not loaded initially', () => {
assert.isFalse(element.$.emailEditor.loadData.called);
});
test('user emails are loaded after email confirmed', function(done) {
element._loadingPromise.then(function() {
test('user emails are loaded after email confirmed', done => {
element._loadingPromise.then(() => {
assert.isTrue(element.$.emailEditor.loadData.calledOnce);
done();
});
resolveConfirm();
});
test('show-alert is fired when email is confirmed', function(done) {
test('show-alert is fired when email is confirmed', done => {
sandbox.spy(element, 'fire');
element._loadingPromise.then(function() {
element._loadingPromise.then(() => {
assert.isTrue(
element.fire.calledWith('show-alert', {message: 'bar'}));
done();
});
resolveConfirm('bar');
});
});
});
</script>