Skip to content

Commit

Permalink
Normalize arguments when using new. (#40)
Browse files Browse the repository at this point in the history
* Normalize arguments when using new.

* lint
  • Loading branch information
stephenplusplus authored and alexander-fenster committed Feb 6, 2018
1 parent f547a1f commit e558697
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/google-cloud-compute/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ var Zone = require('./zone.js');
*/
function Compute(options) {
if (!(this instanceof Compute)) {
options = common.util.normalizeArguments(this, options);
return new Compute(options);
}

options = common.util.normalizeArguments(this, options);

var config = {
baseUrl: 'https://www.googleapis.com/compute/v1',
scopes: ['https://www.googleapis.com/auth/compute'],
Expand Down
27 changes: 12 additions & 15 deletions packages/google-cloud-compute/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var fakeUtil = extend({}, util, {
]);
},
});
var originalFakeUtil = extend(true, {}, fakeUtil);

var fakePaginator = {
extend: function(Class, methods) {
Expand Down Expand Up @@ -178,40 +179,36 @@ describe('Compute', function() {
});

beforeEach(function() {
extend(fakeUtil, originalFakeUtil);
compute = new Compute({
projectId: PROJECT_ID,
});
});

describe('instantiation', function() {
var options = {
projectId: PROJECT_ID,
credentials: 'credentials',
email: 'email',
keyFilename: 'keyFile',
};

it('should return a new instance of Compute', function() {
var compute = new Compute({projectId: PROJECT_ID});
assert(compute instanceof Compute);
});

it('should work without new', function() {
assert.doesNotThrow(function() {
Compute({projectId: PROJECT_ID});
});
});

it('should normalize the arguments', function() {
var normalizeArguments = fakeUtil.normalizeArguments;
var normalizeArgumentsCalled = false;
var fakeContext = {};
var options = {};

fakeUtil.normalizeArguments = function(context, options_) {
normalizeArgumentsCalled = true;
assert.strictEqual(context, fakeContext);
assert.strictEqual(options, options_);
assert.strictEqual(options_, options);
return options_;
};

Compute.call(fakeContext, options);
assert(normalizeArgumentsCalled);

fakeUtil.normalizeArguments = normalizeArguments;
new Compute(options);
assert.strictEqual(normalizeArgumentsCalled, true);
});

it('should inherit from Service', function() {
Expand Down

0 comments on commit e558697

Please sign in to comment.