ES6ify /gr-ssh-editor/*

Bug: Issue 6179
Change-Id: I647dc9696c00d148053f82e8a57fad103c57b0cf
This commit is contained in:
Kasper Nilsson
2017-05-15 17:45:47 -07:00
parent 14e9f7daea
commit c15f556826
2 changed files with 49 additions and 50 deletions

View File

@@ -31,64 +31,63 @@
},
_keysToRemove: {
type: Array,
value: function() { return []; },
value() { return []; },
},
},
loadData: function() {
return this.$.restAPI.getAccountSSHKeys().then(function(keys) {
loadData() {
return this.$.restAPI.getAccountSSHKeys().then(keys => {
this._keys = keys;
}.bind(this));
});
},
save: function() {
var promises = this._keysToRemove.map(function(key) {
save() {
const promises = this._keysToRemove.map(key => {
this.$.restAPI.deleteAccountSSHKey(key.seq);
}.bind(this));
});
return Promise.all(promises).then(function() {
return Promise.all(promises).then(() => {
this._keysToRemove = [];
this.hasUnsavedChanges = false;
}.bind(this));
});
},
_getStatusLabel: function(isValid) {
_getStatusLabel(isValid) {
return isValid ? 'Valid' : 'Invalid';
},
_showKey: function(e) {
var index = parseInt(e.target.getAttribute('data-index'), 10);
_showKey(e) {
const index = parseInt(e.target.getAttribute('data-index'), 10);
this._keyToView = this._keys[index];
this.$.viewKeyOverlay.open();
},
_closeOverlay: function() {
_closeOverlay() {
this.$.viewKeyOverlay.close();
},
_handleDeleteKey: function(e) {
var index = parseInt(e.target.getAttribute('data-index'), 10);
_handleDeleteKey(e) {
const index = parseInt(e.target.getAttribute('data-index'), 10);
this.push('_keysToRemove', this._keys[index]);
this.splice('_keys', index, 1);
this.hasUnsavedChanges = true;
},
_handleAddKey: function() {
_handleAddKey() {
this.$.addButton.disabled = true;
this.$.newKey.disabled = true;
return this.$.restAPI.addAccountSSHKey(this._newKey.trim())
.then(function(key) {
.then(key => {
this.$.newKey.disabled = false;
this._newKey = '';
this.push('_keys', key);
}.bind(this))
.catch(function() {
}).catch(() => {
this.$.addButton.disabled = false;
this.$.newKey.disabled = false;
}.bind(this));
});
},
_computeAddButtonDisabled: function(newKey) {
_computeAddButtonDisabled(newKey) {
return !newKey.length;
},
});

View File

@@ -33,11 +33,11 @@ limitations under the License.
</test-fixture>
<script>
suite('gr-ssh-editor tests', function() {
var element;
var keys;
suite('gr-ssh-editor tests', () => {
let element;
let keys;
setup(function(done) {
setup(done => {
keys = [{
seq: 1,
ssh_public_key: 'ssh-rsa <key 1> comment-one@machine-one',
@@ -55,37 +55,37 @@ limitations under the License.
}];
stub('gr-rest-api-interface', {
getAccountSSHKeys: function() { return Promise.resolve(keys); },
getAccountSSHKeys() { return Promise.resolve(keys); },
});
element = fixture('basic');
element.loadData().then(function() { flush(done); });
element.loadData().then(() => { flush(done); });
});
test('renders', function() {
var rows = Polymer.dom(element.root).querySelectorAll('tbody tr');
test('renders', () => {
const rows = Polymer.dom(element.root).querySelectorAll('tbody tr');
assert.equal(rows.length, 2);
var cells = rows[0].querySelectorAll('td');
let cells = rows[0].querySelectorAll('td');
assert.equal(cells[0].textContent, keys[0].comment);
cells = rows[1].querySelectorAll('td');
assert.equal(cells[0].textContent, keys[1].comment);
});
test('remove key', function(done) {
var lastKey = keys[1];
test('remove key', done => {
const lastKey = keys[1];
var saveStub = sinon.stub(element.$.restAPI, 'deleteAccountSSHKey',
function() { return Promise.resolve(); });
const saveStub = sinon.stub(element.$.restAPI, 'deleteAccountSSHKey',
() => { return Promise.resolve(); });
assert.equal(element._keysToRemove.length, 0);
assert.isFalse(element.hasUnsavedChanges);
// Get the delete button for the last row.
var button = Polymer.dom(element.root).querySelector(
const button = Polymer.dom(element.root).querySelector(
'tbody tr:last-of-type td:nth-child(4) gr-button');
MockInteractions.tap(button);
@@ -96,7 +96,7 @@ limitations under the License.
assert.isTrue(element.hasUnsavedChanges);
assert.isFalse(saveStub.called);
element.save().then(function() {
element.save().then(() => {
assert.isTrue(saveStub.called);
assert.equal(saveStub.lastCall.args[0], lastKey.seq);
assert.equal(element._keysToRemove.length, 0);
@@ -105,11 +105,11 @@ limitations under the License.
});
});
test('show key', function() {
var openSpy = sinon.spy(element.$.viewKeyOverlay, 'open');
test('show key', () => {
const openSpy = sinon.spy(element.$.viewKeyOverlay, 'open');
// Get the show button for the last row.
var button = Polymer.dom(element.root).querySelector(
const button = Polymer.dom(element.root).querySelector(
'tbody tr:last-of-type td:nth-child(3) gr-button');
MockInteractions.tap(button);
@@ -118,9 +118,9 @@ limitations under the License.
assert.isTrue(openSpy.called);
});
test('add key', function(done) {
var newKeyString = 'ssh-rsa <key 3> comment-three@machine-three';
var newKeyObject = {
test('add key', done => {
const newKeyString = 'ssh-rsa <key 3> comment-three@machine-three';
const newKeyObject = {
seq: 3,
ssh_public_key: newKeyString,
encoded_key: '<key 3>',
@@ -129,15 +129,15 @@ limitations under the License.
valid: true,
};
var addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
function() { return Promise.resolve(newKeyObject); });
const addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
() => { return Promise.resolve(newKeyObject); });
element._newKey = newKeyString;
assert.isFalse(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
element._handleAddKey().then(function() {
element._handleAddKey().then(() => {
assert.isTrue(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
assert.equal(element._keys.length, 3);
@@ -151,18 +151,18 @@ limitations under the License.
assert.equal(addStub.lastCall.args[0], newKeyString);
});
test('add invalid key', function(done) {
var newKeyString = 'not even close to valid';
test('add invalid key', done => {
const newKeyString = 'not even close to valid';
var addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
function() { return Promise.reject(); });
const addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
() => { return Promise.reject(); });
element._newKey = newKeyString;
assert.isFalse(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
element._handleAddKey().then(function() {
element._handleAddKey().then(() => {
assert.isFalse(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
assert.equal(element._keys.length, 2);