Skip to content

Commit

Permalink
Normalize arguments when using new. (googleapis#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and alexander-fenster committed Feb 5, 2018
1 parent c18daf9 commit d3cbe30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,10 @@ const gapic = Object.freeze({
*/
function Bigtable(options) {
if (!(this instanceof Bigtable)) {
options = common.util.normalizeArguments(this, options);
return new Bigtable(options);
}

options = options || {};
options = common.util.normalizeArguments(this, options);

var baseUrl = 'bigtable.googleapis.com';
var adminBaseUrl = 'bigtableadmin.googleapis.com';
Expand Down
27 changes: 14 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var fakeUtil = extend({}, common.util, {
assert.deepEqual(options.exclude, ['instance', 'operation']);
},
});
var originalFakeUtil = extend(true, {}, fakeUtil);

var fakePaginator = {
extend: function() {
Expand Down Expand Up @@ -90,6 +91,7 @@ describe('Bigtable', function() {
});

beforeEach(function() {
extend(fakeUtil, originalFakeUtil);
delete process.env.BIGTABLE_EMULATOR_HOST;
bigtable = new Bigtable({projectId: PROJECT_ID});
});
Expand All @@ -110,25 +112,24 @@ describe('Bigtable', function() {
assert(promisified);
});

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

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

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

Bigtable.call(fakeContext, fakeOptions);
assert(normalizeArgumentsCalled);

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

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

0 comments on commit d3cbe30

Please sign in to comment.