Skip to content

Commit e92a4ba

Browse files
committed
Merge branch 'master' into onIdTokenChanged
2 parents 90f35ab + a1957f1 commit e92a4ba

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
one.
2929
- `DataSnapshot.child` now correctly splits child paths by '/'
3030
characters
31-
- `Query.startAt`, `Query.endAt`, and `Query.equalTo` now correctly
32-
accept boolean parameters.
31+
- Boolean values are now allowed in RTDB priority fields and as
32+
arguments to `Query.startAt`, `Query.endAt`, and `Query.equalTo`.
3333

3434

3535
[Unreleased]: https://github.com/dmurvihill/firebase-mock/compare/v2.2.10...HEAD

src/utils.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,21 @@ exports.priAndKeyComparator = function priAndKeyComparator(testPri, testKey, val
6262
};
6363

6464
exports.priorityComparator = function priorityComparator(a, b) {
65+
// https://firebase.google.com/docs/database/web/lists-of-data#data-order
6566
if (a !== b) {
6667
if (a === null || b === null) {
6768
return a === null ? -1 : 1;
6869
}
70+
if(typeof a === 'boolean' && typeof b === 'boolean') {
71+
return !a ? -1 : 1;
72+
}
6973
if (typeof a !== typeof b) {
74+
if(typeof a === 'boolean' || typeof b === 'boolean') {
75+
return typeof a === 'boolean' ? -1 : 1;
76+
}
7077
return typeof a === 'number' ? -1 : 1;
71-
} else {
72-
return a > b ? 1 : -1;
7378
}
79+
return a > b ? 1 : -1;
7480
}
7581
return 0;
7682
};

test/unit/data.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@
7777
"char_c": {
7878
".priority": "c",
7979
"aLetter": "c"
80+
},
81+
"bool_true": {
82+
".priority": true,
83+
"aBoolean": true
84+
},
85+
"bool_false": {
86+
".priority": false,
87+
"aBoolean": false
8088
}
8189
},
8290
"collections": {

test/unit/query.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ describe('MockQuery', function () {
146146
expect(spy.called).to.equal(true);
147147
});
148148

149+
it('should work with boolean equalTo', function() {
150+
var spy = sinon.spy(function(snap) {
151+
expect(_.keys(snap.val())).eql(['bool_true']);
152+
});
153+
154+
ref.equalTo(true).on('value', spy);
155+
ref.flush();
156+
expect(spy).callCount(1);
157+
});
158+
149159
it('should return null if not equalTo', function() {
150160
var spy = sinon.spy(function(snap) {
151161
expect(snap.val()).equals(null);
@@ -345,8 +355,4 @@ describe('MockQuery', function () {
345355

346356
it('should start at the key+priority given');
347357
});
348-
349-
describe('equalTo', function() {
350-
it('should start and stop at the key given');
351-
});
352358
});

test/unit/utils.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var removeEmptyRtdbProperties = require('../../src/utils').removeEmptyRtdbProper
77
var removeEmptyFirestoreProperties = require('../../src/utils').removeEmptyFirestoreProperties;
88
var updateToRtdbObject = require('../../src/utils').updateToRtdbObject;
99
var updateToFirestoreObject = require('../../src/utils').updateToFirestoreObject;
10+
var priorityComparator = require('../../src/utils').priorityComparator;
1011

1112
describe('utils', function () {
1213
describe('removeEmptyRtdbProperties', function () {
@@ -164,4 +165,43 @@ describe('utils', function () {
164165
});
165166
});
166167
});
168+
169+
describe('priorityComparator', function() {
170+
// https://firebase.google.com/docs/database/web/lists-of-data#data-order
171+
it('should give no priority to equal items', function() {
172+
expect(priorityComparator('a', 'a')).to.eql(0);
173+
});
174+
it('should order null items first', function() {
175+
expect(priorityComparator(null, 0)).to.eql(-1);
176+
expect(priorityComparator(0, null)).to.eql(1);
177+
});
178+
it('should order false before true', function() {
179+
expect(priorityComparator(false, true)).to.eql(-1);
180+
expect(priorityComparator(true, false)).to.eql(1);
181+
});
182+
it('should order booleans before numbers', function() {
183+
expect(priorityComparator(false, 0)).to.eql(-1);
184+
expect(priorityComparator(true, 1)).to.eql(-1);
185+
expect(priorityComparator(0, false)).to.eql(1);
186+
expect(priorityComparator(1, true)).to.eql(1);
187+
});
188+
it('should order numbers before strings', function() {
189+
expect(priorityComparator(5, 'foo')).to.eql(-1);
190+
expect(priorityComparator(3, '3')).to.eql(-1);
191+
expect(priorityComparator('foo', 0)).to.eql(1);
192+
expect(priorityComparator('10', 10)).to.eql(1);
193+
});
194+
it('should order numbers in ascending order', function() {
195+
expect(priorityComparator(4,5)).to.eql(-1);
196+
expect(priorityComparator(5,4)).to.eql(1);
197+
expect(priorityComparator(1,10)).to.eql(-1);
198+
expect(priorityComparator(10,1)).to.eql(1);
199+
});
200+
it('should order strings lexicographically', function() {
201+
expect(priorityComparator('bar', 'foo')).to.eql(-1);
202+
expect(priorityComparator('foo', 'bar')).to.eql(1);
203+
expect(priorityComparator('ant', 'art')).to.eql(-1);
204+
expect(priorityComparator('art', 'ant')).to.eql(1);
205+
});
206+
});
167207
});

0 commit comments

Comments
 (0)