Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit 6047baa

Browse files
committed
Add support for "array-contains" where operator.
1 parent 8fe31b9 commit 6047baa

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/firestore-query.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) {
126126
var query;
127127

128128
// check if unsupported operator
129-
if (operator !== '==') {
129+
if (['==', 'array-contains'].indexOf(operator) === -1) {
130130
console.warn('Using unsupported where() operator for firebase-mock, returning entire dataset');
131131
return this;
132132
} else {
@@ -139,6 +139,12 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) {
139139
results[key] = _.cloneDeep(data);
140140
}
141141
break;
142+
case 'array-contains':
143+
var dt = _.get(data, property);
144+
if (Array.isArray(dt) && dt.indexOf(value) > -1) {
145+
results[key] = _.cloneDeep(data);
146+
}
147+
break;
142148
default:
143149
results[key] = _.cloneDeep(data);
144150
break;

test/unit/data.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,30 @@
8585
"name_type": "string",
8686
"complex": {
8787
"name": "a"
88-
}
88+
},
89+
"array": [
90+
"a", "b"
91+
]
8992
},
9093
"b": {
9194
"name": "b",
9295
"name_type": "string",
9396
"complex": {
9497
"name": "b"
95-
}
98+
},
99+
"array": [
100+
"a", "c"
101+
]
96102
},
97103
"c": {
98104
"name": "c",
99105
"name_type": "string",
100106
"complex": {
101107
"name": "c"
102-
}
108+
},
109+
"array": [
110+
"b", "c"
111+
]
103112
},
104113
"1": {
105114
"name": 1,

test/unit/firestore-collection.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ describe('MockFirestoreCollection', function () {
228228
]);
229229
});
230230

231+
it('returns matched documents for operator "array-contains"', function() {
232+
var results1 = collection.where('array', 'array-contains', 'a').get();
233+
db.flush();
234+
235+
return Promise.all([
236+
expect(results1).to.eventually.have.property('size').to.equal(2),
237+
]);
238+
});
239+
231240
it('returns all documents when using unsupported operator', function() {
232241
var expected = 6;
233242

0 commit comments

Comments
 (0)