Skip to content

Commit

Permalink
capabilities.js - used unused variable in merge() (#6935)
Browse files Browse the repository at this point in the history
- minor update
- the code worked before because Capabilities and
Map both happened to have a keys() function
- added tests in capabilities_test.js

Fixes #6911
  • Loading branch information
martin770 authored and jleyba committed Apr 15, 2019
1 parent 767b5c8 commit d179ab4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
30 changes: 14 additions & 16 deletions javascript/node/selenium-webdriver/lib/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,24 +330,22 @@ class Capabilities {
* @return {!Capabilities} A self reference.
*/
merge(other) {
if (!other) {
throw new TypeError('no capabilities provided for merge');
}

let map;
if (other instanceof Capabilities) {
map = other.map_;
} else if (other instanceof Map) {
map = other;
if (other) {
let otherMap;
if (other instanceof Capabilities) {
otherMap = other.map_;
} else if (other instanceof Map) {
otherMap = other;
} else {
otherMap = toMap(other);
}
otherMap.forEach((value, key) => {
this.set(key, value);
});
return this;
} else {
other = toMap(other);
}

for (let key of other.keys()) {
this.set(key, other.get(key));
throw new TypeError('no capabilities provided for merge');
}

return this;
}

/**
Expand Down
28 changes: 23 additions & 5 deletions javascript/node/selenium-webdriver/test/lib/capabilities_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ describe('Capabilities', function() {
});

it('can merge capabilities', function() {
let caps1 = new Capabilities()
.set('foo', 'bar')
.set('color', 'red');
const caps1 = new Capabilities()
.set('foo', 'bar')
.set('color', 'red');

let caps2 = new Capabilities()
.set('color', 'green');
const caps2 = new Capabilities()
.set('color', 'green');

assert.equal('bar', caps1.get('foo'));
assert.equal('red', caps1.get('color'));
Expand All @@ -60,6 +60,24 @@ describe('Capabilities', function() {
assert.equal('red', caps1.get('color'));
assert.equal('red', caps2.get('color'));
assert.equal('bar', caps2.get('foo'));

const caps3 = new Map()
.set('color', 'blue');

caps2.merge(caps3);
assert.equal('blue', caps2.get('color'));
assert.equal('bar', caps2.get('foo'));

const caps4 = {foo: 'baz'};

const caps5 = caps2.merge(caps4);

assert.equal('blue', caps2.get('color'));
assert.equal('baz', caps2.get('foo'));
assert.equal('blue', caps5.get('color'));
assert.equal('baz', caps5.get('foo'));
assert.equal(true, caps5 instanceof Capabilities);
assert.equal(caps2, caps5);
});

it('can be initialized from a hash object', function() {
Expand Down

0 comments on commit d179ab4

Please sign in to comment.