ember-data-factory-guy 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -1
- data/.travis.yml +7 -0
- data/Gruntfile.js +18 -3
- data/README.md +122 -16
- data/bower.json +6 -2
- data/dist/ember-data-factory-guy.js +264 -107
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/dist/factory_guy_has_many.js +188 -0
- data/ember-data-factory-guy.gemspec +1 -1
- data/lib/ember-data-factory-guy.rb +0 -1
- data/package.json +4 -4
- data/src/factory_guy.js +114 -89
- data/src/{factory_guy_helper_mixin.js → factory_guy_test_mixin.js} +2 -1
- data/src/model_definition.js +98 -0
- data/src/sequence.js +16 -0
- data/src/store.js +40 -23
- data/tests/active_model_adapter_factory_test.js +45 -5
- data/tests/factory_guy_test.js +93 -0
- data/tests/fixture_adapter_factory_test.js +14 -40
- data/tests/index.html +1 -0
- data/tests/rest_adapter_factory_test.js +18 -4
- data/tests/support/factories/project_factory.js +3 -1
- data/tests/support/factories/user_factory.js +1 -1
- data/tests/support/libs/sinon.js +4287 -0
- data/tests/support/models/user.js +1 -1
- data/tests/support/test_helper.js +2 -2
- data/tests/test_setup.js +4291 -2
- data/vendor/assets/javascripts/ember_data_factory_guy.js +155 -113
- metadata +8 -3
@@ -1,137 +1,162 @@
|
|
1
1
|
FactoryGuy = Ember.Object.reopenClass({
|
2
|
-
|
3
|
-
fixtureLookup: {},
|
4
|
-
modelIds: {},
|
2
|
+
modelDefinitions: {},
|
5
3
|
|
6
4
|
/**
|
7
5
|
```javascript
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
7
|
+
Person = DS.Model.extend({
|
8
|
+
type: DS.attr('string'),
|
9
|
+
name: DS.attr('string')
|
10
|
+
})
|
11
|
+
|
12
|
+
FactoryGuy.define('person', {
|
13
|
+
sequences: {
|
14
|
+
personName: function(num) {
|
15
|
+
return 'person #' + num;
|
16
|
+
},
|
17
|
+
personType: function(num) {
|
18
|
+
return 'person type #' + num;
|
19
|
+
}
|
20
|
+
},
|
21
|
+
default: {
|
22
|
+
type: 'normal',
|
23
|
+
name: FactoryGuy.generate('personName')
|
24
|
+
},
|
25
|
+
dude: {
|
26
|
+
type: FactoryGuy.generate('personType')
|
27
|
+
},
|
28
|
+
});
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
For the Person model, you can define fixtures like 'dude' or just use 'person'
|
33
|
+
and get default values.
|
34
|
+
|
35
|
+
And to get those fixtures you would call them this way:
|
36
|
+
|
37
|
+
FactoryGuy.build('person') or FactoryGuy.build('dude')
|
38
|
+
|
39
|
+
@param model the model to define
|
40
|
+
@param config your model definition object
|
34
41
|
*/
|
35
42
|
define: function (model, config) {
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
if (key != 'default') {
|
41
|
-
this.fixtureLookup[key] = model;
|
42
|
-
}
|
43
|
+
if (this.modelDefinitions[model]) {
|
44
|
+
this.modelDefinitions[model].merge(config);
|
45
|
+
} else {
|
46
|
+
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
43
47
|
}
|
44
|
-
// setup id
|
45
|
-
this.modelIds[model] = 0;
|
46
48
|
},
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
*/
|
52
|
-
getModelInfo: function (model) {
|
53
|
-
if (!this.fixtureStore[model]) {
|
54
|
-
this.fixtureStore[model] = {};
|
50
|
+
generate: function (sequenceName) {
|
51
|
+
return function () {
|
52
|
+
return this.generate(sequenceName);
|
55
53
|
}
|
56
|
-
return this.fixtureStore[model];
|
57
54
|
},
|
58
55
|
|
56
|
+
|
59
57
|
/**
|
60
58
|
|
61
|
-
|
62
|
-
|
59
|
+
@param name fixture name could be model name like 'user'
|
60
|
+
or specific user like 'admin'
|
61
|
+
@returns model associated with fixture name
|
63
62
|
*/
|
64
63
|
lookupModelForName: function (name) {
|
65
|
-
|
66
|
-
|
67
|
-
if (
|
68
|
-
model
|
64
|
+
for (model in this.modelDefinitions) {
|
65
|
+
var definition = this.modelDefinitions[model];
|
66
|
+
if (definition.matchesName(name)) {
|
67
|
+
return definition.model;
|
69
68
|
}
|
70
69
|
}
|
71
|
-
return model;
|
72
70
|
},
|
73
71
|
|
74
72
|
/**
|
75
|
-
|
73
|
+
|
74
|
+
@param name fixture name could be model name like 'user'
|
75
|
+
or specific user like 'admin'
|
76
|
+
@returns definition associated with model
|
76
77
|
*/
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
lookupDefinitionForName: function (name) {
|
79
|
+
for (model in this.modelDefinitions) {
|
80
|
+
var definition = this.modelDefinitions[model];
|
81
|
+
if (definition.matchesName(name)) {
|
82
|
+
return definition;
|
83
|
+
}
|
84
|
+
}
|
81
85
|
},
|
82
86
|
|
87
|
+
|
83
88
|
/**
|
84
|
-
|
89
|
+
Build fixtures for model or specific fixture name. For example:
|
85
90
|
|
86
|
-
|
87
|
-
|
91
|
+
FactoryGuy.build('user') for User model
|
92
|
+
FactoryGuy.build('bob') for User model with bob attributes
|
88
93
|
|
89
|
-
|
90
|
-
|
91
|
-
|
94
|
+
@param name fixture name
|
95
|
+
@param opts options that will override default fixture values
|
96
|
+
@returns {*}
|
92
97
|
*/
|
93
98
|
build: function (name, opts) {
|
94
|
-
var
|
95
|
-
if (!
|
96
|
-
throw new Error("
|
99
|
+
var definition = this.lookupDefinitionForName(name);
|
100
|
+
if (!definition) {
|
101
|
+
throw new Error("Can't find that factory named [" + name + "]");
|
97
102
|
}
|
103
|
+
return definition.build(name, opts);
|
104
|
+
},
|
105
|
+
|
106
|
+
/**
|
107
|
+
Build list of fixtures for model or specific fixture name. For example:
|
108
|
+
|
109
|
+
FactoryGuy.buildList('user', 2) for 2 User models
|
110
|
+
FactoryGuy.build('bob', 2) for 2 User model with bob attributes
|
98
111
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
112
|
+
@param name fixture name
|
113
|
+
@param number number of fixtures to create
|
114
|
+
@param opts options that will override default fixture values
|
115
|
+
@returns list of fixtures
|
116
|
+
*/
|
117
|
+
buildList: function (name, number, opts) {
|
118
|
+
var definition = this.lookupDefinitionForName(name);
|
119
|
+
if (!definition) {
|
120
|
+
throw new Error("Can't find that factory named [" + name + "]");
|
105
121
|
}
|
106
|
-
return
|
122
|
+
return definition.buildList(name, number, opts);
|
107
123
|
},
|
108
124
|
|
109
125
|
/**
|
110
|
-
|
111
|
-
|
126
|
+
Clear model instances from FIXTURES array, and from store cache.
|
127
|
+
Reset the id sequence for the models back to zero.
|
112
128
|
*/
|
113
129
|
resetModels: function (store) {
|
114
130
|
var typeMaps = store.typeMaps;
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
131
|
+
for (model in this.modelDefinitions) {
|
132
|
+
var definition = this.modelDefinitions[model];
|
133
|
+
definition.reset();
|
134
|
+
try {
|
135
|
+
var modelType = store.modelFor(definition.model);
|
136
|
+
if (store.usingFixtureAdapter()) {
|
137
|
+
modelType.FIXTURES = [];
|
138
|
+
}
|
119
139
|
store.unloadAll(modelType);
|
140
|
+
} catch (e) {
|
120
141
|
}
|
121
|
-
} else {
|
122
|
-
for (model in typeMaps) {
|
123
|
-
store.unloadAll(typeMaps[model].type);
|
124
|
-
}
|
142
|
+
// } else {
|
143
|
+
// for (model in typeMaps) {
|
144
|
+
// store.unloadAll(typeMaps[model].type);
|
145
|
+
// }
|
146
|
+
// }
|
147
|
+
|
148
|
+
// for (model in this.modelDefinitions) {
|
149
|
+
// this.modelDefinitions[model].reset();
|
125
150
|
}
|
126
|
-
this.modelIds = {}
|
127
151
|
},
|
128
152
|
|
129
153
|
/**
|
130
|
-
|
131
|
-
|
154
|
+
Push fixture to model's FIXTURES array.
|
155
|
+
Used when store's adapter is a DS.FixtureAdapter.
|
132
156
|
|
133
|
-
|
134
|
-
|
157
|
+
@param modelClass DS.Model type
|
158
|
+
@param fixture the fixture to add
|
159
|
+
@returns json fixture data
|
135
160
|
*/
|
136
161
|
pushFixture: function (modelClass, fixture) {
|
137
162
|
if (!modelClass['FIXTURES']) {
|
@@ -141,7 +166,6 @@ FactoryGuy = Ember.Object.reopenClass({
|
|
141
166
|
return fixture;
|
142
167
|
}
|
143
168
|
})
|
144
|
-
|
145
169
|
DS.Store.reopen({
|
146
170
|
|
147
171
|
usingFixtureAdapter: function() {
|
@@ -153,9 +177,9 @@ DS.Store.reopen({
|
|
153
177
|
Make new fixture and save to store. If the store is using FixtureAdapter,
|
154
178
|
will push to FIXTURE array, otherwise will use push method on adapter.
|
155
179
|
|
156
|
-
@param name
|
157
|
-
@param options
|
158
|
-
@returns
|
180
|
+
@param name name of fixture
|
181
|
+
@param options fixture options
|
182
|
+
@returns json or record
|
159
183
|
*/
|
160
184
|
makeFixture: function (name, options) {
|
161
185
|
var modelName = FactoryGuy.lookupModelForName(name);
|
@@ -163,31 +187,48 @@ DS.Store.reopen({
|
|
163
187
|
var modelType = this.modelFor(modelName);
|
164
188
|
|
165
189
|
if (this.usingFixtureAdapter()) {
|
166
|
-
this.
|
190
|
+
this.setBelongsToFixturesAdapter(modelType, modelName, fixture);
|
167
191
|
return FactoryGuy.pushFixture(modelType, fixture);
|
168
192
|
} else {
|
169
193
|
var self = this;
|
170
194
|
var model;
|
171
195
|
Em.run( function() {
|
172
196
|
model = self.push(modelName, fixture);
|
173
|
-
self.
|
197
|
+
self.setBelongsToRESTAdapter(modelType, modelName, model);
|
174
198
|
});
|
175
199
|
return model;
|
176
200
|
}
|
177
201
|
},
|
178
202
|
|
179
203
|
/**
|
180
|
-
|
204
|
+
Make a list of Fixtures
|
205
|
+
|
206
|
+
@param name name of fixture
|
207
|
+
@param number number of fixtures
|
208
|
+
@param options fixture options
|
209
|
+
@returns list of json fixtures or records depending on the adapter type
|
210
|
+
*/
|
211
|
+
makeList: function (name, number, options) {
|
212
|
+
var arr = [];
|
213
|
+
for (var i = 0; i < number; i++) {
|
214
|
+
arr.push(this.makeFixture(name, options))
|
215
|
+
}
|
216
|
+
return arr;
|
217
|
+
},
|
218
|
+
|
219
|
+
/**
|
220
|
+
Set the belongsTo association for FixtureAdapter,
|
181
221
|
with models that have a hasMany association.
|
182
222
|
|
183
|
-
For example if a
|
184
|
-
on each project that the
|
185
|
-
now has the belongsTo
|
223
|
+
For example if a user hasMany projects, then set the user.id
|
224
|
+
on each project that the user hasMany of, so that the project
|
225
|
+
now has the belongsTo user association setup.
|
186
226
|
|
187
|
-
@param
|
188
|
-
@param model
|
227
|
+
@param modelType model type like App.User
|
228
|
+
@param modelName model name like 'user'
|
229
|
+
@param parentFixture parent to assign as belongTo
|
189
230
|
*/
|
190
|
-
|
231
|
+
setBelongsToFixturesAdapter: function (modelType, modelName, parentFixture) {
|
191
232
|
var store = this;
|
192
233
|
var adapter = this.adapterFor('application');
|
193
234
|
var relationShips = Ember.get(modelType, 'relationshipNames');
|
@@ -206,18 +247,18 @@ DS.Store.reopen({
|
|
206
247
|
},
|
207
248
|
|
208
249
|
/**
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
250
|
+
Set the belongsTo association for the REST type models
|
251
|
+
with a hasMany association
|
252
|
+
|
253
|
+
For example if a user hasMany projects, then set the user
|
254
|
+
on each project that the user hasMany of, so that the project
|
255
|
+
now has the belongsTo user association setup
|
256
|
+
|
257
|
+
@param modelType model type like 'App.User'
|
258
|
+
@param modelName model name like 'user'
|
259
|
+
@param parent model to check for hasMany
|
219
260
|
*/
|
220
|
-
|
261
|
+
setBelongsToRESTAdapter: function (modelType, modelName, parent) {
|
221
262
|
var relationShips = Ember.get(modelType, 'relationshipNames');
|
222
263
|
|
223
264
|
if (relationShips.hasMany) {
|
@@ -285,8 +326,9 @@ DS.FixtureAdapter.reopen({
|
|
285
326
|
})
|
286
327
|
|
287
328
|
|
288
|
-
|
329
|
+
FactoryGuyTestMixin = Em.Mixin.create({
|
289
330
|
|
331
|
+
// Pass in the app root, which typically is App.
|
290
332
|
setup: function(app) {
|
291
333
|
this.set('container', app.__container__);
|
292
334
|
return this;
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ember-data-factory-guy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-05-06 00:00:00.000000000 +03:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
description: Easily create Fixtures for Ember Data
|
@@ -22,17 +22,21 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
files:
|
24
24
|
- .gitignore
|
25
|
+
- .travis.yml
|
25
26
|
- Gruntfile.js
|
26
27
|
- README.md
|
27
28
|
- bower.json
|
28
29
|
- dist/ember-data-factory-guy.js
|
29
30
|
- dist/ember-data-factory-guy.min.js
|
31
|
+
- dist/factory_guy_has_many.js
|
30
32
|
- ember-data-factory-guy.gemspec
|
31
33
|
- lib/ember-data-factory-guy.rb
|
32
34
|
- package.json
|
33
35
|
- src/factory_guy.js
|
34
|
-
- src/
|
36
|
+
- src/factory_guy_test_mixin.js
|
35
37
|
- src/has_many.js
|
38
|
+
- src/model_definition.js
|
39
|
+
- src/sequence.js
|
36
40
|
- src/store.js
|
37
41
|
- tests/active_model_adapter_factory_test.js
|
38
42
|
- tests/factory_guy_test.js
|
@@ -42,6 +46,7 @@ files:
|
|
42
46
|
- tests/store_test.js
|
43
47
|
- tests/support/factories/project_factory.js
|
44
48
|
- tests/support/factories/user_factory.js
|
49
|
+
- tests/support/libs/sinon.js
|
45
50
|
- tests/support/models/project.js
|
46
51
|
- tests/support/models/user.js
|
47
52
|
- tests/support/test_helper.js
|