Skip to content

Commit

Permalink
Normalize arguments when using new. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jan 24, 2018
1 parent 75d5ff3 commit 01e224d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,12 @@ const gapic = Object.freeze({
*/
function Datastore(options) {
if (!(this instanceof Datastore)) {
options = common.util.normalizeArguments(this, options, {
projectIdRequired: false,
});
return new Datastore(options);
}

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

this.clients_ = new Map();
this.datastore = this;
Expand Down
21 changes: 13 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var fakeEntity = {
};

var fakeUtil = extend({}, util);
var originalFakeUtil = extend(true, {}, fakeUtil);

var googleAutoAuthOverride;
function fakeGoogleAutoAuth() {
Expand Down Expand Up @@ -116,6 +117,8 @@ describe('Datastore', function() {
});

beforeEach(function() {
extend(fakeUtil, originalFakeUtil);

createInsecureOverride = null;
googleAutoAuthOverride = null;

Expand Down Expand Up @@ -143,23 +146,25 @@ describe('Datastore', function() {
});

describe('instantiation', function() {
it('should work without new', function() {
assert.doesNotThrow(function() {
Datastore({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_, config) {
normalizeArgumentsCalled = true;
assert.strictEqual(context, fakeContext);
assert.strictEqual(OPTIONS, options_);
assert.strictEqual(options_, options);
assert.strictEqual(config.projectIdRequired, false);
return options_;
};

Datastore.call(fakeContext, OPTIONS);
assert(normalizeArgumentsCalled);

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

it('should initialize an empty Client map', function() {
Expand Down

0 comments on commit 01e224d

Please sign in to comment.