Skip to content

Commit 6f43689

Browse files
committed
Merge pull request iGLOO-be#2 from LoicMahieu/fix/1
Save created models in closure
2 parents c9768f2 + 29fbb1a commit 6f43689

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

lib/helpers.js

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

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

8282
if(typeof attrs === 'function') {
8383
optionalHandler = attrs;
8484
attrs = undefined;
8585
}
8686

87-
if(typeof optionalHandler === 'string') {
88-
modelKey = optionalHandler;
89-
}
90-
9187
attrs = attrs || {};
9288

9389
beforeEach(function(done) {
9490
if(modelName === '__USERMODEL__') {
95-
modelName = this.userModel ? this.userModel : 'user';
91+
modelName = this.userModel ? this.userModel : 'User';
9692
}
9793

9894
var test = this;
@@ -112,7 +108,15 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
112108
if(err.details) console.error(err.details);
113109
done(err);
114110
} else {
111+
var modelKey = modelName;
112+
if(typeof optionalHandler === 'string') {
113+
modelKey = optionalHandler;
114+
}
115+
116+
test['__USERMODEL__'] = result;
115117
test[modelKey] = result;
118+
modelInstance = result;
119+
116120
done();
117121
}
118122
});
@@ -123,7 +127,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
123127
}
124128

125129
afterEach(function(done) {
126-
this[modelKey].destroy(done);
130+
modelInstance.destroy(done);
127131
});
128132
}
129133

@@ -139,21 +143,25 @@ _beforeEach.givenUser = function(attrs, optionalHandler) {
139143
}
140144

141145
_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
146+
var roleInstance, roleMappingInstance;
147+
142148
if (typeof role === 'string') {
143149
role = {
144150
name: role
145151
}
146152
}
147153
_beforeEach.givenUser(attrs, function (done) {
148154
var test = this;
149-
test.app.models.Role.findOrCreate({name: role.name }, function (err, result) {
155+
test.app.models.Role.findOrCreate(role, function (err, result) {
150156
if(err) {
151157
console.error(err.message);
152158
if(err.details) console.error(err.details);
153159
return done(err);
154160
}
155161

156162
test.userRole = result;
163+
roleInstance = result;
164+
157165
test.app.models.RoleMapping.create(
158166
{principalId: test.__USERMODEL__.id,
159167
principalType: test.app.models.RoleMapping.USER,
@@ -166,6 +174,8 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
166174
}
167175

168176
test.userRoleMapping = result;
177+
roleMappingInstance = result;
178+
169179
done();
170180
}
171181
);
@@ -177,14 +187,13 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
177187
}
178188

179189
afterEach(function(done) {
180-
var test = this;
181-
this.userRole.destroy(function(err) {
190+
roleInstance.destroy(function(err) {
182191
if(err) return done(err);
183-
test.userRole = undefined;
192+
roleInstance = undefined;
184193

185-
test.userRoleMapping.destroy(function(err) {
194+
roleMappingInstance.destroy(function(err) {
186195
if(err) return done(err);
187-
test.userRoleMapping = undefined;
196+
roleMappingInstance = undefined;
188197
done();
189198
});
190199
});

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)