Skip to content

Commit 7a607c3

Browse files
committed
(bugfix): fix #389 with nested hashkeys and improve tests.
1 parent ad288f7 commit 7a607c3

File tree

6 files changed

+201
-25
lines changed

6 files changed

+201
-25
lines changed

modules/angular-meteor-meteorCollection.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ angularMeteorCollections.factory('AngularMeteorCollection', ['$q', '$meteorSubsc
3131

3232
// delete $$hashkey
3333
if (!(item instanceof File))
34-
item = angular.copy(item);
34+
item = $meteorUtils.stripDollarPrefixedKeys(item);
3535

3636
if (item._id) { // Performs an update if the _id property is set.
3737
var item_id = item._id; // Store the _id in temporary variable
@@ -45,6 +45,7 @@ angularMeteorCollections.factory('AngularMeteorCollection', ['$q', '$meteorSubsc
4545
} else {
4646
deferred.resolve({_id: objectId, action: "updated"});
4747
}
48+
$rootScope.$apply();
4849
});
4950
} else { // Performs an insert if the _id property isn't set.
5051
collection.insert(item, function (error, result) {
@@ -53,6 +54,7 @@ angularMeteorCollections.factory('AngularMeteorCollection', ['$q', '$meteorSubsc
5354
} else {
5455
deferred.resolve({_id: result, action: "inserted"});
5556
}
57+
$rootScope.$apply();
5658
});
5759
}
5860
return deferred.promise;
@@ -103,6 +105,7 @@ angularMeteorCollections.factory('AngularMeteorCollection', ['$q', '$meteorSubsc
103105
} else {
104106
deferred.resolve({_id: objectId, action: "removed"});
105107
}
108+
$rootScope.$apply();
106109
});
107110
} else {
108111
deferred.reject("key cannot be null");

modules/angular-meteor-utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ angularMeteorUtils.service('$meteorUtils', [ '$timeout',
2323
// return autorun object so that it can be stopped manually
2424
return comp;
2525
};
26+
// Borrowed from angularFire - https://github.com/firebase/angularfire/blob/master/src/utils.js#L445-L454
27+
this.stripDollarPrefixedKeys = function (data) {
28+
if( !angular.isObject(data) || data instanceof File) { return data; }
29+
var out = angular.isArray(data)? [] : {};
30+
angular.forEach(data, function(v,k) {
31+
if(typeof k !== 'string' || k.charAt(0) !== '$') {
32+
out[k] = self.stripDollarPrefixedKeys(v);
33+
}
34+
});
35+
return out;
36+
}
2637
}]);
2738

