ember-data-factory-guy 0.9.2 → 0.9.3
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.
- checksums.yaml +4 -4
- data/README.md +38 -8
- data/bower.json +1 -1
- data/dist/amd/factory-guy.js +94 -20
- data/dist/ember-data-factory-guy.js +95 -21
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/ember-data-factory-guy.gemspec +5 -1
- data/package.json +1 -1
- data/src/factory_guy.js +3 -1
- data/src/factory_guy_test_mixin.js +39 -13
- data/src/store.js +53 -7
- data/tests/factory_guy_test.js +1 -1
- data/tests/factory_guy_test_mixin_test.js +44 -6
- data/tests/fixture_adapter_factory_test.js +112 -60
- data/tests/index.html +1 -1
- data/tests/store_test.js +198 -0
- data/tests/support/factories/company_factory.js +4 -1
- data/tests/support/factories/property_factory.js +6 -1
- data/tests/support/factories/user_factory.js +3 -0
- data/tests/support/models/person.js +3 -1
- data/tests/support/models/user.js +1 -1
- data/tests/support/test_helper.js +3 -3
- data/tests/test_setup.js +17 -2
- data/vendor/assets/javascripts/ember_data_factory_guy.js +98 -23
- metadata +2 -2
data/tests/index.html
CHANGED
@@ -31,7 +31,7 @@
|
|
31
31
|
<div id="qunit"></div> <!-- QUnit fills this with results, etc -->
|
32
32
|
<script src='active_model_adapter_factory_test.js'></script>
|
33
33
|
<script src='rest_adapter_factory_test.js'></script>
|
34
|
-
|
34
|
+
<script src='fixture_adapter_factory_test.js'></script>
|
35
35
|
<script src='store_test.js'></script>
|
36
36
|
<script src='factory_guy_test.js'></script>
|
37
37
|
<script src='factory_guy_test_mixin_test.js'></script>
|
data/tests/store_test.js
CHANGED
@@ -29,4 +29,202 @@ test("with DS.ActiveModelAdapter", function() {
|
|
29
29
|
equal(store.usingFixtureAdapter(), false );
|
30
30
|
});
|
31
31
|
|
32
|
+
var store, testHelper, testFixtureEquality;
|
33
|
+
module('DS.Store#pushPayload', {
|
34
|
+
setup: function(){
|
35
|
+
testHelper = TestHelper.setup(DS.FixtureAdapter);
|
36
|
+
store = testHelper.getStore();
|
37
|
+
},
|
38
|
+
teardown: function() {
|
39
|
+
Em.run(function() {
|
40
|
+
testHelper.teardown();
|
41
|
+
});
|
42
|
+
}
|
43
|
+
});
|
44
|
+
|
45
|
+
testFixtureEquality = function(models, json, num){
|
46
|
+
equal(models.get('length'), num, 'Expected ' + num + ' ' + models.type.typeKey + ' in the data store but got ' + models.get('length'));
|
47
|
+
deepEqual(
|
48
|
+
models.map(function(model){ return model.get('id') }).sort(),
|
49
|
+
json.map(function(model){ return model.id; }).sort(),
|
50
|
+
'Expected to have all ' + Ember.String.pluralize(models.type.typeKey) + ' created'
|
51
|
+
);
|
52
|
+
}
|
53
|
+
|
54
|
+
asyncTest("no root", function() {
|
55
|
+
var userJson = {
|
56
|
+
id: '1',
|
57
|
+
name: 'monkey'
|
58
|
+
};
|
59
|
+
|
60
|
+
store.pushPayload('user', userJson);
|
61
|
+
store.find('user').then(function(users){
|
62
|
+
equal(users.get('length'), 1, 'Expected one user in the data store');
|
63
|
+
|
64
|
+
equal(users.get('firstObject.id'), userJson.id);
|
65
|
+
equal(users.get('firstObject.name'), userJson.name);
|
66
|
+
start();
|
67
|
+
});
|
68
|
+
});
|
69
|
+
|
70
|
+
asyncTest("no root replace data", function(){
|
71
|
+
var projectJson = {
|
72
|
+
id: '1',
|
73
|
+
title: 'monkey',
|
74
|
+
user: null
|
75
|
+
};
|
76
|
+
var u = FactoryGuy.make('user', { id: '1', name: 'banana' });
|
77
|
+
var p = FactoryGuy.make('project', { user: u.id});
|
78
|
+
store.find('user');
|
79
|
+
|
80
|
+
store.find('project').then(function(projects) {
|
81
|
+
var project = projects.get('firstObject'),
|
82
|
+
user = project.get('user');
|
83
|
+
|
84
|
+
equal(user.get('name'), u.name, 'Expected user name to be equal');
|
85
|
+
equal(user.get('id'), u.id, 'Expected user id to be equal');
|
86
|
+
equal(project.get('title'), p.title, 'Expected project title to equal');
|
87
|
+
|
88
|
+
store.pushPayload('project', projectJson);
|
89
|
+
|
90
|
+
ok(Ember.isEmpty(project.get('user')), 'Did not expect a user on project');
|
91
|
+
equal(project.get('title'), projectJson.title, 'Expected project title to equal');
|
92
|
+
start();
|
93
|
+
});
|
94
|
+
});
|
95
|
+
|
96
|
+
asyncTest("single root", function() {
|
97
|
+
var userJson = {
|
98
|
+
users: [{ id:'1', name:'monkey' }, { id:'2', name:'banana' }]
|
99
|
+
};
|
100
|
+
|
101
|
+
store.pushPayload(userJson);
|
102
|
+
store.find('user').then(function(users) {
|
103
|
+
|
104
|
+
testFixtureEquality(users, userJson['users'], 2);
|
105
|
+
|
106
|
+
users.forEach(function(user) {
|
107
|
+
var u = userJson['users'].findBy('id', user.get('id'));
|
108
|
+
equal(user.get('name'), u.name, 'Got unexpected name ' + user.get('name'));
|
109
|
+
});
|
110
|
+
|
111
|
+
start();
|
112
|
+
});
|
113
|
+
});
|
114
|
+
|
115
|
+
asyncTest("multiple roots", function() {
|
116
|
+
var json = {
|
117
|
+
companies: [{ id: '1', name: 'Google', projects: ['1', '2'] }],
|
118
|
+
projects: [
|
119
|
+
{ id:'1', title: 'Docs', user: '1' },
|
120
|
+
{ id:'2', title: 'Gmail' }
|
121
|
+
],
|
122
|
+
users: [{ id: '1', name: 'monkey', projects: ['1'] }],
|
123
|
+
profiles: [
|
124
|
+
{ id: '1', created_at: new Date(), description: 'banana' },
|
125
|
+
{ id: '2', created_at: new Date(), description: 'monkey' }
|
126
|
+
],
|
127
|
+
hats: [
|
128
|
+
{ id: '2', type: 'Trilby' }
|
129
|
+
],
|
130
|
+
};
|
32
131
|
|
132
|
+
store.pushPayload(json);
|
133
|
+
Ember.RSVP.Promise.all([
|
134
|
+
store.find('company'), store.find('project'),
|
135
|
+
store.find('user'), store.find('profile'), store.find('hat')
|
136
|
+
]).then(function(data) {
|
137
|
+
var companies = data[0],
|
138
|
+
projects = data[1],
|
139
|
+
users = data[2],
|
140
|
+
profiles = data[3],
|
141
|
+
hats = data[4];
|
142
|
+
|
143
|
+
testFixtureEquality(companies, json['companies'], 1);
|
144
|
+
testFixtureEquality(projects, json['projects'], 2);
|
145
|
+
testFixtureEquality(users, json['users'], 1);
|
146
|
+
testFixtureEquality(profiles, json['profiles'], 2);
|
147
|
+
testFixtureEquality(hats, json['hats'], 1);
|
148
|
+
|
149
|
+
start();
|
150
|
+
});
|
151
|
+
});
|
152
|
+
|
153
|
+
asyncTest("multiple roots with type specified", function() {
|
154
|
+
var json = {
|
155
|
+
companies: [{ id: '1', name: 'Google', projects: ['1', '2'] }],
|
156
|
+
projects: [
|
157
|
+
{ id:'1', title: 'Docs', user: '1' },
|
158
|
+
{ id:'2', title: 'Gmail' }
|
159
|
+
],
|
160
|
+
users: [{ id: '1', name: 'monkey', projects: ['1'] }],
|
161
|
+
}
|
162
|
+
|
163
|
+
store.pushPayload('company', json);
|
164
|
+
|
165
|
+
Ember.RSVP.Promise.all([
|
166
|
+
store.find('company'), store.find('project'), store.find('user')
|
167
|
+
]).then(function(data) {
|
168
|
+
var companies = data[0],
|
169
|
+
projects = data[1],
|
170
|
+
users = data[2];
|
171
|
+
|
172
|
+
testFixtureEquality(companies, json['companies'], 1);
|
173
|
+
testFixtureEquality(projects, json['projects'], 2);
|
174
|
+
testFixtureEquality(users, json['users'], 1);
|
175
|
+
|
176
|
+
start();
|
177
|
+
});
|
178
|
+
});
|
179
|
+
|
180
|
+
test('testing for exceptions', function(){
|
181
|
+
throws(function() { store.pushPayload('user'); }, Ember.Error, 'Excepted to raise Ember.Error');
|
182
|
+
throws(function() { store.pushPayload('user', { name: 'foo' }); }, Ember.Error, 'Excepted to raise Ember.Error');
|
183
|
+
throws(function() { store.pushPayload({ someCrazyModelNotPresent: { id: 1, name: 'foo' } }); }, 'Excepted exception');
|
184
|
+
throws(function() { store.pushPayload('someCrazyModelNotPresent', { id: 1, name: 'foo' }); }, 'Excepted exception');
|
185
|
+
|
186
|
+
//no exceptions
|
187
|
+
var noExceptions = true
|
188
|
+
try {
|
189
|
+
store.pushPayload('user', {});
|
190
|
+
store.pushPayload({});
|
191
|
+
} catch(error) {
|
192
|
+
noExceptions = false;
|
193
|
+
}
|
194
|
+
|
195
|
+
ok(noExceptions, 'Should not raise exceptions');
|
196
|
+
});
|
197
|
+
|
198
|
+
asyncTest("replacing record", function() {
|
199
|
+
var json = {
|
200
|
+
companies: [{ id: '1', name: 'Google', projects: ['2', '3'] }],
|
201
|
+
projects: [
|
202
|
+
{ id:'2', title: 'Docs', user: '1' },
|
203
|
+
{ id:'3', title: 'Gmail' }
|
204
|
+
],
|
205
|
+
users: [{ id: '1', name: 'monkey', projects: ['2'] }],
|
206
|
+
profiles: [
|
207
|
+
{ id: '1', created_at: new Date(), description: 'banana' },
|
208
|
+
{ id: '2', created_at: new Date(), description: 'monkey' }
|
209
|
+
],
|
210
|
+
hats: [
|
211
|
+
{ id: '2', type: 'Trilby' }
|
212
|
+
],
|
213
|
+
};
|
214
|
+
|
215
|
+
var p = FactoryGuy.make('project');
|
216
|
+
FactoryGuy.make('user', { id: '1', name: 'banana', projects: [ p.id ] });
|
217
|
+
store.find('project');
|
218
|
+
|
219
|
+
store.find('user').then(function(user) {
|
220
|
+
equal(user.get('firstObject.projects.length'), 1, 'Expected one project');
|
221
|
+
equal(user.get('firstObject.projects.firstObject.id'), '1', 'Expected project with id 1');
|
222
|
+
|
223
|
+
store.pushPayload(json);
|
224
|
+
|
225
|
+
equal(user.get('firstObject.projects.length'), 1, 'Expected one project');
|
226
|
+
equal(user.get('firstObject.projects.firstObject.id'), '2', 'Expected project with id 2');
|
227
|
+
|
228
|
+
start();
|
229
|
+
});
|
230
|
+
});
|
@@ -3,6 +3,9 @@ FactoryGuy.define("company", {
|
|
3
3
|
name: 'Silly corp'
|
4
4
|
},
|
5
5
|
traits: {
|
6
|
+
with_profile: {
|
7
|
+
profile: {}
|
8
|
+
},
|
6
9
|
with_projects: {
|
7
10
|
projects: FactoryGuy.hasMany('project', 2)
|
8
11
|
}
|
@@ -15,4 +18,4 @@ FactoryGuy.define("small_company", {
|
|
15
18
|
name: 'Small Corp',
|
16
19
|
projects: FactoryGuy.hasMany('project', 2)
|
17
20
|
}
|
18
|
-
})
|
21
|
+
})
|
@@ -3,7 +3,7 @@ User = DS.Model.extend({
|
|
3
3
|
info: DS.attr('object'),
|
4
4
|
company: DS.belongsTo('company', {async: true, inverse: 'users', polymorphic: true}),
|
5
5
|
properties: DS.hasMany('property', {async: true, inverse: 'owners'}),
|
6
|
-
projects: DS.hasMany('project'),
|
6
|
+
projects: DS.hasMany('project', {embedded: 'always'}),
|
7
7
|
hats: DS.hasMany('hat', {polymorphic: true})
|
8
8
|
});
|
9
9
|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
ObjectTransform = DS.Transform.extend({
|
2
2
|
serialize: function(obj) {
|
3
|
-
return JSON.parse(obj);
|
3
|
+
return obj ? JSON.parse(obj) : {};
|
4
4
|
},
|
5
5
|
deserialize: function(obj) {
|
6
|
-
return JSON.stringify(obj);
|
6
|
+
return obj ? JSON.stringify(obj) : '{}';
|
7
7
|
}
|
8
8
|
});
|
9
9
|
|
@@ -95,4 +95,4 @@ TestHelper = Ember.Object.createWithMixins(FactoryGuy.testMixin,{
|
|
95
95
|
return setupStore(options).store;
|
96
96
|
};
|
97
97
|
|
98
|
-
})();
|
98
|
+
})();
|
data/tests/test_setup.js
CHANGED
@@ -63,7 +63,9 @@ Outfit = DS.Model.extend({
|
|
63
63
|
|
64
64
|
Person = DS.Model.extend({
|
65
65
|
type: DS.attr('string'),
|
66
|
-
name: DS.attr('string')
|
66
|
+
name: DS.attr('string'),
|
67
|
+
company: DS.belongsTo('company', {embedded: 'always'}),
|
68
|
+
outfits: DS.hasMany('outfit', {embedded: 'always'})
|
67
69
|
})
|
68
70
|
|
69
71
|
Profile = DS.Model.extend({
|
@@ -97,7 +99,7 @@ User = DS.Model.extend({
|
|
97
99
|
info: DS.attr('object'),
|
98
100
|
company: DS.belongsTo('company', {async: true, inverse: 'users', polymorphic: true}),
|
99
101
|
properties: DS.hasMany('property', {async: true, inverse: 'owners'}),
|
100
|
-
projects: DS.hasMany('project'),
|
102
|
+
projects: DS.hasMany('project', {embedded: 'always'}),
|
101
103
|
hats: DS.hasMany('hat', {polymorphic: true})
|
102
104
|
});
|
103
105
|
|
@@ -107,6 +109,9 @@ FactoryGuy.define("company", {
|
|
107
109
|
name: 'Silly corp'
|
108
110
|
},
|
109
111
|
traits: {
|
112
|
+
with_profile: {
|
113
|
+
profile: {}
|
114
|
+
},
|
110
115
|
with_projects: {
|
111
116
|
projects: FactoryGuy.hasMany('project', 2)
|
112
117
|
}
|
@@ -120,6 +125,7 @@ FactoryGuy.define("small_company", {
|
|
120
125
|
projects: FactoryGuy.hasMany('project', 2)
|
121
126
|
}
|
122
127
|
})
|
128
|
+
|
123
129
|
FactoryGuy.define("group", {
|
124
130
|
sequences: {
|
125
131
|
name: function(num) {return 'Group' + num}
|
@@ -242,8 +248,14 @@ FactoryGuy.define("project", {
|
|
242
248
|
FactoryGuy.define('property', {
|
243
249
|
default: {
|
244
250
|
name: 'Silly property'
|
251
|
+
},
|
252
|
+
traits: {
|
253
|
+
with_owners_with_projects: {
|
254
|
+
owners: FactoryGuy.hasMany('user', 2, 'with_projects')
|
255
|
+
}
|
245
256
|
}
|
246
257
|
})
|
258
|
+
|
247
259
|
FactoryGuy.define('user', {
|
248
260
|
sequences: {
|
249
261
|
name: function(num) {return 'User' + num}
|
@@ -260,6 +272,9 @@ FactoryGuy.define('user', {
|
|
260
272
|
projects: FactoryGuy.hasMany('project', 2)
|
261
273
|
},
|
262
274
|
traits: {
|
275
|
+
with_company: {
|
276
|
+
company: {}
|
277
|
+
},
|
263
278
|
with_projects: {
|
264
279
|
projects: FactoryGuy.hasMany('project', 2)
|
265
280
|
},
|
@@ -462,7 +462,9 @@ var FactoryGuy = {
|
|
462
462
|
|
463
463
|
if (store.usingFixtureAdapter()) {
|
464
464
|
store.setAssociationsForFixtureAdapter(modelType, modelName, fixture);
|
465
|
-
|
465
|
+
fixture = FactoryGuy.pushFixture(modelType, fixture);
|
466
|
+
store.loadModelForFixtureAdapter(modelType, fixture);
|
467
|
+
return fixture;
|
466
468
|
} else {
|
467
469
|
return store.makeModel(modelType, fixture);
|
468
470
|
}
|
@@ -643,9 +645,11 @@ var FactoryGuy = {
|
|
643
645
|
setAssociationsForFixtureAdapter: function (modelType, modelName, fixture) {
|
644
646
|
var self = this;
|
645
647
|
var adapter = this.adapterFor('application');
|
648
|
+
|
646
649
|
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship, name) {
|
650
|
+
var hasManyRelation, belongsToRecord;
|
647
651
|
if (relationship.kind == 'hasMany') {
|
648
|
-
|
652
|
+
hasManyRelation = fixture[relationship.key];
|
649
653
|
if (hasManyRelation) {
|
650
654
|
$.each(fixture[relationship.key], function (index, object) {
|
651
655
|
// used to require that the relationship was set by id,
|
@@ -653,17 +657,19 @@ var FactoryGuy = {
|
|
653
657
|
// normalize that back to the id
|
654
658
|
var id = object;
|
655
659
|
if (Ember.typeOf(object) == 'object') {
|
660
|
+
FactoryGuy.pushFixture(relationship.type, object);
|
656
661
|
id = object.id;
|
657
662
|
hasManyRelation[index] = id;
|
658
663
|
}
|
659
664
|
var hasManyfixtures = adapter.fixturesForType(relationship.type);
|
660
|
-
var
|
661
|
-
|
665
|
+
var hasManyFixture = adapter.findFixtureById(hasManyfixtures, id);
|
666
|
+
hasManyFixture[modelName] = fixture.id;
|
667
|
+
self.loadModelForFixtureAdapter(relationship.type, hasManyFixture);
|
662
668
|
});
|
663
669
|
}
|
664
670
|
}
|
665
671
|
if (relationship.kind == 'belongsTo') {
|
666
|
-
|
672
|
+
belongsToRecord = fixture[relationship.key];
|
667
673
|
if (belongsToRecord) {
|
668
674
|
if (typeof belongsToRecord == 'object') {
|
669
675
|
FactoryGuy.pushFixture(relationship.type, belongsToRecord);
|
@@ -676,10 +682,29 @@ var FactoryGuy = {
|
|
676
682
|
belongsTofixture[hasManyName] = [];
|
677
683
|
}
|
678
684
|
belongsTofixture[hasManyName].push(fixture.id);
|
685
|
+
self.loadModelForFixtureAdapter(relationship.type, belongsTofixture);
|
679
686
|
}
|
680
687
|
}
|
681
688
|
});
|
682
689
|
},
|
690
|
+
|
691
|
+
loadModelForFixtureAdapter: function(modelType, fixture) {
|
692
|
+
var storeModel = this.getById(modelType, fixture.id),
|
693
|
+
that = this;
|
694
|
+
if (!Ember.isPresent(storeModel) || storeModel.get('isEmpty')) {
|
695
|
+
Ember.run(function () {
|
696
|
+
var dup = Ember.copy(fixture, true);
|
697
|
+
that.push(modelType, fixture);
|
698
|
+
//replace relationships back to ids instead of built ember objects
|
699
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship, name) {
|
700
|
+
if(fixture[relationship.key]) {
|
701
|
+
fixture[relationship.key] = dup[relationship.key];
|
702
|
+
}
|
703
|
+
});
|
704
|
+
});
|
705
|
+
}
|
706
|
+
},
|
707
|
+
|
683
708
|
/**
|
684
709
|
Before pushing the fixture to the store, do some preprocessing. Descend into the tree
|
685
710
|
of object data, and convert child objects to record instances recursively.
|
@@ -788,9 +813,32 @@ var FactoryGuy = {
|
|
788
813
|
@param {Object} payload
|
789
814
|
*/
|
790
815
|
pushPayload: function (type, payload) {
|
816
|
+
var typeName, model;
|
817
|
+
|
791
818
|
if (this.usingFixtureAdapter()) {
|
792
|
-
|
793
|
-
|
819
|
+
if (Ember.typeOf(type) === 'string' && Ember.isPresent(payload) && Ember.isPresent(payload.id)){
|
820
|
+
//pushPayload('user', {id:..})
|
821
|
+
model = this.modelFor(type);
|
822
|
+
FactoryGuy.pushFixture(model, payload);
|
823
|
+
this.push(model, Ember.copy(payload, true));
|
824
|
+
} else if(Ember.typeOf(type) === 'object' || Ember.typeOf(payload) === 'object') {
|
825
|
+
//pushPayload({users: {id:..}}) OR pushPayload('user', {users: {id:..}})
|
826
|
+
if(Ember.isBlank(payload)){
|
827
|
+
payload = type;
|
828
|
+
}
|
829
|
+
|
830
|
+
for (var prop in payload) {
|
831
|
+
typeName = Ember.String.camelize(Ember.String.singularize(prop));
|
832
|
+
model = this.modelFor(typeName);
|
833
|
+
|
834
|
+
this.pushMany(model, Ember.makeArray( Ember.copy(payload[prop], true) ));
|
835
|
+
Ember.ArrayPolyfills.forEach.call(Ember.makeArray(payload[prop]), function(hash) {
|
836
|
+
FactoryGuy.pushFixture(model, hash);
|
837
|
+
}, this);
|
838
|
+
}
|
839
|
+
} else {
|
840
|
+
throw new Ember.Error('Assertion Failed: You cannot use `store#pushPayload` with this method signature pushPayload(' + type + ',' + payload + ')');
|
841
|
+
}
|
794
842
|
} else {
|
795
843
|
this._super(type, payload);
|
796
844
|
}
|
@@ -877,6 +925,17 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
877
925
|
Handling ajax GET for finding all records for a type of model.
|
878
926
|
You can mock failed find by passing in success argument as false.
|
879
927
|
|
928
|
+
```js
|
929
|
+
// Pass in the parameters you would normally pass into FactoryGuy.makeList,
|
930
|
+
// like fixture name, number of fixtures to make, and optional traits,
|
931
|
+
// or fixture options
|
932
|
+
testHelper.handleFindMany('user', 2, 'with_hats');
|
933
|
+
|
934
|
+
store.find('user').then(function(users){
|
935
|
+
|
936
|
+
});
|
937
|
+
```
|
938
|
+
|
880
939
|
@param {String} name name of the fixture ( or model ) to find
|
881
940
|
@param {Number} number number of fixtures to create
|
882
941
|
@param {String} trait optional traits (one or more)
|
@@ -884,39 +943,54 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
884
943
|
*/
|
885
944
|
handleFindMany: function () {
|
886
945
|
// make the records and load them in the store
|
887
|
-
FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
946
|
+
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
888
947
|
var name = arguments[0];
|
889
948
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
890
949
|
var responseJson = {};
|
891
|
-
|
950
|
+
var json = records.map(function(record) {return record.toJSON({includeId: true})});
|
951
|
+
responseJson[modelName.pluralize()] = json;
|
892
952
|
var url = this.buildURL(modelName);
|
893
|
-
// mock the ajax call, but return nothing, since the records will be
|
894
|
-
// retrieved from the store where they were just loaded above
|
895
953
|
this.stubEndpointForHttpRequest(url, responseJson);
|
896
954
|
},
|
897
955
|
/**
|
898
956
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
899
957
|
|
958
|
+
First variation = pass in model instances
|
900
959
|
```js
|
901
|
-
// First build json for the instances you want 'returned' in your query.
|
902
|
-
var usersJson = FactoryGuy.buildList('user', 2);
|
903
960
|
|
904
|
-
//
|
905
|
-
|
906
|
-
|
961
|
+
// Create model instances
|
962
|
+
var users = FactoryGuy.makeList('user', 2, 'with_hats');
|
963
|
+
|
964
|
+
// Pass in the array of model instances as last argument
|
965
|
+
testHelper.handleFindQuery('user', ['name', 'age'], users);
|
907
966
|
|
908
967
|
store.findQuery('user', {name:'Bob', age: 10}}).then(function(userInstances){
|
909
|
-
/// userInstances
|
968
|
+
/// userInstances will be the same of the users that were passed in
|
910
969
|
})
|
911
970
|
```
|
912
971
|
|
913
|
-
|
972
|
+
Third variation - pass in nothing for last argument
|
973
|
+
```js
|
974
|
+
// This simulates a query that returns no results
|
975
|
+
testHelper.handleFindQuery('user', ['age']);
|
976
|
+
|
977
|
+
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
978
|
+
/// userInstances will be empty
|
979
|
+
})
|
980
|
+
```
|
914
981
|
|
915
982
|
@param {String} modelName name of the mode like 'user' for User model type
|
916
983
|
@param {String} searchParams the parameters that will be queried
|
917
|
-
@param {
|
984
|
+
@param {Array} array of DS.Model records to be 'returned' by query
|
918
985
|
*/
|
919
|
-
handleFindQuery: function (modelName, searchParams,
|
986
|
+
handleFindQuery: function (modelName, searchParams, records) {
|
987
|
+
Ember.assert('The second argument of searchParams must be an array',Em.typeOf(searchParams) == 'array')
|
988
|
+
if (records) {
|
989
|
+
Ember.assert('The third argument ( records ) must be an array - found type:' + Em.typeOf(records), Em.typeOf(records) == 'array')
|
990
|
+
} else {
|
991
|
+
records = []
|
992
|
+
}
|
993
|
+
var json = records.map(function(record) {return record.toJSON({includeId: true})})
|
920
994
|
var responseJson = {};
|
921
995
|
responseJson[modelName.pluralize()] = json;
|
922
996
|
var url = this.buildURL(modelName);
|
@@ -1163,10 +1237,12 @@ if (FactoryGuy !== undefined) {
|
|
1163
1237
|
return null;
|
1164
1238
|
}
|
1165
1239
|
}
|
1166
|
-
|
1167
1240
|
// Inspect the data submitted in the request (either POST body or GET query string)
|
1168
1241
|
if ( handler.data ) {
|
1169
|
-
|
1242
|
+
// console.log('request.data', requestSettings.data )
|
1243
|
+
// console.log('handler.data', handler.data )
|
1244
|
+
// console.log('data equal', isMockDataEqual(handler.data, requestSettings.data) )
|
1245
|
+
if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) {
|
1170
1246
|
// They're not identical, do not mock this request
|
1171
1247
|
return null;
|
1172
1248
|
}
|
@@ -1177,7 +1253,6 @@ if (FactoryGuy !== undefined) {
|
|
1177
1253
|
// The request type doesn't match (GET vs. POST)
|
1178
1254
|
return null;
|
1179
1255
|
}
|
1180
|
-
|
1181
1256
|
return handler;
|
1182
1257
|
}
|
1183
1258
|
|