Skip to content

Commit 29fbb1a

Browse files
committed
givenUser: Add possibility to pass role as object
Add tests for givenUser etc... From strongloop-archive/loopback-testing#69
1 parent debeec2 commit 29fbb1a

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

lib/helpers.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,18 @@ _beforeEach.withArgs = function() {
7777
}
7878

7979
_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
80-
var modelKey = modelName;
8180
var modelInstance;
8281

8382
if(typeof attrs === 'function') {
8483
optionalHandler = attrs;
8584
attrs = undefined;
8685
}
8786

88-
if(typeof optionalHandler === 'string') {
89-
modelKey = optionalHandler;
90-
}
91-
9287
attrs = attrs || {};
9388

9489
beforeEach(function(done) {
9590
if(modelName === '__USERMODEL__') {
96-
modelName = this.userModel ? this.userModel : 'user';
91+
modelName = this.userModel ? this.userModel : 'User';
9792
}
9893

9994
var test = this;
@@ -113,8 +108,15 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
113108
if(err.details) console.error(err.details);
114109
done(err);
115110
} else {
111+
var modelKey = modelName;
112+
if(typeof optionalHandler === 'string') {
113+
modelKey = optionalHandler;
114+
}
115+
116+
test['__USERMODEL__'] = result;
116117
test[modelKey] = result;
117118
modelInstance = result;
119+
118120
done();
119121
}
120122
});
@@ -150,7 +152,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
150152
}
151153
_beforeEach.givenUser(attrs, function (done) {
152154
var test = this;
153-
test.app.models.Role.findOrCreate({name: role.name }, function (err, result) {
155+
test.app.models.Role.findOrCreate(role, function (err, result) {
154156
if(err) {
155157
console.error(err.message);
156158
if(err.details) console.error(err.details);

test/test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ describe('helpers', function () {
66
var testApp = loopback();
77
var db = testApp.dataSource('db', {connector: loopback.Memory});
88
var testModel = testApp.model('xxx-test-model', {dataSource: 'db'});
9+
testApp.model(loopback.User, {dataSource: 'db'});
10+
testApp.model(loopback.Role, {dataSource: 'db'});
11+
testApp.model(loopback.RoleMapping, {dataSource: 'db'});
912

1013
testApp.use(loopback.rest());
1114
helpers.beforeEach.withApp(testApp);
@@ -109,4 +112,50 @@ describe('helpers', function () {
109112
});
110113
});
111114
});
115+
116+
describe('givenUserWithRole', function() {
117+
describe('role as string', function () {
118+
helpers.beforeEach.givenUserWithRole(
119+
{id: 1, email: "[email protected]", password: "abc"},
120+
"testRole");
121+
it("should create a user instance with default userModel with the given role", function() {
122+
assert.equal(this['User'].id, 1);
123+
assert.equal(this.userRole.id, 1);
124+
assert.equal(this.userRole.name, "testRole");
125+
assert(this.userRoleMapping);
126+
});
127+
})
128+
describe('role as object', function () {
129+
helpers.beforeEach.givenUserWithRole(
130+
{id: 1, email: "[email protected]", password: "abc"},
131+
{id: 2, name: "testRole"});
132+
it("should create a user instance with default userModel with the given role", function() {
133+
assert.equal(this['User'].id, 1);
134+
assert.equal(this.userRole.id, 2);
135+
assert.equal(this.userRole.name, "testRole");
136+
assert(this.userRoleMapping);
137+
});
138+
})
139+
});
140+
describe('withUserModel', function() {
141+
helpers.beforeEach.withUserModel('xxx-test-model');
142+
it("should set the user model name", function() {
143+
assert.equal(this.userModel, 'xxx-test-model');
144+
});
145+
describe('givenUser', function() {
146+
helpers.beforeEach.givenUser();
147+
it("should create a new instance of specified User model", function() {
148+
assert(this[this.userModel]);
149+
});
150+
});
151+
describe('givenUserWithRole', function() {
152+
helpers.beforeEach.givenUserWithRole({id: 1}, {id: 2, name: "testRole"});
153+
it("should create a user instance (of specified User model) with the given role", function() {
154+
assert.equal(this[this.userModel].id, 1);
155+
assert.equal(this.userRole.id, 2);
156+
assert.equal(this.userRole.name, "testRole");
157+
assert(this.userRoleMapping);
158+
});
159+
});
160+
});
112161
});

0 commit comments

Comments
 (0)