Skip to content

Commit

Permalink
Merge branch 'master' into onIdTokenChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurvihill committed Dec 19, 2019
2 parents 90f35ab + a1957f1 commit e92a4ba
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
one.
- `DataSnapshot.child` now correctly splits child paths by '/'
characters
- `Query.startAt`, `Query.endAt`, and `Query.equalTo` now correctly
accept boolean parameters.
- Boolean values are now allowed in RTDB priority fields and as
arguments to `Query.startAt`, `Query.endAt`, and `Query.equalTo`.


[Unreleased]: https://github.com/dmurvihill/firebase-mock/compare/v2.2.10...HEAD
10 changes: 8 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@ exports.priAndKeyComparator = function priAndKeyComparator(testPri, testKey, val
};

exports.priorityComparator = function priorityComparator(a, b) {
// https://firebase.google.com/docs/database/web/lists-of-data#data-order
if (a !== b) {
if (a === null || b === null) {
return a === null ? -1 : 1;
}
if(typeof a === 'boolean' && typeof b === 'boolean') {
return !a ? -1 : 1;
}
if (typeof a !== typeof b) {
if(typeof a === 'boolean' || typeof b === 'boolean') {
return typeof a === 'boolean' ? -1 : 1;
}
return typeof a === 'number' ? -1 : 1;
} else {
return a > b ? 1 : -1;
}
return a > b ? 1 : -1;
}
return 0;
};
Expand Down
8 changes: 8 additions & 0 deletions test/unit/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@
"char_c": {
".priority": "c",
"aLetter": "c"
},
"bool_true": {
".priority": true,
"aBoolean": true
},
"bool_false": {
".priority": false,
"aBoolean": false
}
},
"collections": {
Expand Down
14 changes: 10 additions & 4 deletions test/unit/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ describe('MockQuery', function () {
expect(spy.called).to.equal(true);
});

it('should work with boolean equalTo', function() {
var spy = sinon.spy(function(snap) {
expect(_.keys(snap.val())).eql(['bool_true']);
});

ref.equalTo(true).on('value', spy);
ref.flush();
expect(spy).callCount(1);
});

it('should return null if not equalTo', function() {
var spy = sinon.spy(function(snap) {
expect(snap.val()).equals(null);
Expand Down Expand Up @@ -345,8 +355,4 @@ describe('MockQuery', function () {

it('should start at the key+priority given');
});

describe('equalTo', function() {
it('should start and stop at the key given');
});
});
40 changes: 40 additions & 0 deletions test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var removeEmptyRtdbProperties = require('../../src/utils').removeEmptyRtdbProper
var removeEmptyFirestoreProperties = require('../../src/utils').removeEmptyFirestoreProperties;
var updateToRtdbObject = require('../../src/utils').updateToRtdbObject;
var updateToFirestoreObject = require('../../src/utils').updateToFirestoreObject;
var priorityComparator = require('../../src/utils').priorityComparator;

describe('utils', function () {
describe('removeEmptyRtdbProperties', function () {
Expand Down Expand Up @@ -164,4 +165,43 @@ describe('utils', function () {
});
});
});

describe('priorityComparator', function() {
// https://firebase.google.com/docs/database/web/lists-of-data#data-order
it('should give no priority to equal items', function() {
expect(priorityComparator('a', 'a')).to.eql(0);
});
it('should order null items first', function() {
expect(priorityComparator(null, 0)).to.eql(-1);
expect(priorityComparator(0, null)).to.eql(1);
});
it('should order false before true', function() {
expect(priorityComparator(false, true)).to.eql(-1);
expect(priorityComparator(true, false)).to.eql(1);
});
it('should order booleans before numbers', function() {
expect(priorityComparator(false, 0)).to.eql(-1);
expect(priorityComparator(true, 1)).to.eql(-1);
expect(priorityComparator(0, false)).to.eql(1);
expect(priorityComparator(1, true)).to.eql(1);
});
it('should order numbers before strings', function() {
expect(priorityComparator(5, 'foo')).to.eql(-1);
expect(priorityComparator(3, '3')).to.eql(-1);
expect(priorityComparator('foo', 0)).to.eql(1);
expect(priorityComparator('10', 10)).to.eql(1);
});
it('should order numbers in ascending order', function() {
expect(priorityComparator(4,5)).to.eql(-1);
expect(priorityComparator(5,4)).to.eql(1);
expect(priorityComparator(1,10)).to.eql(-1);
expect(priorityComparator(10,1)).to.eql(1);
});
it('should order strings lexicographically', function() {
expect(priorityComparator('bar', 'foo')).to.eql(-1);
expect(priorityComparator('foo', 'bar')).to.eql(1);
expect(priorityComparator('ant', 'art')).to.eql(-1);
expect(priorityComparator('art', 'ant')).to.eql(1);
});
});
});

0 comments on commit e92a4ba

Please sign in to comment.