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

Normalize arguments when using new. #8

Merged
merged 1 commit into from
Feb 2, 2018
Merged
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
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ var Project = require('./project.js');
*/
function Resource(options) {
if (!(this instanceof Resource)) {
options = common.util.normalizeArguments(this, options, {
projectIdRequired: false,
});
return new Resource(options);
}

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

var config = {
baseUrl: 'https://cloudresourcemanager.googleapis.com/v1',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
Expand Down
26 changes: 15 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var fakeUtil = extend({}, util, {
assert.deepEqual(options.exclude, ['operation', 'project']);
},
});
var originalFakeUtil = extend(true, {}, fakeUtil);

describe('Resource', function() {
var PROJECT_ID = 'test-project-id';
Expand All @@ -90,6 +91,7 @@ describe('Resource', function() {
});

beforeEach(function() {
extend(fakeUtil, originalFakeUtil);
makeAuthenticatedRequestFactoryOverride = null;

resource = new Resource({
Expand All @@ -110,23 +112,25 @@ describe('Resource', function() {
assert(promisified);
});

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

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

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

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