Skip to content

Commit 94fd1c6

Browse files
committed
Unrevert revert DataSnapshot changes.
1 parent 35a1632 commit 94fd1c6

File tree

1 file changed

+137
-9
lines changed

1 file changed

+137
-9
lines changed

spec/v1/providers/database.spec.ts

Lines changed: 137 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,17 +569,52 @@ describe('Database Functions', () => {
569569
expect(subject.val()).to.equal(0);
570570
populate({ myKey: 0 });
571571
expect(subject.val()).to.deep.equal({ myKey: 0 });
572-
573-
// Null values are still reported as null.
574-
populate({ myKey: null });
575-
expect(subject.val()).to.deep.equal({ myKey: null });
576572
});
577573

578574
// Regression test: .val() was returning array of nulls when there's a property called length (BUG#37683995)
579575
it('should return correct values when data has "length" property', () => {
580576
populate({ length: 3, foo: 'bar' });
581577
expect(subject.val()).to.deep.equal({ length: 3, foo: 'bar' });
582578
});
579+
580+
it('should deal with null-values appropriately', () => {
581+
populate(null);
582+
expect(subject.val()).to.be.null;
583+
584+
populate({ myKey: null });
585+
expect(subject.val()).to.be.null;
586+
});
587+
588+
it('should deal with empty object values appropriately', () => {
589+
populate({});
590+
expect(subject.val()).to.be.null;
591+
592+
populate({ myKey: {} });
593+
expect(subject.val()).to.be.null;
594+
595+
populate({ myKey: { child: null } });
596+
expect(subject.val()).to.be.null;
597+
});
598+
599+
it('should deal with empty array values appropriately', () => {
600+
populate([]);
601+
expect(subject.val()).to.be.null;
602+
603+
populate({ myKey: [] });
604+
expect(subject.val()).to.be.null;
605+
606+
populate({ myKey: [null] });
607+
expect(subject.val()).to.be.null;
608+
609+
populate({ myKey: [{}] });
610+
expect(subject.val()).to.be.null;
611+
612+
populate({ myKey: [{ myKey: null }] });
613+
expect(subject.val()).to.be.null;
614+
615+
populate({ myKey: [{ myKey: {} }] });
616+
expect(subject.val()).to.be.null;
617+
});
583618
});
584619

585620
describe('#child(): DataSnapshot', () => {
@@ -606,14 +641,37 @@ describe('Database Functions', () => {
606641
});
607642

608643
it('should be false for a non-existent value', () => {
609-
populate({ a: { b: 'c' } });
644+
populate({ a: { b: 'c', nullChild: null } });
610645
expect(subject.child('d').exists()).to.be.false;
646+
expect(subject.child('nullChild').exists()).to.be.false;
611647
});
612648

613649
it('should be false for a value pathed beyond a leaf', () => {
614650
populate({ a: { b: 'c' } });
615651
expect(subject.child('a/b/c').exists()).to.be.false;
616652
});
653+
654+
it('should be false for an empty object value', () => {
655+
populate({ a: {} });
656+
expect(subject.child('a').exists()).to.be.false;
657+
658+
populate({ a: { child: null } });
659+
expect(subject.child('a').exists()).to.be.false;
660+
661+
populate({ a: { child: {} } });
662+
expect(subject.child('a').exists()).to.be.false;
663+
});
664+
665+
it('should be false for an empty array value', () => {
666+
populate({ a: [] });
667+
expect(subject.child('a').exists()).to.be.false;
668+
669+
populate({ a: [null] });
670+
expect(subject.child('a').exists()).to.be.false;
671+
672+
populate({ a: [{}] });
673+
expect(subject.child('a').exists()).to.be.false;
674+
});
617675
});
618676

619677
describe('#forEach(action: (a: DataSnapshot) => boolean): boolean', () => {
@@ -642,6 +700,17 @@ describe('Database Functions', () => {
642700

643701
expect(subject.forEach(counter)).to.equal(false);
644702
expect(count).to.eq(0);
703+
704+
populate({
705+
a: 'foo',
706+
nullChild: null,
707+
emptyObjectChild: {},
708+
emptyArrayChild: [],
709+
});
710+
count = 0;
711+
712+
expect(subject.forEach(counter)).to.equal(false);
713+
expect(count).to.eq(1);
645714
});
646715

647716
it('should cancel further enumeration if callback returns true', () => {
@@ -681,13 +750,51 @@ describe('Database Functions', () => {
681750

682751
describe('#numChildren()', () => {
683752
it('should be key count for objects', () => {
684-
populate({ a: 'b', c: 'd' });
753+
populate({
754+
a: 'b',
755+
c: 'd',
756+
nullChild: null,
757+
emptyObjectChild: {},
758+
emptyArrayChild: [],
759+
});
685760
expect(subject.numChildren()).to.eq(2);
686761
});
687762

688763
it('should be 0 for non-objects', () => {
689764
populate(23);
690765
expect(subject.numChildren()).to.eq(0);
766+
767+
populate({
768+
nullChild: null,
769+
emptyObjectChild: {},
770+
emptyArrayChild: [],
771+
});
772+
expect(subject.numChildren()).to.eq(0);
773+
});
774+
});
775+
776+
describe('#hasChildren()', () => {
777+
it('should true for objects', () => {
778+
populate({
779+
a: 'b',
780+
c: 'd',
781+
nullChild: null,
782+
emptyObjectChild: {},
783+
emptyArrayChild: [],
784+
});
785+
expect(subject.hasChildren()).to.be.true;
786+
});
787+
788+
it('should be false for non-objects', () => {
789+
populate(23);
790+
expect(subject.hasChildren()).to.be.false;
791+
792+
populate({
793+
nullChild: null,
794+
emptyObjectChild: {},
795+
emptyArrayChild: [],
796+
});
797+
expect(subject.hasChildren()).to.be.false;
691798
});
692799
});
693800

@@ -699,7 +806,17 @@ describe('Database Functions', () => {
699806
});
700807

701808
it('should return false if a child is missing', () => {
702-
populate({ a: 'b' });
809+
populate({
810+
a: 'b',
811+
nullChild: null,
812+
emptyObjectChild: {},
813+
emptyArrayChild: [],
814+
});
815+
expect(subject.hasChild('c')).to.be.false;
816+
expect(subject.hasChild('a/b')).to.be.false;
817+
expect(subject.hasChild('nullChild')).to.be.false;
818+
expect(subject.hasChild('emptyObjectChild')).to.be.false;
819+
expect(subject.hasChild('emptyArrayChild')).to.be.false;
703820
expect(subject.hasChild('c')).to.be.false;
704821
expect(subject.hasChild('a/b')).to.be.false;
705822
});
@@ -731,11 +848,22 @@ describe('Database Functions', () => {
731848

732849
describe('#toJSON(): Object', () => {
733850
it('should return the current value', () => {
734-
populate({ a: 'b' });
851+
populate({
852+
a: 'b',
853+
nullChild: null,
854+
emptyObjectChild: {},
855+
emptyArrayChild: [],
856+
});
735857
expect(subject.toJSON()).to.deep.equal(subject.val());
736858
});
859+
737860
it('should be stringifyable', () => {
738-
populate({ a: 'b' });
861+
populate({
862+
a: 'b',
863+
nullChild: null,
864+
emptyObjectChild: {},
865+
emptyArrayChild: [],
866+
});
739867
expect(JSON.stringify(subject)).to.deep.equal('{"a":"b"}');
740868
});
741869
});

0 commit comments

Comments
 (0)