Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions spec/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('DeltaSnapshot', () => {
});
});

describe('#forEach(childAction: Function)', () => {
describe('#forEach(action: (a: DeltaSnapshot) => boolean): boolean', () => {
it('should iterate through child snapshots', () => {
populate({ a: 'b' }, { c: 'd' });
let out = '';
Expand All @@ -223,12 +223,46 @@ describe('DeltaSnapshot', () => {
let count = 0;
let counter = snap => count++;

subject.forEach(counter);
expect(subject.forEach(counter)).to.equal(false);
populate(23, null);

subject.forEach(counter);
expect(subject.forEach(counter)).to.equal(false);
expect(count).to.eq(0);
});

it('should cancel further enumeration if callback returns true', () => {
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
let out = '';
const ret = subject.forEach(snap => {
if (snap.val() === 'f') {
return true;
}
out += snap.val();
});
expect(out).to.equal('bd');
expect(ret).to.equal(true);
});

it('should not cancel further enumeration if callback returns a truthy value', () => {
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
let out = '';
const ret = subject.forEach(snap => {
out += snap.val();
return 1;
});
expect(out).to.equal('bdfh');
expect(ret).to.equal(false);
});

it('should not cancel further enumeration if callback does not return', () => {
populate(null, { a: 'b', c: 'd', e: 'f', g: 'h' });
let out = '';
const ret = subject.forEach(snap => {
out += snap.val();
});
expect(out).to.equal('bdfh');
expect(ret).to.equal(false);
});
});

describe('#numChildren()', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,10 @@ export class DeltaSnapshot implements firebase.database.DataSnapshot {
return valAt(this._delta, this._childPath) !== undefined;
}

// TODO(inlined) what is this boolean for?
forEach(action: (a: DeltaSnapshot) => boolean): boolean {
let val = this.val();
if (_.isPlainObject(val)) {
_.keys(val).forEach(key => action(this.child(key)));
return _.some(val, (value, key: string) => action(this.child(key)) === true);
}
return false;
}
Expand Down