2839
angularMeteorUtils.run(['$rootScope', '$meteorUtils',

package.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ Package.onTest(function(api) {
6363

6464
// auxiliary
6565
api.addFiles([
66-
'tests/integration/auxiliary/matchers.js'
66+
'tests/integration/auxiliary/matchers.js',
67+
'tests/integration/auxiliary/test_data.js'
6768
]);
6869

6970
// spec files

tests/integration/angular-meteor-collection-spec.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -202,29 +202,16 @@ describe('$meteorCollection service', function() {
202202

203203
meteorArray.save();
204204

205-
expect({a : 444}).toBeFoundInCollection(MyCollection);
205+
expect({a : 444, b: 2}).toBeFoundExactlyInCollection(MyCollection);
206206
});
207207

208-
it('should save objects with nested $$haskey fields when save is called', function() {
209-
var itemWithNestedArray = {
210-
myId : 555,
211-
nestedArray : [
212-
{
213-
$$hashkey: 1,
214-
a: 10,
215-
b: 11
216-
},
217-
{
218-
$$hashkey: 2,
219-
a: 12,
220-
b: 13
221-
}
222-
]
223-
};
224-
225-
meteorArray.save(itemWithNestedArray);
226-
227-
expect({myId : 555}).toBeFoundInCollection(MyCollection);
208+
it('should save objects with nested $$haskey fields when save is called', function(done) {
209+
meteorArray.save(itemWithNestedHashkeyArrays).then(function() {
210+
expect(itemWithNestedHashkeysRemoved).toBeFoundExactlyInCollection(MyCollection);
211+
done();
212+
}, function() {
213+
done();
214+
});
228215
});
229216
});
230217
});

tests/integration/auxiliary/matchers.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@ customMatchers = {
99
}
1010
};
1111
},
12-
toBeFoundInCollection : function() {
12+
toBeFoundExactlyInCollection : function(util, customEqualityTesters) {
1313
return {
1414
compare : function(actual, expected) {
1515
var result = {};
1616
var expectedItem = expected.findOne(actual);
17-
result.pass = !_.isUndefined(expectedItem);
17+
18+
if (_.isUndefined(expectedItem)) {
19+
result.pass = false;
20+
result.message = 'Expected ' + JSON.stringify(actual) + ' to be found in collection ' + JSON.stringify(expected.find({}).fetch());
21+
}
22+
else {
23+
delete expectedItem._id;
24+
result.pass = util.equals(actual, expectedItem, customEqualityTesters);
25+
}
26+
27+
if (!result.pass) {
28+
result.message = 'Expected ' + JSON.stringify(actual) + ' to be found in collection ' + JSON.stringify(expected.find({}).fetch());
29+
}
30+
1831
return result;
1932
}
2033
};
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
itemWithNestedHashkeyArrays = {
2+
myId : 555,
3+
nestedArray : [
4+
{
5+
$$hashkey: 1,
6+
a: 10,
7+
b: 11,
8+
nestedArray : [
9+
{
10+
$$hashkey: 1,
11+
a: 10,
12+
b: 11,
13+
nestedArray : [
14+
{
15+
$$hashkey: 1,
16+
a: 10,
17+
b: 11
18+
},
19+
{
20+
$$hashkey: 2,
21+
a: 12,
22+
b: 13
23+
}
24+
]
25+
},
26+
{
27+
$$hashkey: 2,
28+
a: 12,
29+
b: 13,
30+
nestedArray : [
31+
{
32+
$$hashkey: 1,
33+
a: 10,
34+
b: 11
35+
},
36+
{
37+
$$hashkey: 2,
38+
a: 12,
39+
b: 13
40+
}
41+
]
42+
}
43+
]
44+
},
45+
{
46+
$$hashkey: 2,
47+
a: 12,
48+
b: 13,
49+
nestedArray : [
50+
{
51+
$$hashkey: 1,
52+
a: 10,
53+
b: 11,
54+
nestedArray : [
55+
{
56+
$$hashkey: 1,
57+
a: 10,
58+
b: 11
59+
},
60+
{
61+
$$hashkey: 2,
62+
a: 12,
63+
b: 13
64+
}
65+
]
66+
},
67+
{
68+
$$hashkey: 2,
69+
a: 12,
70+
b: 13,
71+
nestedArray : [
72+
{
73+
$$hashkey: 1,
74+
a: 10,
75+
b: 11
76+
},
77+
{
78+
$$hashkey: 2,
79+
a: 12,
80+
b: 13
81+
}
82+
]
83+
}
84+
]
85+
}
86+
]
87+
};
88+
89+
itemWithNestedHashkeysRemoved = {
90+
myId : 555,
91+
nestedArray : [
92+
{
93+
a: 10,
94+
b: 11,
95+
nestedArray : [
96+
{
97+
a: 10,
98+
b: 11,
99+
nestedArray : [
100+
{
101+
a: 10,
102+
b: 11
103+
},
104+
{
105+
a: 12,
106+
b: 13
107+
}
108+
]
109+
},
110+
{
111+
a: 12,
112+
b: 13,
113+
nestedArray : [
114+
{
115+
a: 10,
116+
b: 11
117+
},
118+
{
119+
a: 12,
120+
b: 13
121+
}
122+
]
123+
}
124+
]
125+
},
126+
{
127+
a: 12,
128+
b: 13,
129+
nestedArray : [
130+
{
131+
a: 10,
132+
b: 11,
133+
nestedArray : [
134+
{
135+
a: 10,
136+
b: 11
137+
},
138+
{
139+
a: 12,
140+
b: 13
141+
}
142+
]
143+
},
144+
{
145+
a: 12,
146+
b: 13,
147+
nestedArray : [
148+
{
149+
a: 10,
150+
b: 11
151+
},
152+
{
153+
a: 12,
154+
b: 13
155+
}
156+
]
157+
}
158+
]
159+
}
160+
]
161+
};

0 commit comments

Comments
 (0)