Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Normalize arguments when using new. #38

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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
20 changes: 12 additions & 8 deletions 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,6 +179,7 @@ describe('Compute', function() {
});

beforeEach(function() {
extend(fakeUtil, originalFakeUtil);
compute = new Compute({
projectId: PROJECT_ID,
});
Expand All @@ -196,22 +198,24 @@ describe('Compute', function() {
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