ember-data-factory-guy 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gruntfile.js +45 -0
- data/README.md +118 -0
- data/bower.json +35 -0
- data/dist/ember-data-factory-guy.js +376 -0
- data/dist/ember-data-factory-guy.min.js +1 -0
- data/ember-data-factory-guy.gemspec +20 -0
- data/lib/ember-data-factory-guy.rb +13 -0
- data/package.json +41 -0
- data/src/factory_guy.js +144 -0
- data/src/factory_guy_helper_mixin.js +89 -0
- data/src/has_many.js +188 -0
- data/src/store.js +142 -0
- data/tests/active_model_adapter_factory_test.js +63 -0
- data/tests/factory_guy_test.js +0 -0
- data/tests/fixture_adapter_factory_test.js +134 -0
- data/tests/index.html +38 -0
- data/tests/rest_adapter_factory_test.js +61 -0
- data/tests/store_test.js +35 -0
- data/tests/support/factories/project_factory.js +3 -0
- data/tests/support/factories/user_factory.js +10 -0
- data/tests/support/models/project.js +4 -0
- data/tests/support/models/user.js +4 -0
- data/tests/support/test_helper.js +23 -0
- data/tests/test_setup.js +22 -0
- data/vendor/assets/javascripts/ember_data_factory_guy.js +376 -0
- data/vendor/assets/javascripts/factory_guy_has_many.js +188 -0
- metadata +76 -0
data/src/store.js
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
DS.Store.reopen({
|
2
|
+
|
3
|
+
usingFixtureAdapter: function() {
|
4
|
+
var adapter = this.adapterFor('application');
|
5
|
+
return adapter instanceof DS.FixtureAdapter
|
6
|
+
},
|
7
|
+
|
8
|
+
/**
|
9
|
+
Make new fixture and save to store. If the store is using FixtureAdapter,
|
10
|
+
will push to FIXTURE array, otherwise will use push method on adapter.
|
11
|
+
|
12
|
+
@param name
|
13
|
+
@param options
|
14
|
+
@returns {*}
|
15
|
+
*/
|
16
|
+
makeFixture: function (name, options) {
|
17
|
+
var modelName = FactoryGuy.lookupModelForName(name);
|
18
|
+
var fixture = FactoryGuy.build(name, options);
|
19
|
+
var modelType = this.modelFor(modelName);
|
20
|
+
|
21
|
+
if (this.usingFixtureAdapter()) {
|
22
|
+
this.setBelongsToFixturesAssociation(modelType, modelName, fixture);
|
23
|
+
return FactoryGuy.pushFixture(modelType, fixture);
|
24
|
+
} else {
|
25
|
+
var self = this;
|
26
|
+
var model;
|
27
|
+
Em.run( function() {
|
28
|
+
model = self.push(modelName, fixture);
|
29
|
+
self.setBelongsToRestAssociation(modelType, modelName, model);
|
30
|
+
});
|
31
|
+
return model;
|
32
|
+
}
|
33
|
+
},
|
34
|
+
|
35
|
+
/**
|
36
|
+
Trying to set the belongsTo association for FixtureAdapter,
|
37
|
+
with models that have a hasMany association.
|
38
|
+
|
39
|
+
For example if a client hasMany projects, then set the client.id
|
40
|
+
on each project that the client hasMany of, so that the project
|
41
|
+
now has the belongsTo client association setup.
|
42
|
+
|
43
|
+
@param name
|
44
|
+
@param model
|
45
|
+
*/
|
46
|
+
setBelongsToFixturesAssociation: function (modelType, modelName, parentFixture) {
|
47
|
+
var store = this;
|
48
|
+
var adapter = this.adapterFor('application');
|
49
|
+
var relationShips = Ember.get(modelType, 'relationshipNames');
|
50
|
+
if (relationShips.hasMany) {
|
51
|
+
relationShips.hasMany.forEach(function (relationship) {
|
52
|
+
var hasManyModel = store.modelFor(Em.String.singularize(relationship));
|
53
|
+
if (parentFixture[relationship]) {
|
54
|
+
parentFixture[relationship].forEach(function(id) {
|
55
|
+
var hasManyfixtures = adapter.fixturesForType(hasManyModel);
|
56
|
+
var fixture = adapter.findFixtureById(hasManyfixtures, id);
|
57
|
+
fixture[modelName] = parentFixture.id;
|
58
|
+
})
|
59
|
+
}
|
60
|
+
})
|
61
|
+
}
|
62
|
+
},
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Trying to set the belongsTo association for the rest type models
|
66
|
+
* with a hasMany association
|
67
|
+
*
|
68
|
+
* For example if a client hasMany projects, then set the client
|
69
|
+
* on each project that the client hasMany of, so that the project
|
70
|
+
* now has the belongsTo client association setup
|
71
|
+
*
|
72
|
+
* @param modelType
|
73
|
+
* @param modelName
|
74
|
+
* @param parent model to check for hasMany
|
75
|
+
*/
|
76
|
+
setBelongsToRestAssociation: function (modelType, modelName, parent) {
|
77
|
+
var relationShips = Ember.get(modelType, 'relationshipNames');
|
78
|
+
|
79
|
+
if (relationShips.hasMany) {
|
80
|
+
relationShips.hasMany.forEach(function (name) {
|
81
|
+
var children = parent.get(name);
|
82
|
+
if (children.get('length') > 0) {
|
83
|
+
children.forEach(function(child) {
|
84
|
+
child.set(modelName, parent)
|
85
|
+
})
|
86
|
+
}
|
87
|
+
})
|
88
|
+
}
|
89
|
+
},
|
90
|
+
|
91
|
+
/**
|
92
|
+
Adding a pushPayload for FixtureAdapter, but using the original with
|
93
|
+
other adapters that support pushPayload.
|
94
|
+
|
95
|
+
@param type
|
96
|
+
@param payload
|
97
|
+
*/
|
98
|
+
pushPayload: function (type, payload) {
|
99
|
+
if (this.usingFixtureAdapter()) {
|
100
|
+
var model = this.modelFor(modelName);
|
101
|
+
FactoryGuy.pushFixture(model, payload);
|
102
|
+
} else {
|
103
|
+
this._super(type, payload);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
});
|
107
|
+
|
108
|
+
|
109
|
+
DS.FixtureAdapter.reopen({
|
110
|
+
|
111
|
+
/**
|
112
|
+
Overriding createRecord in FixtureAdapter to add the record
|
113
|
+
created to the hashMany records for all of the records that
|
114
|
+
this one belongsTo.
|
115
|
+
|
116
|
+
@method createRecord
|
117
|
+
@param {DS.Store} store
|
118
|
+
@param {subclass of DS.Model} type
|
119
|
+
@param {DS.Model} record
|
120
|
+
@return {Promise} promise
|
121
|
+
*/
|
122
|
+
createRecord: function(store, type, record) {
|
123
|
+
var promise = this._super(store, type, record);
|
124
|
+
|
125
|
+
promise.then( function() {
|
126
|
+
var hasManyName = Ember.String.pluralize(type.typeKey);
|
127
|
+
var relationShips = Ember.get(type, 'relationshipNames');
|
128
|
+
if (relationShips.belongsTo) {
|
129
|
+
// console.log('record',record+'', type.typeKey, hasManyName);
|
130
|
+
// relationShips.belongsTo.forEach(function (relationship) {
|
131
|
+
// console.log(relationship, record.get(relationship)+'')
|
132
|
+
// var belongsToRecord = record.get(relationship);
|
133
|
+
// console.log(relationshipForType)
|
134
|
+
// belongsToRecord.get(hasManyName).addObject(record);
|
135
|
+
// })
|
136
|
+
}
|
137
|
+
})
|
138
|
+
return promise;
|
139
|
+
}
|
140
|
+
|
141
|
+
})
|
142
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
var testHelper;
|
2
|
+
var store;
|
3
|
+
|
4
|
+
module('FactoryGuy with ActiveModelAdapter', {
|
5
|
+
setup: function() {
|
6
|
+
testHelper = TestHelper.setup(DS.ActiveModelAdapter);
|
7
|
+
store = testHelper.getStore();
|
8
|
+
},
|
9
|
+
teardown: function() {
|
10
|
+
Em.run(function() { testHelper.teardown(); });
|
11
|
+
}
|
12
|
+
});
|
13
|
+
|
14
|
+
|
15
|
+
test("#resetModels clears the store of models, and resets the model ids", function() {
|
16
|
+
var project = store.makeFixture('project');
|
17
|
+
var user = store.makeFixture('user', {projects: [project.id]});
|
18
|
+
|
19
|
+
FactoryGuy.resetModels(store);
|
20
|
+
|
21
|
+
equal(store.all('user').get('content.length'),0)
|
22
|
+
equal(store.all('project').get('content.length'),0)
|
23
|
+
|
24
|
+
deepEqual(FactoryGuy.modelIds, {});
|
25
|
+
});
|
26
|
+
|
27
|
+
|
28
|
+
module('DS.Store#makeFixture with ActiveModelAdapter', {
|
29
|
+
setup: function() {
|
30
|
+
testHelper = TestHelper.setup(DS.ActiveModelAdapter);
|
31
|
+
store = testHelper.getStore();
|
32
|
+
},
|
33
|
+
teardown: function() {
|
34
|
+
Em.run(function() { testHelper.teardown(); });
|
35
|
+
}
|
36
|
+
});
|
37
|
+
|
38
|
+
|
39
|
+
asyncTest("creates records in the store", function() {
|
40
|
+
var user = store.makeFixture('user');
|
41
|
+
|
42
|
+
store.find('user', user.id).then ( function(store_user) {
|
43
|
+
deepEqual(store_user.toJSON(), user.toJSON());
|
44
|
+
start();
|
45
|
+
});
|
46
|
+
});
|
47
|
+
|
48
|
+
test("supports hasMany associations", function() {
|
49
|
+
var p1 = store.makeFixture('project');
|
50
|
+
var p2 = store.makeFixture('project');
|
51
|
+
var user = store.makeFixture('user', {projects: [p1.id, p2.id]})
|
52
|
+
|
53
|
+
equal(user.get('projects.length'), 2);
|
54
|
+
})
|
55
|
+
|
56
|
+
|
57
|
+
test("when hasMany associations assigned, belongTo parent is assigned", function() {
|
58
|
+
var p1 = store.makeFixture('project');
|
59
|
+
var user = store.makeFixture('user', {projects: [p1.id]})
|
60
|
+
|
61
|
+
deepEqual(p1.get('user').toJSON(), user.toJSON());
|
62
|
+
})
|
63
|
+
|
File without changes
|
@@ -0,0 +1,134 @@
|
|
1
|
+
window.testHelper;
|
2
|
+
var store;
|
3
|
+
|
4
|
+
module('FactoryGuy with DS.FixtureAdapter', {
|
5
|
+
setup: function() {
|
6
|
+
testHelper = TestHelper.setup(DS.FixtureAdapter);
|
7
|
+
store = testHelper.getStore();
|
8
|
+
},
|
9
|
+
teardown: function() {
|
10
|
+
Em.run(function() { testHelper.teardown(); });
|
11
|
+
}
|
12
|
+
});
|
13
|
+
|
14
|
+
|
15
|
+
test("#build creates default json for model", function() {
|
16
|
+
var json = FactoryGuy.build('user');
|
17
|
+
deepEqual(json, {id: 1, name: 'User1'});
|
18
|
+
});
|
19
|
+
|
20
|
+
|
21
|
+
test("#build can override default model attributes", function() {
|
22
|
+
var json = FactoryGuy.build('user',{name: 'bob'});
|
23
|
+
deepEqual(json, {id: 1, name: 'bob'});
|
24
|
+
});
|
25
|
+
|
26
|
+
|
27
|
+
test("#build can have named model definition with custom attributes", function() {
|
28
|
+
var json = FactoryGuy.build('admin')
|
29
|
+
deepEqual(json, {id: 1, name: 'Admin'});
|
30
|
+
});
|
31
|
+
|
32
|
+
|
33
|
+
test("#build can override named model attributes", function() {
|
34
|
+
var json = FactoryGuy.build('admin', {name: 'AdminGuy'})
|
35
|
+
deepEqual(json, {id: 1, name: 'AdminGuy'});
|
36
|
+
});
|
37
|
+
|
38
|
+
|
39
|
+
test("#build similar model type ids are created sequentially", function() {
|
40
|
+
var user1 = FactoryGuy.build('user');
|
41
|
+
var user2 = FactoryGuy.build('user');
|
42
|
+
var project = FactoryGuy.build('project');
|
43
|
+
equal(user1.id, 1);
|
44
|
+
equal(user2.id, 2);
|
45
|
+
equal(project.id, 1);
|
46
|
+
});
|
47
|
+
|
48
|
+
|
49
|
+
test("#pushFixture adds fixture to Fixture array on model", function() {
|
50
|
+
var fixtureJson = FactoryGuy.build('user');
|
51
|
+
FactoryGuy.pushFixture(User, fixtureJson);
|
52
|
+
equal(User.FIXTURES.length, 1);
|
53
|
+
|
54
|
+
var fixtureJson2 = FactoryGuy.build('user');
|
55
|
+
FactoryGuy.pushFixture(User, fixtureJson2);
|
56
|
+
equal(User.FIXTURES.length, 2);
|
57
|
+
});
|
58
|
+
|
59
|
+
|
60
|
+
asyncTest("can change fixture attributes after creation", function() {
|
61
|
+
var user = store.makeFixture('user');
|
62
|
+
user.name = "new name";
|
63
|
+
|
64
|
+
store.find('user', 1).then( function(user) {
|
65
|
+
equal(user.get('name'), "new name", "changes local attributes");
|
66
|
+
start();
|
67
|
+
});
|
68
|
+
});
|
69
|
+
|
70
|
+
|
71
|
+
test("#resetModels clears the store of models, clears the FIXTURES arrays for each model and resets the model ids", function() {
|
72
|
+
var project = store.makeFixture('project');
|
73
|
+
var user = store.makeFixture('user', {projects: [project.id]});
|
74
|
+
|
75
|
+
FactoryGuy.resetModels(store);
|
76
|
+
|
77
|
+
equal(User.FIXTURES.length, 0);
|
78
|
+
equal(Project.FIXTURES.length, 0);
|
79
|
+
|
80
|
+
equal(store.all('user').get('content.length'),0)
|
81
|
+
equal(store.all('project').get('content.length'),0)
|
82
|
+
|
83
|
+
deepEqual(FactoryGuy.modelIds, {});
|
84
|
+
});
|
85
|
+
|
86
|
+
|
87
|
+
module('DS.Store with DS.FixtureAdapter', {
|
88
|
+
setup: function() {
|
89
|
+
testHelper = TestHelper.setup(DS.FixtureAdapter);
|
90
|
+
store = testHelper.getStore();
|
91
|
+
},
|
92
|
+
teardown: function() {
|
93
|
+
Em.run(function() { testHelper.teardown(); });
|
94
|
+
}
|
95
|
+
});
|
96
|
+
|
97
|
+
|
98
|
+
test("#makeFixture builds and pushes fixture into the store", function() {
|
99
|
+
var json = store.makeFixture('user');
|
100
|
+
equal(User.FIXTURES.length, 1);
|
101
|
+
equal(User.FIXTURES[0], json);
|
102
|
+
});
|
103
|
+
|
104
|
+
|
105
|
+
asyncTest("#makeFixture sets hasMany associations on fixtures", function() {
|
106
|
+
var p1 = store.makeFixture('project');
|
107
|
+
// second project not added on purpose to make sure only one is
|
108
|
+
// assigned in hasMany
|
109
|
+
store.makeFixture('project');
|
110
|
+
var user = store.makeFixture('user', {projects: [p1.id]})
|
111
|
+
|
112
|
+
store.find('user', 1).then ( function(user) {
|
113
|
+
user.get('projects').then( function(projects) {
|
114
|
+
equal(projects.get('length'), 1, "adds hasMany records");
|
115
|
+
equal(projects.get('firstObject.user.id'), 1, "sets belongsTo record");
|
116
|
+
start();
|
117
|
+
})
|
118
|
+
})
|
119
|
+
})
|
120
|
+
|
121
|
+
asyncTest("#createRecord adds belongsTo associations to hasMany array", function() {
|
122
|
+
var user = store.makeFixture('user');
|
123
|
+
|
124
|
+
store.find('user', user.id).then(function(user){
|
125
|
+
|
126
|
+
var projectJson = {title:'project', user: user};
|
127
|
+
|
128
|
+
store.createRecord('project', projectJson).save()
|
129
|
+
.then( function() {
|
130
|
+
equal(user.get('projects.length'), 1);
|
131
|
+
start();
|
132
|
+
});
|
133
|
+
})
|
134
|
+
})
|
data/tests/index.html
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset='UTF-8' />
|
5
|
+
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
|
6
|
+
|
7
|
+
<title>jQuery.simpleFAQ QUnit Test Runner</title>
|
8
|
+
|
9
|
+
<link rel="stylesheet" type="text/css" href="../bower_components/qunit/qunit/qunit.css">
|
10
|
+
<!-- add any external libraries your code needs -->
|
11
|
+
<script src='../bower_components/jquery/jquery.js'></script>
|
12
|
+
<script src='../bower_components/handlebars/handlebars.js'></script>
|
13
|
+
<script src='../bower_components/ember/ember.js'></script>
|
14
|
+
<script src='../bower_components/ember-data/ember-data.js'></script>
|
15
|
+
|
16
|
+
<script src='../dist/ember-data-factory-guy.js'></script>
|
17
|
+
<script src='../src/has_many.js'></script>
|
18
|
+
|
19
|
+
<!-- your tests, any and all to run with the given fixtures below -->
|
20
|
+
<script src='../tests/test_setup.js'></script>
|
21
|
+
<script src='../tests/support/test_helper.js'></script>
|
22
|
+
|
23
|
+
<script src="../bower_components/qunit/qunit/qunit.js"></script>
|
24
|
+
|
25
|
+
</head>
|
26
|
+
<body>
|
27
|
+
<div id="qunit"></div> <!-- QUnit fills this with results, etc -->
|
28
|
+
<script src='active_model_adapter_factory_test.js'></script>
|
29
|
+
<script src='fixture_adapter_factory_test.js'></script>
|
30
|
+
<script src='rest_adapter_factory_test.js'></script>
|
31
|
+
<script src='store_test.js'></script>
|
32
|
+
<div id='qunit-fixture'>
|
33
|
+
|
34
|
+
<!-- any HTML you want to be present in each test (will be reset for each test) -->
|
35
|
+
|
36
|
+
</div>
|
37
|
+
</body>
|
38
|
+
</html>
|
@@ -0,0 +1,61 @@
|
|
1
|
+
var testHelper;
|
2
|
+
var store;
|
3
|
+
|
4
|
+
module('FactoryGuy with DS.RESTAdapter', {
|
5
|
+
setup: function() {
|
6
|
+
testHelper = TestHelper.setup(DS.RESTAdapter);
|
7
|
+
store = testHelper.getStore();
|
8
|
+
},
|
9
|
+
teardown: function() {
|
10
|
+
Em.run(function() { testHelper.teardown(); });
|
11
|
+
}
|
12
|
+
});
|
13
|
+
|
14
|
+
|
15
|
+
test("#resetModels clears the store of models, and resets the model ids", function() {
|
16
|
+
var project = store.makeFixture('project');
|
17
|
+
var user = store.makeFixture('user', {projects: [project.id]});
|
18
|
+
|
19
|
+
FactoryGuy.resetModels(store);
|
20
|
+
|
21
|
+
equal(store.all('user').get('content.length'),0)
|
22
|
+
equal(store.all('project').get('content.length'),0)
|
23
|
+
|
24
|
+
deepEqual(FactoryGuy.modelIds, {});
|
25
|
+
});
|
26
|
+
|
27
|
+
|
28
|
+
module('DS.Store#makeFixture with RestAdapter', {
|
29
|
+
setup: function() {
|
30
|
+
testHelper = TestHelper.setup(DS.RESTAdapter);
|
31
|
+
store = testHelper.getStore();
|
32
|
+
},
|
33
|
+
teardown: function() {
|
34
|
+
Em.run(function() { testHelper.teardown(); });
|
35
|
+
}
|
36
|
+
});
|
37
|
+
|
38
|
+
asyncTest("creates records in the store", function() {
|
39
|
+
var user = store.makeFixture('user');
|
40
|
+
|
41
|
+
store.find('user', user.id).then ( function(store_user) {
|
42
|
+
deepEqual(store_user.toJSON(), user.toJSON());
|
43
|
+
start()
|
44
|
+
});
|
45
|
+
});
|
46
|
+
|
47
|
+
test("supports hasMany associations", function() {
|
48
|
+
var p1 = store.makeFixture('project');
|
49
|
+
var p2 = store.makeFixture('project');
|
50
|
+
var user = store.makeFixture('user', {projects: [p1.id, p2.id]})
|
51
|
+
|
52
|
+
equal(user.get('projects.length'), 2);
|
53
|
+
});
|
54
|
+
|
55
|
+
|
56
|
+
test("when hasMany associations assigned, belongTo parent is assigned", function() {
|
57
|
+
var p1 = store.makeFixture('project');
|
58
|
+
var user = store.makeFixture('user', {projects: [p1.id]})
|
59
|
+
|
60
|
+
deepEqual(p1.get('user').toJSON(), user.toJSON());
|
61
|
+
});
|
data/tests/store_test.js
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
var container;
|
2
|
+
|
3
|
+
module('DS.Store#usingFixtureAdapter', {
|
4
|
+
setup: function() {
|
5
|
+
container = new Ember.Container();
|
6
|
+
},
|
7
|
+
teardown: function() {}
|
8
|
+
});
|
9
|
+
|
10
|
+
var getStore = function(adapter) {
|
11
|
+
container.register("store:main", DS.Store.extend({adapter: adapter}));
|
12
|
+
return container.lookup("store:main");
|
13
|
+
}
|
14
|
+
|
15
|
+
test("with DS.FixtureAdapter", function() {
|
16
|
+
var adapter = DS.FixtureAdapter
|
17
|
+
equal(getStore(adapter).usingFixtureAdapter(), true );
|
18
|
+
});
|
19
|
+
|
20
|
+
test("when extending DS.FixtureAdapter", function() {
|
21
|
+
var adapter = DS.FixtureAdapter.extend({});
|
22
|
+
equal(getStore(adapter).usingFixtureAdapter(), true );
|
23
|
+
});
|
24
|
+
|
25
|
+
test("with DS.RESTAdapter", function() {
|
26
|
+
var adapter = DS.RESTAdapter
|
27
|
+
equal(getStore(adapter).usingFixtureAdapter(), false );
|
28
|
+
});
|
29
|
+
|
30
|
+
test("with DS.ActiveModelAdapter", function() {
|
31
|
+
var adapter = DS.ActiveModelAdapter
|
32
|
+
equal(getStore(adapter).usingFixtureAdapter(), false );
|
33
|
+
});
|
34
|
+
|
35
|
+
|