Skip to content

Commit 8a8481e

Browse files
committed
Test cases for DataSnapshot to align methods closer to @firebase/database
1 parent 0083fc7 commit 8a8481e

File tree

1 file changed

+94
-9
lines changed

1 file changed

+94
-9
lines changed

spec/v1/providers/database.spec.ts

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -541,17 +541,52 @@ describe('Database Functions', () => {
541541
expect(subject.val()).to.equal(0);
542542
populate({ myKey: 0 });
543543
expect(subject.val()).to.deep.equal({ myKey: 0 });
544-
545-
// Null values are still reported as null.
546-
populate({ myKey: null });
547-
expect(subject.val()).to.deep.equal({ myKey: null });
548544
});
549545

550546
// Regression test: .val() was returning array of nulls when there's a property called length (BUG#37683995)
551547
it('should return correct values when data has "length" property', () => {
552548
populate({ length: 3, foo: 'bar' });
553549
expect(subject.val()).to.deep.equal({ length: 3, foo: 'bar' });
554550
});
551+
552+
it('should deal with null-values appropriately', () => {
553+
populate(null);
554+
expect(subject.val()).to.be.null;
555+
556+
populate({ myKey: null });
557+
expect(subject.val()).to.be.null;
558+
});
559+
560+
it('should deal with empty object values appropriately', () => {
561+
populate({});
562+
expect(subject.val()).to.be.null;
563+
564+
populate({ myKey: {} });
565+
expect(subject.val()).to.be.null;
566+
567+
populate({ myKey: { child: null } });
568+
expect(subject.val()).to.be.null;
569+
});
570+
571+
it('should deal with empty array values appropriately', () => {
572+
populate([]);
573+
expect(subject.val()).to.be.null;
574+
575+
populate({ myKey: [] });
576+
expect(subject.val()).to.be.null;
577+
578+
populate({ myKey: [null] });
579+
expect(subject.val()).to.be.null;
580+
581+
populate({ myKey: [{}] });
582+
expect(subject.val()).to.be.null;
583+
584+
populate({ myKey: [{ myKey: null }] });
585+
expect(subject.val()).to.be.null;
586+
587+
populate({ myKey: [{ myKey: {} }] });
588+
expect(subject.val()).to.be.null;
589+
});
555590
});
556591

557592
describe('#child(): DataSnapshot', () => {
@@ -578,14 +613,37 @@ describe('Database Functions', () => {
578613
});
579614

580615
it('should be false for a non-existent value', () => {
581-
populate({ a: { b: 'c' } });
616+
populate({ a: { b: 'c', nullChild: null } });
582617
expect(subject.child('d').exists()).to.be.false;
618+
expect(subject.child('nullChild').exists()).to.be.false;
583619
});
584620

585621
it('should be false for a value pathed beyond a leaf', () => {
586622
populate({ a: { b: 'c' } });
587623
expect(subject.child('a/b/c').exists()).to.be.false;
588624
});
625+
626+
it('should be false for an empty object value', () => {
627+
populate({ a: {} });
628+
expect(subject.child('a').exists()).to.be.false;
629+
630+
populate({ a: { child: null } });
631+
expect(subject.child('a').exists()).to.be.false;
632+
633+
populate({ a: { child: {} } });
634+
expect(subject.child('a').exists()).to.be.false;
635+
});
636+
637+
it('should be false for an empty array value', () => {
638+
populate({ a: [] });
639+
expect(subject.child('a').exists()).to.be.false;
640+
641+
populate({ a: [null] });
642+
expect(subject.child('a').exists()).to.be.false;
643+
644+
populate({ a: [{}] });
645+
expect(subject.child('a').exists()).to.be.false;
646+
});
589647
});
590648

591649
describe('#forEach(action: (a: DataSnapshot) => boolean): boolean', () => {
@@ -614,6 +672,12 @@ describe('Database Functions', () => {
614672

615673
expect(subject.forEach(counter)).to.equal(false);
616674
expect(count).to.eq(0);
675+
676+
populate({ a: 'foo', nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
677+
count = 0;
678+
679+
expect(subject.forEach(counter)).to.equal(false);
680+
expect(count).to.eq(1);
617681
});
618682

619683
it('should cancel further enumeration if callback returns true', () => {
@@ -653,13 +717,31 @@ describe('Database Functions', () => {
653717

654718
describe('#numChildren()', () => {
655719
it('should be key count for objects', () => {
656-
populate({ a: 'b', c: 'd' });
720+
populate({ a: 'b', c: 'd', nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
657721
expect(subject.numChildren()).to.eq(2);
658722
});
659723

660724
it('should be 0 for non-objects', () => {
661725
populate(23);
662726
expect(subject.numChildren()).to.eq(0);
727+
728+
populate({ nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
729+
expect(subject.numChildren()).to.eq(0);
730+
});
731+
});
732+
733+
describe('#hasChildren()', () => {
734+
it('should true for objects', () => {
735+
populate({ a: 'b', c: 'd', nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
736+
expect(subject.hasChildren()).to.be.true;
737+
});
738+
739+
it('should be false for non-objects', () => {
740+
populate(23);
741+
expect(subject.hasChildren()).to.be.false;
742+
743+
populate({ nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
744+
expect(subject.hasChildren()).to.be.false;
663745
});
664746
});
665747

@@ -671,9 +753,12 @@ describe('Database Functions', () => {
671753
});
672754

673755
it('should return false if a child is missing', () => {
674-
populate({ a: 'b' });
756+
populate({ a: 'b', nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
675757
expect(subject.hasChild('c')).to.be.false;
676758
expect(subject.hasChild('a/b')).to.be.false;
759+
expect(subject.hasChild('nullChild')).to.be.false;
760+
expect(subject.hasChild('emptyObjectChild')).to.be.false;
761+
expect(subject.hasChild('emptyArrayChild')).to.be.false;
677762
});
678763
});
679764

@@ -703,11 +788,11 @@ describe('Database Functions', () => {
703788

704789
describe('#toJSON(): Object', () => {
705790
it('should return the current value', () => {
706-
populate({ a: 'b' });
791+
populate({ a: 'b', nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
707792
expect(subject.toJSON()).to.deep.equal(subject.val());
708793
});
709794
it('should be stringifyable', () => {
710-
populate({ a: 'b' });
795+
populate({ a: 'b', nullChild: null, emptyObjectChild: {}, emptyArrayChild: [] });
711796
expect(JSON.stringify(subject)).to.deep.equal('{"a":"b"}');
712797
});
713798
});

0 commit comments

Comments
 (0)