Skip to content

Commit

Permalink
Normalize arguments when using new. (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jan 24, 2018
1 parent 6ee797b commit a11a9c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ const gapic = Object.freeze({
*/
function Spanner(options) {
if (!(this instanceof Spanner)) {
options = common.util.normalizeArguments(this, options);
return new Spanner(options);
}

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

this.clients_ = new Map();
this.instances_ = new Map();

Expand Down
25 changes: 14 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var fakeUtil = extend({}, util, {
]);
},
});
var originalFakeUtil = extend(true, {}, fakeUtil);

var fakeGapicClient = util.noop;
fakeGapicClient.scopes = [];
Expand Down Expand Up @@ -105,6 +106,7 @@ describe('Spanner', function() {
});

beforeEach(function() {
extend(fakeUtil, originalFakeUtil);
fakeGapicClient = util.noop;
fakeGapicClient.scopes = [];
fakeV1.DatabaseAdminClient = fakeGapicClient;
Expand Down Expand Up @@ -135,23 +137,24 @@ describe('Spanner', function() {
assert.strictEqual(spanner.getInstancesStream, 'getInstances');
});

it('should work without new', function() {
assert.doesNotThrow(function() {
Spanner(OPTIONS);
});
});

it('should normalize the arguments', function() {
var normalizeArguments = fakeUtil.normalizeArguments;
var normalizeArgumentsCalled = false;
var fakeOptions = {projectId: OPTIONS.projectId};
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_;
};

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

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

it('should create an auth instance from google-auto-auth', function() {
Expand Down

0 comments on commit a11a9c5

Please sign in to comment.