Skip to content

Commit ec514e2

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

File tree

1 file changed

+134
-9
lines changed

1 file changed

+134
-9
lines changed

spec/v1/providers/database.spec.ts

+134-9
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,17 @@ describe('Database Functions', () => {
614672

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

619688
it('should cancel further enumeration if callback returns true', () => {
@@ -653,13 +722,51 @@ describe('Database Functions', () => {
653722

654723
describe('#numChildren()', () => {
655724
it('should be key count for objects', () => {
656-
populate({ a: 'b', c: 'd' });
725+
populate({
726+
a: 'b',
727+
c: 'd',
728+
nullChild: null,
729+
emptyObjectChild: {},
730+
emptyArrayChild: [],
731+
});
657732
expect(subject.numChildren()).to.eq(2);
658733
});
659734

660735
it('should be 0 for non-objects', () => {
661736
populate(23);
662737
expect(subject.numChildren()).to.eq(0);
738+
739+
populate({
740+
nullChild: null,
741+
emptyObjectChild: {},
742+
emptyArrayChild: [],
743+
});
744+
expect(subject.numChildren()).to.eq(0);
745+
});
746+
});
747+
748+
describe('#hasChildren()', () => {
749+
it('should true for objects', () => {
750+
populate({
751+
a: 'b',
752+
c: 'd',
753+
nullChild: null,
754+
emptyObjectChild: {},
755+
emptyArrayChild: [],
756+
});
757+
expect(subject.hasChildren()).to.be.true;
758+
});
759+
760+
it('should be false for non-objects', () => {
761+
populate(23);
762+
expect(subject.hasChildren()).to.be.false;
763+
764+
populate({
765+
nullChild: null,
766+
emptyObjectChild: {},
767+
emptyArrayChild: [],
768+
});
769+
expect(subject.hasChildren()).to.be.false;
663770
});
664771
});
665772

@@ -671,9 +778,17 @@ describe('Database Functions', () => {
671778
});
672779

673780
it('should return false if a child is missing', () => {
674-
populate({ a: 'b' });
781+
populate({
782+
a: 'b',
783+
nullChild: null,
784+
emptyObjectChild: {},
785+
emptyArrayChild: [],
786+
});
675787
expect(subject.hasChild('c')).to.be.false;
676788
expect(subject.hasChild('a/b')).to.be.false;
789+
expect(subject.hasChild('nullChild')).to.be.false;
790+
expect(subject.hasChild('emptyObjectChild')).to.be.false;
791+
expect(subject.hasChild('emptyArrayChild')).to.be.false;
677792
});
678793
});
679794

@@ -703,11 +818,21 @@ describe('Database Functions', () => {
703818

704819
describe('#toJSON(): Object', () => {
705820
it('should return the current value', () => {
706-
populate({ a: 'b' });
821+
populate({
822+
a: 'b',
823+
nullChild: null,
824+
emptyObjectChild: {},
825+
emptyArrayChild: [],
826+
});
707827
expect(subject.toJSON()).to.deep.equal(subject.val());
708828
});
709829
it('should be stringifyable', () => {
710-
populate({ a: 'b' });
830+
populate({
831+
a: 'b',
832+
nullChild: null,
833+
emptyObjectChild: {},
834+
emptyArrayChild: [],
835+
});
711836
expect(JSON.stringify(subject)).to.deep.equal('{"a":"b"}');
712837
});
713838
});

0 commit comments

Comments
 (0)