ember-data-factory-guy 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +133 -129
- data/bower.json +1 -1
- data/dist/amd/factory-guy.js +38 -4
- data/dist/ember-data-factory-guy.js +37 -3
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/package.json +1 -1
- data/src/factory_guy_test_mixin.js +37 -3
- data/tests/factory_guy_test_mixin_test.js +44 -0
- data/vendor/assets/javascripts/ember_data_factory_guy.js +38 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3487ba4072e7e89a32ffd9235291a65f24f79503
|
4
|
+
data.tar.gz: c55698399c38e7f46f7036346a379f2a4648def4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fa20e03471792329dfec799f3a47f6471d7c3d255ae024e615d6c64fdc28e2d2419b8bfdffd1a0ceac2d8b59456abab91164637489f6e888ed920bd400f3155
|
7
|
+
data.tar.gz: 5002f87e0d686881e3260d2725c4d2ce5a2e1d65fd13c9e3d7faf4e109b2075258fc24bfda52adaad5bda794e7766825bcf3364838e0eb90b4444d2ceb1be1f5
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Ember Data Factory Guy [![Build Status](https://secure.travis-ci.org/danielspaniel/ember-data-factory-guy.png?branch=master)](http://travis-ci.org/danielspaniel/ember-data-factory-guy)
|
2
2
|
|
3
3
|
*NOTE*
|
4
|
-
|
4
|
+
|
5
5
|
ember-data is changing the way they are doing relationships in 1.0.0-beta.10 and above
|
6
6
|
so, if you are using ember-data-1.0.0-beta.8 and earlier, then be sure to use version 0.6.4
|
7
7
|
of ember-data-factory-guy.
|
@@ -10,9 +10,9 @@ of ember-data-factory-guy.
|
|
10
10
|
- 0.6.4 -> ember-data-1.0.0-beta.8 and under
|
11
11
|
- 0.7.1.1 -> ember-data-1.0.0-beta.10
|
12
12
|
- 0.8.6 -> ember-data-1.0.0-beta.11
|
13
|
-
- 0.9.
|
13
|
+
- 0.9.3 -> ember-data-1.0.0-beta.12
|
14
14
|
|
15
|
-
**Support for fixture adapter is
|
15
|
+
** Support for fixture adapter is back in business as of version 0.9.3 **
|
16
16
|
|
17
17
|
*Version 0.9.0 and up deprecates explicit call to store.makeFixture in your tests, in favor
|
18
18
|
of using the FactoryGuy.make or testHelper.make function from FactoryGuyTestHelperMixin instead.
|
@@ -21,8 +21,8 @@ FactoryGuy.setStore(store) somewhere in your code before you start making fixtur
|
|
21
21
|
|
22
22
|
## Using with Ember Cli
|
23
23
|
- https://github.com/igorrKurr/ember-cli-factory-guy-example
|
24
|
-
An example of how to manually set up ember-data-factory-guy with ember cli
|
25
|
-
|
24
|
+
An example of how to manually set up ember-data-factory-guy with ember cli
|
25
|
+
|
26
26
|
- https://github.com/cristinawithout/ember-cli-data-factory-guy
|
27
27
|
A wrapper around ember-data-factory-guy for ember-cli for even easier setup
|
28
28
|
|
@@ -39,7 +39,7 @@ gem 'ember-data-factory-guy', group: test
|
|
39
39
|
or for particular version:
|
40
40
|
|
41
41
|
```ruby
|
42
|
-
gem 'ember-data-factory-guy', '0.9.
|
42
|
+
gem 'ember-data-factory-guy', '0.9.3', group: test
|
43
43
|
```
|
44
44
|
|
45
45
|
then:
|
@@ -60,7 +60,7 @@ require the 'ember_data_factory_guy' javascript file in your test helper
|
|
60
60
|
|
61
61
|
Add as one of your dependencies in bower.json file:
|
62
62
|
|
63
|
-
```json
|
63
|
+
```json
|
64
64
|
"dependencies": {
|
65
65
|
"foo-dependency": "latest",
|
66
66
|
"other-foo-dependency": "latest",
|
@@ -74,7 +74,7 @@ or for particular version:
|
|
74
74
|
"dependencies": {
|
75
75
|
"foo-dependency": "latest",
|
76
76
|
"other-foo-dependency": "latest",
|
77
|
-
"ember-data-factory-guy": "0.9.
|
77
|
+
"ember-data-factory-guy": "0.9.3"
|
78
78
|
}
|
79
79
|
```
|
80
80
|
|
@@ -85,29 +85,29 @@ then:
|
|
85
85
|
|
86
86
|
### How this works
|
87
87
|
|
88
|
-
- Using DS.RestAdapter / DS.ActiveModelAdapter
|
88
|
+
- Using DS.RestAdapter / DS.ActiveModelAdapter
|
89
89
|
- Add record instances to the store
|
90
|
-
- Faster, since models can be accessed synchronously
|
91
|
-
- Using DS.FixtureAdapter
|
90
|
+
- Faster, since models can be accessed synchronously
|
91
|
+
- Using DS.FixtureAdapter
|
92
92
|
- Add fixtures to the store
|
93
93
|
- Slower, since models are accessed asynchronously
|
94
|
-
|
94
|
+
|
95
95
|
|
96
96
|
##### DS.RestAdapter / DS.ActiveModelAdapter
|
97
97
|
|
98
|
-
The preferred way to use this project is to use the default adapter for your project,
|
98
|
+
The preferred way to use this project is to use the default adapter for your project,
|
99
99
|
which is usually going to be the RESTAdapter/ActiveModelAdapter.
|
100
|
-
*In other words, it is NOT recommended to use the DS.FixtureAdapter.*
|
100
|
+
*In other words, it is NOT recommended to use the DS.FixtureAdapter.*
|
101
101
|
|
102
|
-
When you call: store.makeFixture('user'), you create model in the store and this method
|
103
|
-
returns this model instance
|
102
|
+
When you call: store.makeFixture('user'), you create model in the store and this method
|
103
|
+
returns this model instance
|
104
104
|
|
105
105
|
*Since you are synchronously getting model instances, you can immediately start asking
|
106
106
|
for data from the model, and its associations, which is why it is faster to use
|
107
107
|
the REST/ActiveModel adapter than the FixtureAdapter*
|
108
108
|
|
109
109
|
##### Using DS.FixtureAdapter
|
110
|
-
|
110
|
+
|
111
111
|
The benefit of using FactoryGuy is that you can run your tests with the
|
112
112
|
default adapter that your application's store normally uses. In other words:
|
113
113
|
You do not have to use the DS.FixtureAdapter. But if you do choose to use the Fixture adapter,
|
@@ -119,10 +119,10 @@ but some of the associated records were not loaded. Either make sure they are al
|
|
119
119
|
|
120
120
|
If you do get these types of errors try requiring the factory_guy_has_many.js file
|
121
121
|
( located in dist dir and vendor dir ) AFTER you require ember-data,
|
122
|
-
but BEFORE you require your models.
|
122
|
+
but BEFORE you require your models.
|
123
123
|
|
124
124
|
|
125
|
-
### Setup
|
125
|
+
### Setup
|
126
126
|
|
127
127
|
In the following examples, assume the models look like this:
|
128
128
|
|
@@ -139,22 +139,22 @@ In the following examples, assume the models look like this:
|
|
139
139
|
title: DS.attr('string'),
|
140
140
|
user: DS.belongsTo('user')
|
141
141
|
});
|
142
|
-
|
143
|
-
// polymorphic models
|
142
|
+
|
143
|
+
// polymorphic models
|
144
144
|
Hat = DS.Model.extend({
|
145
145
|
type: DS.attr('string'),
|
146
146
|
user: DS.belongsTo('user')
|
147
147
|
});
|
148
|
-
|
148
|
+
|
149
149
|
BigHat = Hat.extend();
|
150
150
|
SmallHat = Hat.extend();
|
151
151
|
```
|
152
152
|
|
153
153
|
|
154
154
|
### Defining Factories
|
155
|
-
- A factory has a name and a set of attributes.
|
156
|
-
- The name should match the model type name. So, for 'User' model, the name would be 'user'
|
157
|
-
|
155
|
+
- A factory has a name and a set of attributes.
|
156
|
+
- The name should match the model type name. So, for 'User' model, the name would be 'user'
|
157
|
+
|
158
158
|
|
159
159
|
##### Standard models
|
160
160
|
|
@@ -187,7 +187,7 @@ It is better to define each polymorphic model in it's own typed definition:
|
|
187
187
|
type: 'SmallHat'
|
188
188
|
}
|
189
189
|
})
|
190
|
-
|
190
|
+
|
191
191
|
FactoryGuy.define('big_hat', {
|
192
192
|
default: {
|
193
193
|
type: 'BigHat'
|
@@ -199,7 +199,7 @@ It is better to define each polymorphic model in it's own typed definition:
|
|
199
199
|
rather than doing this:
|
200
200
|
|
201
201
|
```javascript
|
202
|
-
|
202
|
+
|
203
203
|
FactoryGuy.define('hat', {
|
204
204
|
default: {},
|
205
205
|
small_hat: {
|
@@ -209,10 +209,10 @@ rather than doing this:
|
|
209
209
|
type: 'BigHat'
|
210
210
|
}
|
211
211
|
})
|
212
|
-
|
212
|
+
|
213
213
|
```
|
214
214
|
|
215
|
-
Since there are times that the latter can cause problems when
|
215
|
+
Since there are times that the latter can cause problems when
|
216
216
|
the store is looking up the correct model type name
|
217
217
|
|
218
218
|
|
@@ -220,12 +220,12 @@ the store is looking up the correct model type name
|
|
220
220
|
- FactorGuy.setStore
|
221
221
|
- pass in the store instance to FactoryGuy before making fixtures.
|
222
222
|
- FactoryGuy.build
|
223
|
-
- Builds json
|
223
|
+
- Builds json
|
224
224
|
- FactoryGuy.make
|
225
|
-
- Loads model instance into the store
|
225
|
+
- Loads model instance into the store
|
226
226
|
- Can override default attributes by passing in a hash
|
227
|
-
- Can add attributes with traits ( see traits section )
|
228
|
-
|
227
|
+
- Can add attributes with traits ( see traits section )
|
228
|
+
|
229
229
|
```javascript
|
230
230
|
// First set the store on FactoryGuy. You don't have to do this step manually
|
231
231
|
// if you use FactoryGuyTestHelperMixin since this is done for you in the setup
|
@@ -234,41 +234,41 @@ the store is looking up the correct model type name
|
|
234
234
|
var store = App.__container__.lookup('store:main');
|
235
235
|
FactoryGuy.setStore(store);
|
236
236
|
|
237
|
-
// returns json
|
238
|
-
var json = FactoryGuy.build('user');
|
237
|
+
// returns json
|
238
|
+
var json = FactoryGuy.build('user');
|
239
239
|
json // => {id: 1, name: 'Dude', style: 'normal'}
|
240
240
|
|
241
|
-
// returns a User instance that is loaded into your application's store
|
241
|
+
// returns a User instance that is loaded into your application's store
|
242
242
|
var user = FactoryGuy.make('user');
|
243
243
|
user.toJSON({includeId: true}) // => {id: 2, name: 'Dude', style: 'normal'}
|
244
244
|
|
245
|
-
var json = FactoryGuy.build('admin');
|
245
|
+
var json = FactoryGuy.build('admin');
|
246
246
|
json // => {id: 3, name: 'Admin', style: 'super'}
|
247
247
|
|
248
248
|
var user = FactoryGuy.make('admin');
|
249
249
|
user.toJSON({includeId: true}) // => {id: 4, name: 'Admin', style: 'super'}
|
250
|
-
|
250
|
+
|
251
251
|
```
|
252
252
|
|
253
|
-
You can override the default attributes by passing in a hash
|
253
|
+
You can override the default attributes by passing in a hash
|
254
254
|
|
255
255
|
```javascript
|
256
|
-
|
257
|
-
var json = FactoryGuy.build('user', {name: 'Fred'});
|
256
|
+
|
257
|
+
var json = FactoryGuy.build('user', {name: 'Fred'});
|
258
258
|
// json.name => 'Fred'
|
259
|
-
|
259
|
+
|
260
260
|
```
|
261
261
|
|
262
262
|
|
263
263
|
### Sequences
|
264
264
|
|
265
|
-
- For generating unique attribute values.
|
266
|
-
- Can be defined:
|
267
|
-
- In the model definition's sequences hash
|
268
|
-
- Inline on the attribute
|
265
|
+
- For generating unique attribute values.
|
266
|
+
- Can be defined:
|
267
|
+
- In the model definition's sequences hash
|
268
|
+
- Inline on the attribute
|
269
269
|
- Values are generated by calling FactoryGuy.generate
|
270
270
|
|
271
|
-
##### Declaring sequences in sequences hash
|
271
|
+
##### Declaring sequences in sequences hash
|
272
272
|
|
273
273
|
```javascript
|
274
274
|
|
@@ -285,7 +285,7 @@ You can override the default attributes by passing in a hash
|
|
285
285
|
}
|
286
286
|
});
|
287
287
|
|
288
|
-
var json = FactoryGuy.build('user');
|
288
|
+
var json = FactoryGuy.build('user');
|
289
289
|
json.name // => 'User1'
|
290
290
|
|
291
291
|
var user = FactoryGuy.make('user');
|
@@ -293,7 +293,7 @@ You can override the default attributes by passing in a hash
|
|
293
293
|
|
294
294
|
```
|
295
295
|
|
296
|
-
##### Declaring an inline sequence on attribute
|
296
|
+
##### Declaring an inline sequence on attribute
|
297
297
|
|
298
298
|
```javascript
|
299
299
|
|
@@ -303,7 +303,7 @@ You can override the default attributes by passing in a hash
|
|
303
303
|
},
|
304
304
|
});
|
305
305
|
|
306
|
-
var json = FactoryGuy.build('special_project');
|
306
|
+
var json = FactoryGuy.build('special_project');
|
307
307
|
json.title // => 'Project #1'
|
308
308
|
|
309
309
|
var project = FactoryGuy.make('special_project');
|
@@ -313,15 +313,15 @@ You can override the default attributes by passing in a hash
|
|
313
313
|
|
314
314
|
### Inline Functions
|
315
315
|
|
316
|
-
- Declare a function for an attribute
|
316
|
+
- Declare a function for an attribute
|
317
317
|
- Can reference other attributes
|
318
318
|
|
319
|
-
```javascript
|
320
|
-
|
319
|
+
```javascript
|
320
|
+
|
321
321
|
FactoryGuy.define('user', {
|
322
322
|
// Assume that this definition includes the same sequences and default section
|
323
|
-
// from the user definition in: "Declaring sequences in sequences hash" section.
|
324
|
-
|
323
|
+
// from the user definition in: "Declaring sequences in sequences hash" section.
|
324
|
+
|
325
325
|
funny_user: {
|
326
326
|
style: function(f) { return 'funny ' + f.name }
|
327
327
|
}
|
@@ -330,7 +330,7 @@ You can override the default attributes by passing in a hash
|
|
330
330
|
var json = FactoryGuy.build('funny_user');
|
331
331
|
json.name // => 'User1'
|
332
332
|
json.style // => 'funny User1'
|
333
|
-
|
333
|
+
|
334
334
|
var user = FactoryGuy.make('funny_user');
|
335
335
|
user.get('name') // => 'User2'
|
336
336
|
user.get('style') // => 'funny User2'
|
@@ -343,10 +343,10 @@ You can override the default attributes by passing in a hash
|
|
343
343
|
|
344
344
|
### Traits
|
345
345
|
|
346
|
-
- For grouping attributes together
|
346
|
+
- For grouping attributes together
|
347
347
|
- Can use one or more traits in a row
|
348
348
|
- The last trait included overrides any values in traits before it
|
349
|
-
|
349
|
+
|
350
350
|
```javascript
|
351
351
|
|
352
352
|
FactoryGuy.define('user', {
|
@@ -355,8 +355,8 @@ You can override the default attributes by passing in a hash
|
|
355
355
|
friendly: { style: 'Friendly' }
|
356
356
|
}
|
357
357
|
});
|
358
|
-
|
359
|
-
var json = FactoryGuy.build('user', 'big', 'friendly');
|
358
|
+
|
359
|
+
var json = FactoryGuy.build('user', 'big', 'friendly');
|
360
360
|
json.name // => 'Big Guy'
|
361
361
|
json.style // => 'Friendly'
|
362
362
|
|
@@ -366,9 +366,9 @@ You can override the default attributes by passing in a hash
|
|
366
366
|
|
367
367
|
```
|
368
368
|
|
369
|
-
You can still pass in a hash of options when using traits. This hash of
|
370
|
-
attributes will override any trait attributes or default attributes
|
371
|
-
|
369
|
+
You can still pass in a hash of options when using traits. This hash of
|
370
|
+
attributes will override any trait attributes or default attributes
|
371
|
+
|
372
372
|
```javascript
|
373
373
|
|
374
374
|
var user = FactoryGuy.make('user', 'big', 'friendly', {name: 'Dave'});
|
@@ -387,7 +387,7 @@ attributes will override any trait attributes or default attributes
|
|
387
387
|
- The inverse association is being set up for you
|
388
388
|
|
389
389
|
##### Setup belongsTo associations in Factory Definition
|
390
|
-
|
390
|
+
|
391
391
|
```javascript
|
392
392
|
// Recall ( from above setup ) that there is a user belongsTo on the Project model
|
393
393
|
// Also, assume 'user' factory is same as from 'user' factory definition above in
|
@@ -407,8 +407,8 @@ attributes will override any trait attributes or default attributes
|
|
407
407
|
user: FactoryGuy.belongsTo('admin')
|
408
408
|
}
|
409
409
|
});
|
410
|
-
|
411
|
-
var json = FactoryGuy.build('project_with_user');
|
410
|
+
|
411
|
+
var json = FactoryGuy.build('project_with_user');
|
412
412
|
json.user // => {id:1, name: 'Dude', style: 'normal'}
|
413
413
|
|
414
414
|
var json = FactoryGuy.build('project_with_bob');
|
@@ -423,17 +423,17 @@ attributes will override any trait attributes or default attributes
|
|
423
423
|
*You could also accomplish the above with traits:*
|
424
424
|
|
425
425
|
```javascript
|
426
|
-
|
426
|
+
|
427
427
|
FactoryGuy.define('project', {
|
428
428
|
traits: {
|
429
429
|
with_user: { user: {} },
|
430
430
|
with_admin: { user: FactoryGuy.belongsTo('admin') }
|
431
431
|
}
|
432
432
|
});
|
433
|
-
|
433
|
+
|
434
434
|
var user = FactoryGuy.make('project', 'with_user');
|
435
435
|
project.get('user').toJSON({includeId: true}) // => {id:1, name: 'Dude', style: 'normal'}
|
436
|
-
|
436
|
+
|
437
437
|
```
|
438
438
|
|
439
439
|
|
@@ -442,12 +442,12 @@ attributes will override any trait attributes or default attributes
|
|
442
442
|
```javascript
|
443
443
|
var user = FactoryGuy.make('user');
|
444
444
|
var project = FactoryGuy.make('project', {user: user});
|
445
|
-
|
445
|
+
|
446
446
|
project.get('user').toJSON({includeId: true}) // => {id:1, name: 'Dude', style: 'normal'}
|
447
447
|
```
|
448
448
|
|
449
449
|
*Note that though you are setting the 'user' belongsTo association on a project,
|
450
|
-
the reverse user hasMany 'projects' association is being setup for you on the user
|
450
|
+
the reverse user hasMany 'projects' association is being setup for you on the user
|
451
451
|
( for both manual and factory defined belongsTo associations ) as well*
|
452
452
|
|
453
453
|
```javascript
|
@@ -462,16 +462,16 @@ the reverse user hasMany 'projects' association is being setup for you on the us
|
|
462
462
|
FactoryGuy.define('user', {
|
463
463
|
user_with_projects: { FactoryGuy.hasMany('project', 2) }
|
464
464
|
});
|
465
|
-
|
465
|
+
|
466
466
|
var user = FactoryGuy.make('user_with_projects');
|
467
467
|
user.get('projects.length') // => 2
|
468
|
-
|
468
|
+
|
469
469
|
```
|
470
470
|
|
471
471
|
*You could also accomplish the above with traits:*
|
472
472
|
|
473
473
|
```javascript
|
474
|
-
|
474
|
+
|
475
475
|
FactoryGuy.define('project', {
|
476
476
|
traits: {
|
477
477
|
with_projects: {
|
@@ -479,10 +479,10 @@ the reverse user hasMany 'projects' association is being setup for you on the us
|
|
479
479
|
}
|
480
480
|
}
|
481
481
|
});
|
482
|
-
|
482
|
+
|
483
483
|
var user = FactoryGuy.make('user', 'with_projects');
|
484
484
|
user.get('projects.length') // => 2
|
485
|
-
|
485
|
+
|
486
486
|
```
|
487
487
|
|
488
488
|
##### Setup hasMany associations manually
|
@@ -492,18 +492,18 @@ the reverse user hasMany 'projects' association is being setup for you on the us
|
|
492
492
|
var project2 = FactoryGuy.make('project');
|
493
493
|
var user = FactoryGuy.make('user', {projects: [project1,project2]});
|
494
494
|
user.get('projects.length') // => 2
|
495
|
-
|
496
|
-
// or
|
495
|
+
|
496
|
+
// or
|
497
497
|
var projects = FactoryGuy.makeList('project', 2);
|
498
498
|
var user = FactoryGuy.make('user', {projects: projects});
|
499
499
|
user.get('projects.length') // => 2
|
500
|
-
|
500
|
+
|
501
501
|
```
|
502
502
|
|
503
503
|
*Note that though you are setting the 'projects' hasMany association on a user,
|
504
|
-
the reverse 'user' belongsTo association is being setup for you on the project
|
504
|
+
the reverse 'user' belongsTo association is being setup for you on the project
|
505
505
|
( for both manual and factory defined hasMany associations ) as well*
|
506
|
-
|
506
|
+
|
507
507
|
```javascript
|
508
508
|
projects.get('firstObject.user') // => user
|
509
509
|
```
|
@@ -517,11 +517,11 @@ the reverse 'user' belongsTo association is being setup for you on the project
|
|
517
517
|
- Loads one or more instances into store
|
518
518
|
|
519
519
|
|
520
|
-
##### Building json array
|
520
|
+
##### Building json array
|
521
521
|
|
522
522
|
```javascript
|
523
|
-
var json = FactoryGuy.buildList('user', 2)
|
524
|
-
json.length // => 2
|
523
|
+
var json = FactoryGuy.buildList('user', 2)
|
524
|
+
json.length // => 2
|
525
525
|
json[0] // => {id: 1, name: 'User1', style: 'normal'}
|
526
526
|
json[1] // => {id: 2, name: 'User2', style: 'normal'}
|
527
527
|
|
@@ -531,14 +531,14 @@ the reverse 'user' belongsTo association is being setup for you on the project
|
|
531
531
|
|
532
532
|
```javascript
|
533
533
|
var users = FactoryGuy.makeList('user', 2)
|
534
|
-
users.get('length') // => 2
|
534
|
+
users.get('length') // => 2
|
535
535
|
users[0].toJSON({includeId: true}) // => {id: 3, name: 'User3', style: 'normal'}
|
536
536
|
users[1].toJSON({includeId: true}) // => {id: 4, name: 'User4', style: 'normal'}
|
537
537
|
|
538
538
|
```
|
539
539
|
|
540
540
|
|
541
|
-
### Testing models, controllers, views
|
541
|
+
### Testing models, controllers, views
|
542
542
|
|
543
543
|
- Testing the models, controllers and views in isolation
|
544
544
|
- Use FactoryGuyTestMixin to help with testing
|
@@ -549,7 +549,7 @@ the reverse 'user' belongsTo association is being setup for you on the project
|
|
549
549
|
- Using FactoryGuyTestMixin helper methods:
|
550
550
|
- make
|
551
551
|
- teardown
|
552
|
-
|
552
|
+
|
553
553
|
```javascript
|
554
554
|
|
555
555
|
// Create a helper class using FactoryGuyTestMixin.
|
@@ -594,11 +594,12 @@ test("make a user using your applications default adapter", function() {
|
|
594
594
|
|
595
595
|
- Uses mockjax
|
596
596
|
- Has helper methods
|
597
|
-
- handleFindMany
|
597
|
+
- handleFindMany
|
598
|
+
- handleFindOne
|
598
599
|
- handleFindQuery
|
599
600
|
- handleCreate
|
600
|
-
- handleUpdate
|
601
|
-
- handleDelete
|
601
|
+
- handleUpdate
|
602
|
+
- handleDelete
|
602
603
|
|
603
604
|
Since it is recommended to use your normal adapter ( which is usually a subclass of RESTAdapter, )
|
604
605
|
FactoryGuyTestMixin assumes you will want to use that adapter to do your integration tests.
|
@@ -624,31 +625,29 @@ tests run as shown in the previous section (Using FactoryGuyTestMixin)**
|
|
624
625
|
```javascript
|
625
626
|
// can use traits and extra fixture options here as you would with FactoryGuy#makeList
|
626
627
|
testHelper.handleFindMany('profile', 2);
|
627
|
-
|
628
|
+
|
628
629
|
store.find('profile').then(function (profiles) {
|
629
630
|
profiles.get('length') //=> 2
|
630
631
|
});
|
631
632
|
```
|
632
633
|
|
634
|
+
##### handleFindOne
|
635
|
+
- for dealing with finding one record with an id
|
633
636
|
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
+
```javascript
|
638
|
+
// can use traits and extra fixture options here as you would with FactoryGuy#make
|
639
|
+
testHelper.handleFindOne('profile', {id: 1});
|
637
640
|
|
638
|
-
|
641
|
+
store.find('profile', 1).then(function (profile) {
|
642
|
+
profile.get('id') //=> 1
|
643
|
+
});
|
644
|
+
```
|
639
645
|
|
640
|
-
```javascript
|
641
|
-
// First build json for the instances you want 'returned' in your query.
|
642
|
-
var usersJson = FactoryGuy.buildList('user', 2);
|
643
646
|
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
+
##### handleFindQuery
|
648
|
+
- for dealing with finding all records for a type of model with query parameters.
|
649
|
+
- can pass in model instances or empty array
|
647
650
|
|
648
|
-
store.findQuery('user', {name:'Bob', age: 10}}).then(function(userInstances){
|
649
|
-
// userInstances returned are created from the usersJson that you passed in
|
650
|
-
})
|
651
|
-
```
|
652
651
|
|
653
652
|
*Passing in array of model instances*
|
654
653
|
|
@@ -683,19 +682,19 @@ tests run as shown in the previous section (Using FactoryGuyTestMixin)**
|
|
683
682
|
- succeed - flag to indicate if the request should succeed ( default is true )
|
684
683
|
|
685
684
|
**Note**
|
686
|
-
|
685
|
+
|
687
686
|
*Any attributes in match will be added to the response json automatically,
|
688
687
|
so you don't need to include them in the returns hash as well.*
|
689
|
-
|
688
|
+
|
690
689
|
*If you match on a belongsTo association, you don't have to include that in the
|
691
690
|
returns hash.*
|
692
|
-
|
693
|
-
|
691
|
+
|
692
|
+
|
694
693
|
Realistically, you will have code in a view action or controller action that will
|
695
|
-
create the record, and setup any associations.
|
696
|
-
|
694
|
+
create the record, and setup any associations.
|
695
|
+
|
697
696
|
```javascript
|
698
|
-
|
697
|
+
|
699
698
|
// most actions that create a record look something like this:
|
700
699
|
action: {
|
701
700
|
addProject: function (user) {
|
@@ -710,10 +709,10 @@ Realistically, you will have code in a view action or controller action that wil
|
|
710
709
|
In this case, you are are creating a 'project' record with a specific name, and belonging
|
711
710
|
to a particular user. To mock this createRecord call here are a few ways to do this using
|
712
711
|
match and or returns options.
|
713
|
-
|
712
|
+
|
714
713
|
```javascript
|
715
|
-
// Simplest case
|
716
|
-
// Don't care about a match just handle createRecord for any project
|
714
|
+
// Simplest case
|
715
|
+
// Don't care about a match just handle createRecord for any project
|
717
716
|
testHelper.handleCreate('project')
|
718
717
|
// Exactly matching attributes
|
719
718
|
testHelper.handleCreate('project', {match: {name: "Moo", user: user}})
|
@@ -727,17 +726,17 @@ match and or returns options.
|
|
727
726
|
*mocking a failed create*
|
728
727
|
|
729
728
|
```javascript
|
730
|
-
// set the succeed flag to 'false'
|
729
|
+
// set the succeed flag to 'false'
|
731
730
|
testHelper.handleCreate('project', {succeed: false});
|
732
|
-
|
731
|
+
|
733
732
|
// when the createRecord on the 'project' is called, it will fail
|
734
733
|
store.createRecord('project').save() //=> fails
|
735
734
|
|
736
|
-
// or fail only if the attribues match exactly
|
735
|
+
// or fail only if the attribues match exactly
|
737
736
|
testHelper.handleCreate('project', {
|
738
737
|
match: {name: "Moo", user: user}, {succeed: false}
|
739
738
|
})
|
740
|
-
|
739
|
+
|
741
740
|
store.createRecord('project', {name: "Moo", user: user}).save() //=> fails
|
742
741
|
```
|
743
742
|
|
@@ -864,26 +863,24 @@ test("Creates new project", function() {
|
|
864
863
|
|
865
864
|
### Using DS.Fixture adapter
|
866
865
|
|
867
|
-
- Not recommended
|
868
866
|
- FactoryGuy.make ... creates model in the store and returns json
|
869
867
|
|
870
868
|
Technically when you call FactoryGuy.make with a store using the DS.FixtureAdapter,
|
871
|
-
the fixture is
|
872
|
-
to the store because when you call store.find to get that record, the adapter looks
|
873
|
-
in that FIXTURE array to find it and then puts it in the store.
|
869
|
+
the fixture is added to the model's FIXTURE array and also pushed into the store. Normally the store is populated by the DS.FixtureAdapter when you call store.find.
|
874
870
|
|
875
|
-
```javascript
|
876
871
|
|
872
|
+
```javascript
|
877
873
|
|
878
874
|
FactoryGuy.make('user'); // user.FIXTURES = [{id: 1, name: 'User1', style: 'normal'}]
|
879
875
|
FactoryGuy.make('user', {name: 'bob'}); // user.FIXTURES = [{id: 2, name: 'bob', style: 'normal'}]
|
880
876
|
FactoryGuy.make('admin'); // user.FIXTURES = [{id: 3, name: 'Admin', style: 'super'}]
|
881
877
|
FactoryGuy.make('admin', {name: 'Fred'}); // user.FIXTURES = [{id: 4, name: 'Fred', style: 'super'}]
|
882
878
|
|
879
|
+
// This works because we push the models into the store when called FactoryGuy.make
|
880
|
+
var userJson = FactoryGuy.make('user'); // user.FIXTURES = [{id: 1, name: 'User1', style: 'normal'}]
|
881
|
+
store.all('user') // returns the user equal to userJson
|
883
882
|
|
884
|
-
// Use store.find to get the model instance
|
885
|
-
// you use the ActiveModelAdapter or RESTAdapter the record is returned so you don't
|
886
|
-
// have to then go and find it )
|
883
|
+
// Use store.find to get the model instance
|
887
884
|
var userJson = FactoryGuy.make('user');
|
888
885
|
store.find('user', userJson.id).then(function(user) {
|
889
886
|
user.toJSON({includeId: true}) ( pretty much equals ) userJson;
|
@@ -896,8 +893,15 @@ var userJson = FactoryGuy.make('user', {projects: [projectJson.id]});
|
|
896
893
|
var userJson = FactoryGuy.make('user');
|
897
894
|
var projectJson = FactoryGuy.make('project', {user: userJson.id});
|
898
895
|
|
899
|
-
//
|
900
|
-
|
896
|
+
// Associations are pushed on as well
|
897
|
+
store.find('user', 1).then(function (user) {
|
898
|
+
var projects = user.get('projects');
|
899
|
+
projects.get('length'); //1
|
900
|
+
projects.get('firstObject').toJSON({includeId: true}); //equal to projectJSON
|
901
|
+
});
|
902
|
+
|
903
|
+
// If you are using factory_guy_has_many.js fix, will give you the same result,
|
904
|
+
// but all associations are treated as async, so it's
|
901
905
|
// a bit clunky to get this associated data. When using DS.FixtureAdapter
|
902
906
|
// in view specs though, this clunk is dealt with for you. But remember,
|
903
907
|
// you DON'T have to use the Fixture adapter.
|
data/bower.json
CHANGED
data/dist/amd/factory-guy.js
CHANGED
@@ -601,7 +601,7 @@ var FactoryGuy = {
|
|
601
601
|
* Most of the work of making the model from the json fixture is going on here.
|
602
602
|
* @param modelType
|
603
603
|
* @param fixture
|
604
|
-
* @returns {
|
604
|
+
* @returns {DS.Model} instance of DS.Model
|
605
605
|
*/
|
606
606
|
makeModel: function (modelType, fixture) {
|
607
607
|
var store = this,
|
@@ -957,10 +957,42 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
957
957
|
var url = this.buildURL(modelName);
|
958
958
|
this.stubEndpointForHttpRequest(url, responseJson);
|
959
959
|
},
|
960
|
+
|
961
|
+
/**
|
962
|
+
Handling ajax GET for finding one record for a type of model and an id.
|
963
|
+
You can mock failed find by passing in success argument as false.
|
964
|
+
|
965
|
+
```js
|
966
|
+
// Pass in the parameters you would normally pass into FactoryGuy.make,
|
967
|
+
// like fixture name, number of fixtures to make, and optional traits,
|
968
|
+
// or fixture options
|
969
|
+
testHelper.handleFindOne('user', 'with_hats', {id: 1});
|
970
|
+
|
971
|
+
store.find('user', 1).then(function(user){
|
972
|
+
|
973
|
+
});
|
974
|
+
```
|
975
|
+
|
976
|
+
@param {String} name name of the fixture ( or model ) to find
|
977
|
+
@param {String} trait optional traits (one or more)
|
978
|
+
@param {Object} opts optional fixture options (including id)
|
979
|
+
*/
|
980
|
+
handleFindOne: function () {
|
981
|
+
// make the records and load them in the store
|
982
|
+
var record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
983
|
+
var name = arguments[0];
|
984
|
+
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
985
|
+
var responseJson = {};
|
986
|
+
var json = record.toJSON({includeId: true});
|
987
|
+
responseJson[modelName.pluralize()] = json;
|
988
|
+
var url = this.buildURL(modelName, record.id);
|
989
|
+
this.stubEndpointForHttpRequest(url, responseJson);
|
990
|
+
},
|
991
|
+
|
960
992
|
/**
|
961
993
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
962
994
|
|
963
|
-
|
995
|
+
|
964
996
|
```js
|
965
997
|
|
966
998
|
// Create model instances
|
@@ -974,9 +1006,11 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
974
1006
|
})
|
975
1007
|
```
|
976
1008
|
|
977
|
-
|
1009
|
+
By omitting the last argument (pass in no records), this simulates a findQuery
|
1010
|
+
request that returns no records
|
1011
|
+
|
978
1012
|
```js
|
979
|
-
//
|
1013
|
+
// Simulate a query that returns no results
|
980
1014
|
testHelper.handleFindQuery('user', ['age']);
|
981
1015
|
|
982
1016
|
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
@@ -952,10 +952,42 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
952
952
|
var url = this.buildURL(modelName);
|
953
953
|
this.stubEndpointForHttpRequest(url, responseJson);
|
954
954
|
},
|
955
|
+
|
956
|
+
/**
|
957
|
+
Handling ajax GET for finding one record for a type of model and an id.
|
958
|
+
You can mock failed find by passing in success argument as false.
|
959
|
+
|
960
|
+
```js
|
961
|
+
// Pass in the parameters you would normally pass into FactoryGuy.make,
|
962
|
+
// like fixture name, number of fixtures to make, and optional traits,
|
963
|
+
// or fixture options
|
964
|
+
testHelper.handleFindOne('user', 'with_hats', {id: 1});
|
965
|
+
|
966
|
+
store.find('user', 1).then(function(user){
|
967
|
+
|
968
|
+
});
|
969
|
+
```
|
970
|
+
|
971
|
+
@param {String} name name of the fixture ( or model ) to find
|
972
|
+
@param {String} trait optional traits (one or more)
|
973
|
+
@param {Object} opts optional fixture options (including id)
|
974
|
+
*/
|
975
|
+
handleFindOne: function () {
|
976
|
+
// make the records and load them in the store
|
977
|
+
var record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
978
|
+
var name = arguments[0];
|
979
|
+
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
980
|
+
var responseJson = {};
|
981
|
+
var json = record.toJSON({includeId: true});
|
982
|
+
responseJson[modelName.pluralize()] = json;
|
983
|
+
var url = this.buildURL(modelName, record.id);
|
984
|
+
this.stubEndpointForHttpRequest(url, responseJson);
|
985
|
+
},
|
986
|
+
|
955
987
|
/**
|
956
988
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
957
989
|
|
958
|
-
|
990
|
+
|
959
991
|
```js
|
960
992
|
|
961
993
|
// Create model instances
|
@@ -969,9 +1001,11 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
969
1001
|
})
|
970
1002
|
```
|
971
1003
|
|
972
|
-
|
1004
|
+
By omitting the last argument (pass in no records), this simulates a findQuery
|
1005
|
+
request that returns no records
|
1006
|
+
|
973
1007
|
```js
|
974
|
-
//
|
1008
|
+
// Simulate a query that returns no results
|
975
1009
|
testHelper.handleFindQuery('user', ['age']);
|
976
1010
|
|
977
1011
|
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
@@ -1 +1 @@
|
|
1
|
-
var Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};var MissingSequenceError=function(message){this.toString=function(){return message}};if(FactoryGuy!==undefined){FactoryGuy.sequence=Sequence;FactoryGuy.missingSequenceError=MissingSequenceError}var ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;var sequenceName=null;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.nextId=function(){return modelId++};this.generate=function(name,sequenceFn){if(sequenceFn){if(!sequences[name]){sequences[name]=new Sequence(sequenceFn)}}var sequence=sequences[name];if(!sequence){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequence.next()};this.build=function(name,opts,traitArgs){var traitsObj={};traitArgs.forEach(function(trait){$.extend(traitsObj,traits[trait])});var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,traitsObj,opts);for(var attribute in fixture){if(Ember.typeOf(fixture[attribute])=="function"){fixture[attribute]=fixture[attribute].call(this,fixture)}else if(Ember.typeOf(fixture[attribute])=="object"){if(FactoryGuy.getStore()){if(FactoryGuy.isAttributeRelationship(this.model,attribute)){fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}else{fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}}if(!fixture.id){fixture.id=this.nextId()}return fixture};this.buildList=function(name,number,traits,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts,traits))}return arr};this.reset=function(){modelId=1;for(var name in sequences){sequences[name].reset()}};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseTraits=function(object){if(!object){return}traits=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(Ember.typeOf(sequenceFn)!="function"){throw new Error("Problem with ["+sequenceName+"] sequence definition. Sequences must be functions")}object[sequenceName]=new Sequence(sequenceFn)}sequences=object};var parseConfig=function(config){parseSequences(config.sequences);delete config.sequences;parseTraits(config.traits);delete config.traits;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};if(FactoryGuy!==undefined){FactoryGuy.modelDefiniton=ModelDefinition}var FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},setStore:function(store){Ember.assert("FactoryGuy#setStore needs a valid store instance.You passed in ["+store+"]",store instanceof DS.Store);this.store=store},getStore:function(){return this.store},isAttributeRelationship:function(typeName,attribute){if(!this.store){Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures");return true}var model=this.store.modelFor(typeName);return!!model.typeForRelationship(attribute)},generate:function(nameOrFunction){var sortaRandomName=Math.floor((1+Math.random())*65536).toString(16)+Date.now();return function(){if(Em.typeOf(nameOrFunction)=="function"){return this.generate(sortaRandomName,nameOrFunction)}else{return this.generate(nameOrFunction)}}},belongsTo:function(fixtureName,opts){return function(){return FactoryGuy.build(fixtureName,opts)}},association:function(fixtureName,opts){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.belongsTo instead");return this.belongsTo(fixtureName,opts)},hasMany:function(fixtureName,number,opts){return function(){return FactoryGuy.buildList(fixtureName,number,opts)}},lookupModelForFixtureName:function(name){var definition=this.lookupDefinitionForFixtureName(name);if(definition){return definition.model}},lookupDefinitionForFixtureName:function(name){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(){var args=Array.prototype.slice.call(arguments);var opts={};var name=args.shift();if(!name){throw new Error("Build needs a factory name to build")}if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts,traits)},buildList:function(){var args=Array.prototype.slice.call(arguments);var name=args.shift();var number=args.shift();if(!name||!number){throw new Error("buildList needs a name and a number ( at least ) to build with")}var opts={};if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,traits,opts)},make:function(){var store=this.store;Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",store);var fixture=this.build.apply(this,arguments);var name=arguments[0];var modelName=this.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(store.usingFixtureAdapter()){store.setAssociationsForFixtureAdapter(modelType,modelName,fixture);fixture=FactoryGuy.pushFixture(modelType,fixture);store.loadModelForFixtureAdapter(modelType,fixture);return fixture}else{return store.makeModel(modelType,fixture)}},makeList:function(){Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",this.store);var arr=[];var args=Array.prototype.slice.call(arguments);Ember.assert("makeList needs at least 2 arguments, a name and a number",args.length>=2);var number=args.splice(1,1)[0];Ember.assert("Second argument to makeList should be a number (of fixtures to make.)",typeof number=="number");for(var i=0;i<number;i++){arr.push(this.make.apply(this,args))}return arr},resetModels:function(store){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];definition.reset();try{var modelType=store.modelFor(definition.model);if(store.usingFixtureAdapter()){modelType.FIXTURES=[]}store.unloadAll(modelType)}catch(e){console.log("resetModels",e)}}},pushFixture:function(modelClass,fixture){var index;if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}index=this.indexOfFixture(modelClass.FIXTURES,fixture);if(index>-1){modelClass.FIXTURES.splice(index,1)}modelClass.FIXTURES.push(fixture);return fixture},indexOfFixture:function(fixtures,fixture){var index=-1,id=fixture.id+"";Ember.A(fixtures).find(function(r,i){if(""+Ember.get(r,"id")===id){index=i;return true}else{return false}});return index},clear:function(opts){if(!opts){this.modelDefinitions={}}}};(function(){DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.make instead");FactoryGuy.make.call(FactoryGuy,arguments)},makeList:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.makeList instead");FactoryGuy.makeList.call(FactoryGuy,arguments)},makeModel:function(modelType,fixture){var store=this,modelName=store.modelFor(modelType).typeKey,model;Em.run(function(){store.findEmbeddedAssociationsForRESTAdapter(modelType,fixture);if(fixture.type){modelName=fixture.type.underscore();modelType=store.modelFor(modelName)}model=store.push(modelName,fixture);store.setAssociationsForRESTAdapter(modelType,modelName,model)});return model},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){var hasManyRelation,belongsToRecord;if(relationship.kind=="hasMany"){hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){FactoryGuy.pushFixture(relationship.type,object);id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var hasManyFixture=adapter.findFixtureById(hasManyfixtures,id);hasManyFixture[modelName]=fixture.id;self.loadModelForFixtureAdapter(relationship.type,hasManyFixture)})}}if(relationship.kind=="belongsTo"){belongsToRecord=fixture[relationship.key];if(belongsToRecord){if(typeof belongsToRecord=="object"){FactoryGuy.pushFixture(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord.id}var hasManyName=self.findHasManyRelationshipNameForFixtureAdapter(relationship.type,relationship.parentType);var belongsToFixtures=adapter.fixturesForType(relationship.type);var belongsTofixture=adapter.findFixtureById(belongsToFixtures,fixture[relationship.key]);if(!belongsTofixture[hasManyName]){belongsTofixture[hasManyName]=[]}belongsTofixture[hasManyName].push(fixture.id);self.loadModelForFixtureAdapter(relationship.type,belongsTofixture)}}})},loadModelForFixtureAdapter:function(modelType,fixture){var storeModel=this.getById(modelType,fixture.id),that=this;if(!Ember.isPresent(storeModel)||storeModel.get("isEmpty")){Ember.run(function(){var dup=Ember.copy(fixture,true);that.push(modelType,fixture);Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(fixture[relationship.key]){fixture[relationship.key]=dup[relationship.key]}})})}},findEmbeddedAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(Ember.typeOf(belongsToRecord)=="object"){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,belongsToRecord);belongsToRecord=store.push(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord}}if(relationship.kind=="hasMany"){var hasManyRecords=fixture[relationship.key];if(Ember.typeOf(hasManyRecords)=="array"){if(Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.map(function(object){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,object);var record=store.push(relationship.type,object);records.push(record);return record});fixture[relationship.key]=records}}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var children=model.get(name)||[];children.forEach(function(child){var belongsToName=self.findRelationshipName("belongsTo",child.constructor,model);if(belongsToName){child.set(belongsToName,model)}})}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind==kind&&childModel instanceof relationship.type){relationshipName=relationship.key}});return relationshipName},findHasManyRelationshipNameForFixtureAdapter:function(belongToModelType,childModelType){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"&&childModelType==relationship.type){relationshipName=relationship.key}});return relationshipName},pushPayload:function(type,payload){var typeName,model;if(this.usingFixtureAdapter()){if(Ember.typeOf(type)==="string"&&Ember.isPresent(payload)&&Ember.isPresent(payload.id)){model=this.modelFor(type);FactoryGuy.pushFixture(model,payload);this.push(model,Ember.copy(payload,true))}else if(Ember.typeOf(type)==="object"||Ember.typeOf(payload)==="object"){if(Ember.isBlank(payload)){payload=type}for(var prop in payload){typeName=Ember.String.camelize(Ember.String.singularize(prop));model=this.modelFor(typeName);this.pushMany(model,Ember.makeArray(Ember.copy(payload[prop],true)));Ember.ArrayPolyfills.forEach.call(Ember.makeArray(payload[prop]),function(hash){FactoryGuy.pushFixture(model,hash)},this)}}else{throw new Ember.Error("Assertion Failed: You cannot use `store#pushPayload` with this method signature pushPayload("+type+","+payload+")")}}else{this._super(type,payload)}}})})();var FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);FactoryGuy.setStore(this.getStore());return this},useFixtureAdapter:function(app){app.ApplicationAdapter=DS.FixtureAdapter;this.getStore().adapterFor("application").simulateRemoteResponse=false},usingActiveModelSerializer:function(type){var store=this.getStore();var modelType=store.modelFor(type);var serializer=store.serializerFor(modelType.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(){return FactoryGuy.make.apply(FactoryGuy,arguments)},getStore:function(){return this.get("container").lookup("store:main")},stubEndpointForHttpRequest:function(url,json,options){options=options||{};var request={url:url,dataType:"json",responseText:json,type:options.type||"GET",status:options.status||200};if(options.urlParams){request.urlParams=options.urlParams}if(options.data){request.data=options.data}$.mockjax(request)},buildURL:function(typeName,id){var type=this.getStore().modelFor(typeName);return this.getStore().adapterFor(type).buildURL(type.typeKey,id)},handleFindMany:function(){var records=FactoryGuy.makeList.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};var json=records.map(function(record){return record.toJSON({includeId:true})});responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson)},handleFindQuery:function(modelName,searchParams,records){Ember.assert("The second argument of searchParams must be an array",Em.typeOf(searchParams)=="array");if(records){Ember.assert("The third argument ( records ) must be an array - found type:"+Em.typeOf(records),Em.typeOf(records)=="array")}else{records=[]}var json=records.map(function(record){return record.toJSON({includeId:true})});var responseJson={};responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{urlParams:searchParams})},handleCreate:function(modelName,options){var opts=options===undefined?{}:options;var succeed=opts.succeed===undefined?true:opts.succeed;var match=opts.match||{};var returnArgs=opts.returns||{};var url=this.buildURL(modelName);var definition=FactoryGuy.modelDefinitions[modelName];var httpOptions={type:"POST"};if(opts.match){var expectedRequest={};var record=this.getStore().createRecord(modelName,match);expectedRequest[modelName]=record.serialize();httpOptions.data=JSON.stringify(expectedRequest)}var modelType=this.getStore().modelFor(modelName);var responseJson={};if(succeed){responseJson[modelName]=$.extend({id:definition.nextId()},match,returnArgs);Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}else{httpOptions.status=500}this.stubEndpointForHttpRequest(url,responseJson,httpOptions)},handleUpdate:function(){var args=Array.prototype.slice.call(arguments);Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var succeed=true;if(typeof args[args.length-1]=="boolean"){args.pop();succeed=false}Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var type,id;if(args[0]instanceof DS.Model){var model=args[0];type=model.constructor.typeKey;id=model.id}else if(typeof args[0]=="string"&&typeof parseInt(args[1])=="number"){type=args[0];id=args[1]}Ember.assert("To handleUpdate pass in a model instance or a type and an id",type&&id);this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:succeed?200:500})},handleDelete:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:succeed?200:500})},teardown:function(){FactoryGuy.resetModels(this.getStore());$.mockjax.clear()}});if(FactoryGuy!==undefined){FactoryGuy.testMixin=FactoryGuyTestMixin}
|
1
|
+
var Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};var MissingSequenceError=function(message){this.toString=function(){return message}};if(FactoryGuy!==undefined){FactoryGuy.sequence=Sequence;FactoryGuy.missingSequenceError=MissingSequenceError}var ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;var sequenceName=null;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.nextId=function(){return modelId++};this.generate=function(name,sequenceFn){if(sequenceFn){if(!sequences[name]){sequences[name]=new Sequence(sequenceFn)}}var sequence=sequences[name];if(!sequence){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequence.next()};this.build=function(name,opts,traitArgs){var traitsObj={};traitArgs.forEach(function(trait){$.extend(traitsObj,traits[trait])});var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,traitsObj,opts);for(var attribute in fixture){if(Ember.typeOf(fixture[attribute])=="function"){fixture[attribute]=fixture[attribute].call(this,fixture)}else if(Ember.typeOf(fixture[attribute])=="object"){if(FactoryGuy.getStore()){if(FactoryGuy.isAttributeRelationship(this.model,attribute)){fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}else{fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}}if(!fixture.id){fixture.id=this.nextId()}return fixture};this.buildList=function(name,number,traits,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts,traits))}return arr};this.reset=function(){modelId=1;for(var name in sequences){sequences[name].reset()}};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseTraits=function(object){if(!object){return}traits=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(Ember.typeOf(sequenceFn)!="function"){throw new Error("Problem with ["+sequenceName+"] sequence definition. Sequences must be functions")}object[sequenceName]=new Sequence(sequenceFn)}sequences=object};var parseConfig=function(config){parseSequences(config.sequences);delete config.sequences;parseTraits(config.traits);delete config.traits;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};if(FactoryGuy!==undefined){FactoryGuy.modelDefiniton=ModelDefinition}var FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},setStore:function(store){Ember.assert("FactoryGuy#setStore needs a valid store instance.You passed in ["+store+"]",store instanceof DS.Store);this.store=store},getStore:function(){return this.store},isAttributeRelationship:function(typeName,attribute){if(!this.store){Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures");return true}var model=this.store.modelFor(typeName);return!!model.typeForRelationship(attribute)},generate:function(nameOrFunction){var sortaRandomName=Math.floor((1+Math.random())*65536).toString(16)+Date.now();return function(){if(Em.typeOf(nameOrFunction)=="function"){return this.generate(sortaRandomName,nameOrFunction)}else{return this.generate(nameOrFunction)}}},belongsTo:function(fixtureName,opts){return function(){return FactoryGuy.build(fixtureName,opts)}},association:function(fixtureName,opts){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.belongsTo instead");return this.belongsTo(fixtureName,opts)},hasMany:function(fixtureName,number,opts){return function(){return FactoryGuy.buildList(fixtureName,number,opts)}},lookupModelForFixtureName:function(name){var definition=this.lookupDefinitionForFixtureName(name);if(definition){return definition.model}},lookupDefinitionForFixtureName:function(name){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(){var args=Array.prototype.slice.call(arguments);var opts={};var name=args.shift();if(!name){throw new Error("Build needs a factory name to build")}if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts,traits)},buildList:function(){var args=Array.prototype.slice.call(arguments);var name=args.shift();var number=args.shift();if(!name||!number){throw new Error("buildList needs a name and a number ( at least ) to build with")}var opts={};if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,traits,opts)},make:function(){var store=this.store;Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",store);var fixture=this.build.apply(this,arguments);var name=arguments[0];var modelName=this.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(store.usingFixtureAdapter()){store.setAssociationsForFixtureAdapter(modelType,modelName,fixture);fixture=FactoryGuy.pushFixture(modelType,fixture);store.loadModelForFixtureAdapter(modelType,fixture);return fixture}else{return store.makeModel(modelType,fixture)}},makeList:function(){Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",this.store);var arr=[];var args=Array.prototype.slice.call(arguments);Ember.assert("makeList needs at least 2 arguments, a name and a number",args.length>=2);var number=args.splice(1,1)[0];Ember.assert("Second argument to makeList should be a number (of fixtures to make.)",typeof number=="number");for(var i=0;i<number;i++){arr.push(this.make.apply(this,args))}return arr},resetModels:function(store){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];definition.reset();try{var modelType=store.modelFor(definition.model);if(store.usingFixtureAdapter()){modelType.FIXTURES=[]}store.unloadAll(modelType)}catch(e){console.log("resetModels",e)}}},pushFixture:function(modelClass,fixture){var index;if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}index=this.indexOfFixture(modelClass.FIXTURES,fixture);if(index>-1){modelClass.FIXTURES.splice(index,1)}modelClass.FIXTURES.push(fixture);return fixture},indexOfFixture:function(fixtures,fixture){var index=-1,id=fixture.id+"";Ember.A(fixtures).find(function(r,i){if(""+Ember.get(r,"id")===id){index=i;return true}else{return false}});return index},clear:function(opts){if(!opts){this.modelDefinitions={}}}};(function(){DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.make instead");FactoryGuy.make.call(FactoryGuy,arguments)},makeList:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.makeList instead");FactoryGuy.makeList.call(FactoryGuy,arguments)},makeModel:function(modelType,fixture){var store=this,modelName=store.modelFor(modelType).typeKey,model;Em.run(function(){store.findEmbeddedAssociationsForRESTAdapter(modelType,fixture);if(fixture.type){modelName=fixture.type.underscore();modelType=store.modelFor(modelName)}model=store.push(modelName,fixture);store.setAssociationsForRESTAdapter(modelType,modelName,model)});return model},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){var hasManyRelation,belongsToRecord;if(relationship.kind=="hasMany"){hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){FactoryGuy.pushFixture(relationship.type,object);id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var hasManyFixture=adapter.findFixtureById(hasManyfixtures,id);hasManyFixture[modelName]=fixture.id;self.loadModelForFixtureAdapter(relationship.type,hasManyFixture)})}}if(relationship.kind=="belongsTo"){belongsToRecord=fixture[relationship.key];if(belongsToRecord){if(typeof belongsToRecord=="object"){FactoryGuy.pushFixture(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord.id}var hasManyName=self.findHasManyRelationshipNameForFixtureAdapter(relationship.type,relationship.parentType);var belongsToFixtures=adapter.fixturesForType(relationship.type);var belongsTofixture=adapter.findFixtureById(belongsToFixtures,fixture[relationship.key]);if(!belongsTofixture[hasManyName]){belongsTofixture[hasManyName]=[]}belongsTofixture[hasManyName].push(fixture.id);self.loadModelForFixtureAdapter(relationship.type,belongsTofixture)}}})},loadModelForFixtureAdapter:function(modelType,fixture){var storeModel=this.getById(modelType,fixture.id),that=this;if(!Ember.isPresent(storeModel)||storeModel.get("isEmpty")){Ember.run(function(){var dup=Ember.copy(fixture,true);that.push(modelType,fixture);Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(fixture[relationship.key]){fixture[relationship.key]=dup[relationship.key]}})})}},findEmbeddedAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(Ember.typeOf(belongsToRecord)=="object"){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,belongsToRecord);belongsToRecord=store.push(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord}}if(relationship.kind=="hasMany"){var hasManyRecords=fixture[relationship.key];if(Ember.typeOf(hasManyRecords)=="array"){if(Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.map(function(object){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,object);var record=store.push(relationship.type,object);records.push(record);return record});fixture[relationship.key]=records}}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var children=model.get(name)||[];children.forEach(function(child){var belongsToName=self.findRelationshipName("belongsTo",child.constructor,model);if(belongsToName){child.set(belongsToName,model)}})}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind==kind&&childModel instanceof relationship.type){relationshipName=relationship.key}});return relationshipName},findHasManyRelationshipNameForFixtureAdapter:function(belongToModelType,childModelType){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"&&childModelType==relationship.type){relationshipName=relationship.key}});return relationshipName},pushPayload:function(type,payload){var typeName,model;if(this.usingFixtureAdapter()){if(Ember.typeOf(type)==="string"&&Ember.isPresent(payload)&&Ember.isPresent(payload.id)){model=this.modelFor(type);FactoryGuy.pushFixture(model,payload);this.push(model,Ember.copy(payload,true))}else if(Ember.typeOf(type)==="object"||Ember.typeOf(payload)==="object"){if(Ember.isBlank(payload)){payload=type}for(var prop in payload){typeName=Ember.String.camelize(Ember.String.singularize(prop));model=this.modelFor(typeName);this.pushMany(model,Ember.makeArray(Ember.copy(payload[prop],true)));Ember.ArrayPolyfills.forEach.call(Ember.makeArray(payload[prop]),function(hash){FactoryGuy.pushFixture(model,hash)},this)}}else{throw new Ember.Error("Assertion Failed: You cannot use `store#pushPayload` with this method signature pushPayload("+type+","+payload+")")}}else{this._super(type,payload)}}})})();var FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);FactoryGuy.setStore(this.getStore());return this},useFixtureAdapter:function(app){app.ApplicationAdapter=DS.FixtureAdapter;this.getStore().adapterFor("application").simulateRemoteResponse=false},usingActiveModelSerializer:function(type){var store=this.getStore();var modelType=store.modelFor(type);var serializer=store.serializerFor(modelType.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(){return FactoryGuy.make.apply(FactoryGuy,arguments)},getStore:function(){return this.get("container").lookup("store:main")},stubEndpointForHttpRequest:function(url,json,options){options=options||{};var request={url:url,dataType:"json",responseText:json,type:options.type||"GET",status:options.status||200};if(options.urlParams){request.urlParams=options.urlParams}if(options.data){request.data=options.data}$.mockjax(request)},buildURL:function(typeName,id){var type=this.getStore().modelFor(typeName);return this.getStore().adapterFor(type).buildURL(type.typeKey,id)},handleFindMany:function(){var records=FactoryGuy.makeList.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};var json=records.map(function(record){return record.toJSON({includeId:true})});responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson)},handleFindOne:function(){var record=FactoryGuy.make.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};var json=record.toJSON({includeId:true});responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName,record.id);this.stubEndpointForHttpRequest(url,responseJson)},handleFindQuery:function(modelName,searchParams,records){Ember.assert("The second argument of searchParams must be an array",Em.typeOf(searchParams)=="array");if(records){Ember.assert("The third argument ( records ) must be an array - found type:"+Em.typeOf(records),Em.typeOf(records)=="array")}else{records=[]}var json=records.map(function(record){return record.toJSON({includeId:true})});var responseJson={};responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{urlParams:searchParams})},handleCreate:function(modelName,options){var opts=options===undefined?{}:options;var succeed=opts.succeed===undefined?true:opts.succeed;var match=opts.match||{};var returnArgs=opts.returns||{};var url=this.buildURL(modelName);var definition=FactoryGuy.modelDefinitions[modelName];var httpOptions={type:"POST"};if(opts.match){var expectedRequest={};var record=this.getStore().createRecord(modelName,match);expectedRequest[modelName]=record.serialize();httpOptions.data=JSON.stringify(expectedRequest)}var modelType=this.getStore().modelFor(modelName);var responseJson={};if(succeed){responseJson[modelName]=$.extend({id:definition.nextId()},match,returnArgs);Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}else{httpOptions.status=500}this.stubEndpointForHttpRequest(url,responseJson,httpOptions)},handleUpdate:function(){var args=Array.prototype.slice.call(arguments);Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var succeed=true;if(typeof args[args.length-1]=="boolean"){args.pop();succeed=false}Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var type,id;if(args[0]instanceof DS.Model){var model=args[0];type=model.constructor.typeKey;id=model.id}else if(typeof args[0]=="string"&&typeof parseInt(args[1])=="number"){type=args[0];id=args[1]}Ember.assert("To handleUpdate pass in a model instance or a type and an id",type&&id);this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:succeed?200:500})},handleDelete:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:succeed?200:500})},teardown:function(){FactoryGuy.resetModels(this.getStore());$.mockjax.clear()}});if(FactoryGuy!==undefined){FactoryGuy.testMixin=FactoryGuyTestMixin}
|
data/package.json
CHANGED
@@ -104,10 +104,42 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
104
104
|
var url = this.buildURL(modelName);
|
105
105
|
this.stubEndpointForHttpRequest(url, responseJson);
|
106
106
|
},
|
107
|
+
|
108
|
+
/**
|
109
|
+
Handling ajax GET for finding one record for a type of model and an id.
|
110
|
+
You can mock failed find by passing in success argument as false.
|
111
|
+
|
112
|
+
```js
|
113
|
+
// Pass in the parameters you would normally pass into FactoryGuy.make,
|
114
|
+
// like fixture name, number of fixtures to make, and optional traits,
|
115
|
+
// or fixture options
|
116
|
+
testHelper.handleFindOne('user', 'with_hats', {id: 1});
|
117
|
+
|
118
|
+
store.find('user', 1).then(function(user){
|
119
|
+
|
120
|
+
});
|
121
|
+
```
|
122
|
+
|
123
|
+
@param {String} name name of the fixture ( or model ) to find
|
124
|
+
@param {String} trait optional traits (one or more)
|
125
|
+
@param {Object} opts optional fixture options (including id)
|
126
|
+
*/
|
127
|
+
handleFindOne: function () {
|
128
|
+
// make the records and load them in the store
|
129
|
+
var record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
130
|
+
var name = arguments[0];
|
131
|
+
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
132
|
+
var responseJson = {};
|
133
|
+
var json = record.toJSON({includeId: true});
|
134
|
+
responseJson[modelName.pluralize()] = json;
|
135
|
+
var url = this.buildURL(modelName, record.id);
|
136
|
+
this.stubEndpointForHttpRequest(url, responseJson);
|
137
|
+
},
|
138
|
+
|
107
139
|
/**
|
108
140
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
109
141
|
|
110
|
-
|
142
|
+
|
111
143
|
```js
|
112
144
|
|
113
145
|
// Create model instances
|
@@ -121,9 +153,11 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
121
153
|
})
|
122
154
|
```
|
123
155
|
|
124
|
-
|
156
|
+
By omitting the last argument (pass in no records), this simulates a findQuery
|
157
|
+
request that returns no records
|
158
|
+
|
125
159
|
```js
|
126
|
-
//
|
160
|
+
// Simulate a query that returns no results
|
127
161
|
testHelper.handleFindQuery('user', ['age']);
|
128
162
|
|
129
163
|
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
@@ -149,6 +149,50 @@ asyncTest("#handleFindMany with traits and extra options", function () {
|
|
149
149
|
});
|
150
150
|
|
151
151
|
|
152
|
+
//////// handleFindOne /////////
|
153
|
+
|
154
|
+
asyncTest("#handleFindOne the basic", function () {
|
155
|
+
var id = 1
|
156
|
+
testHelper.handleFindOne('profile', {id: id});
|
157
|
+
|
158
|
+
store.find('profile', 1).then(function (profile) {
|
159
|
+
ok(profile.get('id') == id);
|
160
|
+
start();
|
161
|
+
});
|
162
|
+
});
|
163
|
+
|
164
|
+
asyncTest("#handleFindOne with traits", function () {
|
165
|
+
var id = 1
|
166
|
+
testHelper.handleFindOne('profile', 'goofy_description', {id: id});
|
167
|
+
|
168
|
+
store.find('profile', 1).then(function (profile) {
|
169
|
+
ok(profile.get('description') == 'goofy');
|
170
|
+
start();
|
171
|
+
});
|
172
|
+
});
|
173
|
+
|
174
|
+
asyncTest("#handleFindOne with arguments", function () {
|
175
|
+
var id = 1
|
176
|
+
var description = 'guy';
|
177
|
+
testHelper.handleFindOne('profile', {id: id, description: description });
|
178
|
+
|
179
|
+
store.find('profile', 1).then(function (profile) {
|
180
|
+
ok(profile.get('description') == description);
|
181
|
+
start();
|
182
|
+
});
|
183
|
+
});
|
184
|
+
|
185
|
+
asyncTest("#handleFindOne with traits and arguments", function () {
|
186
|
+
var id = 1
|
187
|
+
var description = 'guy';
|
188
|
+
testHelper.handleFindOne('profile', 'goofy_description', {id: id, description: description});
|
189
|
+
|
190
|
+
store.find('profile', 1).then(function (profile) {
|
191
|
+
ok(profile.get('description') == description);
|
192
|
+
start();
|
193
|
+
});
|
194
|
+
})
|
195
|
+
|
152
196
|
|
153
197
|
|
154
198
|
module('FactoryGuyTestMixin (using mockjax) with DS.ActiveModelAdapter', {
|
@@ -596,7 +596,7 @@ var FactoryGuy = {
|
|
596
596
|
* Most of the work of making the model from the json fixture is going on here.
|
597
597
|
* @param modelType
|
598
598
|
* @param fixture
|
599
|
-
* @returns {
|
599
|
+
* @returns {DS.Model} instance of DS.Model
|
600
600
|
*/
|
601
601
|
makeModel: function (modelType, fixture) {
|
602
602
|
var store = this,
|
@@ -952,10 +952,42 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
952
952
|
var url = this.buildURL(modelName);
|
953
953
|
this.stubEndpointForHttpRequest(url, responseJson);
|
954
954
|
},
|
955
|
+
|
956
|
+
/**
|
957
|
+
Handling ajax GET for finding one record for a type of model and an id.
|
958
|
+
You can mock failed find by passing in success argument as false.
|
959
|
+
|
960
|
+
```js
|
961
|
+
// Pass in the parameters you would normally pass into FactoryGuy.make,
|
962
|
+
// like fixture name, number of fixtures to make, and optional traits,
|
963
|
+
// or fixture options
|
964
|
+
testHelper.handleFindOne('user', 'with_hats', {id: 1});
|
965
|
+
|
966
|
+
store.find('user', 1).then(function(user){
|
967
|
+
|
968
|
+
});
|
969
|
+
```
|
970
|
+
|
971
|
+
@param {String} name name of the fixture ( or model ) to find
|
972
|
+
@param {String} trait optional traits (one or more)
|
973
|
+
@param {Object} opts optional fixture options (including id)
|
974
|
+
*/
|
975
|
+
handleFindOne: function () {
|
976
|
+
// make the records and load them in the store
|
977
|
+
var record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
978
|
+
var name = arguments[0];
|
979
|
+
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
980
|
+
var responseJson = {};
|
981
|
+
var json = record.toJSON({includeId: true});
|
982
|
+
responseJson[modelName.pluralize()] = json;
|
983
|
+
var url = this.buildURL(modelName, record.id);
|
984
|
+
this.stubEndpointForHttpRequest(url, responseJson);
|
985
|
+
},
|
986
|
+
|
955
987
|
/**
|
956
988
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
957
989
|
|
958
|
-
|
990
|
+
|
959
991
|
```js
|
960
992
|
|
961
993
|
// Create model instances
|
@@ -969,9 +1001,11 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
969
1001
|
})
|
970
1002
|
```
|
971
1003
|
|
972
|
-
|
1004
|
+
By omitting the last argument (pass in no records), this simulates a findQuery
|
1005
|
+
request that returns no records
|
1006
|
+
|
973
1007
|
```js
|
974
|
-
//
|
1008
|
+
// Simulate a query that returns no results
|
975
1009
|
testHelper.handleFindQuery('user', ['age']);
|
976
1010
|
|
977
1011
|
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
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.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Sudol
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-01-
|
12
|
+
date: 2015-01-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|