Skip to content

Commit

Permalink
Made existing test cases work, made arrays work
Browse files Browse the repository at this point in the history
  • Loading branch information
puf committed Jun 2, 2015
1 parent f098f7c commit 4ef3840
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/reactfire.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,17 @@ var ReactFireMixin = {
var out = [];
if (obj) {
if (this._isArray(obj)) {
out = obj;
for (var i=0; i < obj.length; i++) {
out.push({ $id: i, $value: obj[i] });
}
}
else if (typeof(obj) === "object") {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var item = obj[key];
if (typeof(item) !== 'object') {
item = { $value: item };
}
item.$id = key;
out.push(item);
}
Expand Down
46 changes: 44 additions & 2 deletions tests/specs/reactfire.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe("ReactFireMixin Tests:", function() {
},

componentDidUpdate: function(prevProps, prevState) {
expect(this.state).toEqual({ items: [1, 2, 3] });
expect(this.state).toEqual({ items: [{ $id: 'a', $value: 1 }, { $id: 'b', $value: 2 }, { $id:'c', $value: 3 }] });
done();
},

Expand All @@ -134,7 +134,7 @@ describe("ReactFireMixin Tests:", function() {
},

componentDidUpdate: function(prevProps, prevState) {
expect(this.state).toEqual({ items: [2, 3] });
expect(this.state).toEqual({ items: [{ $id: 'b', $value: 2 }, { $id:'c', $value: 3 }] });
done();
},

Expand All @@ -145,6 +145,48 @@ describe("ReactFireMixin Tests:", function() {

React.render(new TestComponent(), document.body);
});

it("bindAsArray() makes $id available on array items when they are objects", function(done) {
var TestComponent = React.createClass({
mixins: [ReactFireMixin],
componentWillMount: function() {
this.bindAsArray(firebaseRef, "items");
},
componentDidMount: function() {
firebaseRef.set({ first: { index: 1 }, second: { index: 2 }, third: { index: 3 } });
},
componentDidUpdate: function(prevProps, prevState) {
expect(this.state.items.map(function(item) { return item.$id; })).toEqual(['first', 'second', 'third']);
done();
},
render: function() {
return React.DOM.div(null, "Testing");
}
});
React.render(new TestComponent(), document.body);
})

it("bindAsArray() makes $id available on array items when they are primitives", function(done) {
var TestComponent = React.createClass({
mixins: [ReactFireMixin],
componentWillMount: function() {
this.bindAsArray(firebaseRef, "items");
},
componentDidMount: function() {
firebaseRef.set(["first", "second", "third"]);
},
componentDidUpdate: function(prevProps, prevState) {
console.log(JSON.stringify(this.state));
expect(this.state.items.map(function(item) { return item.$id; })).toEqual([0, 1, 2]);
expect(this.state.items.map(function(item) { return item.$value; })).toEqual(['first', 'second', 'third']);
done();
},
render: function() {
return React.DOM.div(null, "Testing");
}
});
React.render(new TestComponent(), document.body);
})
});

describe("bindAsObject():", function() {
Expand Down

0 comments on commit 4ef3840

Please sign in to comment.