diff --git a/src/firestore-query.js b/src/firestore-query.js index e2b275db..5e957965 100644 --- a/src/firestore-query.js +++ b/src/firestore-query.js @@ -126,7 +126,7 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) { var query; // check if unsupported operator - if (operator !== '==') { + if (operator !== '==' && operator != 'array-contains') { console.warn('Using unsupported where() operator for firebase-mock, returning entire dataset'); return this; } else { @@ -139,6 +139,11 @@ MockFirestoreQuery.prototype.where = function (property, operator, value) { results[key] = _.cloneDeep(data); } break; + case 'array-contains': + if (_.includes(_.get(data, property), value)) { + results[key] = _.cloneDeep(data); + } + break; default: results[key] = _.cloneDeep(data); break; diff --git a/test/unit/data.json b/test/unit/data.json index dd566989..6b2ae7ab 100644 --- a/test/unit/data.json +++ b/test/unit/data.json @@ -90,6 +90,7 @@ "b": { "name": "b", "name_type": "string", + "array": ["x"], "complex": { "name": "b" } @@ -97,6 +98,7 @@ "c": { "name": "c", "name_type": "string", + "array": ["y"], "complex": { "name": "c" } @@ -104,12 +106,14 @@ "1": { "name": 1, "name_type": "number", + "array": ["xx"], "complex": { "name": 1 } }, "2": { "name": 2, + "array": ["x", "y"], "name_type": "number", "complex": { "name": 2 @@ -119,7 +123,8 @@ "name": 3, "name_type": "number", "complex": { - "name": 3 + "name": 3, + "array": ["x"] } } }, diff --git a/test/unit/firestore-collection.js b/test/unit/firestore-collection.js index 2e4c8d47..ad69f52c 100644 --- a/test/unit/firestore-collection.js +++ b/test/unit/firestore-collection.js @@ -215,6 +215,8 @@ describe('MockFirestoreCollection', function () { var results5 = collection.where('name_type', '==', 'number').get(); var results6 = collection.where('name_type', '==', 'abc').get(); var results7 = collection.where('value', '==', 3).get(); + var results8 = collection.where('array', 'array-contains', 'x').get(); + var results9 = collection.where('array', 'array-contains', 'z').get(); db.flush(); return Promise.all([ @@ -224,7 +226,9 @@ describe('MockFirestoreCollection', function () { expect(results4).to.eventually.have.property('size').to.equal(3), expect(results5).to.eventually.have.property('size').to.equal(3), expect(results6).to.eventually.have.property('size').to.equal(0), - expect(results7).to.eventually.have.property('size').to.equal(0) + expect(results7).to.eventually.have.property('size').to.equal(0), + expect(results8).to.eventually.have.property('size').to.equal(2), + expect(results9).to.eventually.have.property('size').to.equal(0) ]); });