Skip to content

Commit 7da0bea

Browse files
test(firestore): variable implementation
1 parent bfc227e commit 7da0bea

5 files changed

Lines changed: 28 additions & 21 deletions

File tree

.github/scripts/compare-types/configs/firestore-pipelines.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ const config: PackageConfig = {
123123
name: 'timestampExtract',
124124
reason: 'Newer firebase-js-sdk timestamp expression helper not yet exposed by RN Firebase pipelines.',
125125
},
126-
{
127-
name: 'variable',
128-
reason: 'Newer firebase-js-sdk variable expression helper not yet exposed by RN Firebase pipelines.',
129-
},
130126
{
131127
name: 'DefineStageOptions',
132128
reason: 'Newer firebase-js-sdk stage options type not yet exposed by RN Firebase pipelines.',
@@ -165,7 +161,7 @@ const config: PackageConfig = {
165161
},
166162
{
167163
name: 'ExpressionType',
168-
reason: 'RN Firebase has not yet exposed the newer firebase-js-sdk `Variable` and `PipelineValue` expression kinds.',
164+
reason: 'RN Firebase has not yet exposed the newer firebase-js-sdk `PipelineValue` expression kind.',
169165
},
170166
{
171167
name: 'StageOptions',

packages/firestore/__tests__/pipelines-web.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jest.mock('@react-native-firebase/app/dist/module/internal/web/firebaseFirestore
1515
conditional: jest.fn(actual.conditional as (...args: unknown[]) => unknown),
1616
isType: jest.fn(actual.isType as (...args: unknown[]) => unknown),
1717
mapGet: jest.fn(actual.mapGet as (...args: unknown[]) => unknown),
18+
variable: jest.fn(actual.variable as (...args: unknown[]) => unknown),
1819
euclideanDistance: jest.fn(actual.euclideanDistance as (...args: unknown[]) => unknown),
1920
};
2021
});
@@ -70,7 +71,7 @@ describe('Firestore web pipeline bridge', function () {
7071
exprType: 'Function',
7172
name: 'greaterThan',
7273
args: [
73-
{ __kind: 'expression', exprType: 'Field', path: 'rating' },
74+
{ __kind: 'expression', exprType: 'Variable', name: 'score' },
7475
{ __kind: 'expression', exprType: 'Constant', value: 3 },
7576
],
7677
},
@@ -124,6 +125,7 @@ describe('Firestore web pipeline bridge', function () {
124125
const whereArg = (pipelineInstance.where as jest.Mock).mock.calls[0][0] as any;
125126
expect(whereArg).toBeDefined();
126127
expect(whereArg.__kind).toBeUndefined();
128+
expect(firebaseFirestorePipelines.variable).toHaveBeenCalledWith('score');
127129

128130
const selectArg = (pipelineInstance.select as jest.Mock).mock.calls[0][0] as any;
129131
expect(selectArg).toBeDefined();

packages/firestore/__tests__/pipelines.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
timestampAdd,
1818
timestampSubtract,
1919
trunc,
20+
variable,
2021
} from '../lib/pipelines';
2122
import '../lib/pipelines';
2223
import { ConstantExpression } from '../lib/pipelines/expressions';
@@ -148,10 +149,10 @@ describe('Firestore pipelines runtime', function () {
148149
.pipeline()
149150
.collection('firestore')
150151
.select(
151-
arrayFilter('scores', 'score', greaterThan(constant(1), constant(0))).as(
152+
arrayFilter('scores', 'score', greaterThan(variable('score'), constant(15))).as(
152153
'passingScores',
153154
),
154-
field('scores').arrayFilter('score', greaterThan(constant(2), constant(1))).as(
155+
field('scores').arrayFilter('score', greaterThan(variable('score'), constant(20))).as(
155156
'topScores',
156157
),
157158
)
@@ -173,8 +174,8 @@ describe('Firestore pipelines runtime', function () {
173174
exprType: 'Function',
174175
name: 'greaterThan',
175176
args: [
176-
{ exprType: 'Constant', value: 1 },
177-
{ exprType: 'Constant', value: 0 },
177+
{ exprType: 'Variable', name: 'score' },
178+
{ exprType: 'Constant', value: 15 },
178179
],
179180
},
180181
],
@@ -192,8 +193,8 @@ describe('Firestore pipelines runtime', function () {
192193
exprType: 'Function',
193194
name: 'greaterThan',
194195
args: [
195-
{ exprType: 'Constant', value: 2 },
196-
{ exprType: 'Constant', value: 1 },
196+
{ exprType: 'Variable', name: 'score' },
197+
{ exprType: 'Constant', value: 20 },
197198
],
198199
},
199200
],

packages/firestore/e2e/Pipeline.e2e.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,7 @@ describe('FirestorePipeline', function () {
15211521
arrayConcat,
15221522
arrayFilter,
15231523
arraySum,
1524+
variable,
15241525
and,
15251526
greaterThan,
15261527
arrayContains,
@@ -1565,7 +1566,7 @@ describe('FirestorePipeline', function () {
15651566
arrayLength(field('tags')).as('tagCount'),
15661567
arrayGet(field('items'), 0).as('firstItem'),
15671568
arrayConcat(field('primaryTags'), field('secondaryTags')).as('allTags'),
1568-
arrayFilter(field('items'), 'item', greaterThan(constant(1), constant(0))).as(
1569+
arrayFilter(field('scores'), 'score', greaterThan(variable('score'), 15)).as(
15691570
'filteredItems',
15701571
),
15711572
arraySum(field('scores')).as('totalScore'),
@@ -1589,7 +1590,7 @@ describe('FirestorePipeline', function () {
15891590
array([constant(1), constant(2), constant(3)]).as('fixedArr'),
15901591
arrayLength(field('tags')).as('tagCount'),
15911592
arrayConcat(field('primaryTags'), field('secondaryTags')).as('allTags'),
1592-
arrayFilter('items', 'item', greaterThan(constant(1), constant(0))).as(
1593+
arrayFilter('scores', 'score', greaterThan(variable('score'), 15)).as(
15931594
'filteredItems',
15941595
),
15951596
arraySum(field('scores')).as('totalScore'),
@@ -1601,7 +1602,7 @@ describe('FirestorePipeline', function () {
16011602
iosData.fixedArr.should.eql([1, 2, 3]);
16021603
iosData.tagCount.should.equal(2);
16031604
iosData.allTags.should.eql(['a', 'b', 'c', 'd']);
1604-
iosData.filteredItems.should.eql(['x', 'y', 'z']);
1605+
iosData.filteredItems.should.eql([20, 30]);
16051606
iosData.totalScore.should.equal(60);
16061607
return;
16071608
}
@@ -1614,7 +1615,7 @@ describe('FirestorePipeline', function () {
16141615
data.tagCount.should.equal(2);
16151616
data.firstItem.should.equal('x');
16161617
data.allTags.should.eql(['a', 'b', 'c', 'd']);
1617-
data.filteredItems.should.eql(['x', 'y', 'z']);
1618+
data.filteredItems.should.eql([20, 30]);
16181619
data.totalScore.should.equal(60);
16191620
});
16201621
});

packages/firestore/type-test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ import {
167167
collectionId,
168168
type as pipelineType,
169169
currentTimestamp,
170+
variable,
170171
// array
171172
array,
172173
arrayFilter,
@@ -1313,10 +1314,12 @@ void currentTimestamp();
13131314
// array
13141315
void array([1, 2, 3]);
13151316
void array([field('a'), constant(2)]);
1317+
// variable: (string) => Expression
1318+
void variable('score');
13161319
// arrayFilter: (string, alias, BooleanExpression) | (Expression, alias, BooleanExpression)
1317-
void arrayFilter('scores', 'score', greaterThan(constant(1), constant(0)));
1318-
void arrayFilter(field('scores'), 'score', greaterThan(constant(1), constant(0)));
1319-
void field('scores').arrayFilter('score', greaterThan(constant(1), constant(0)));
1320+
void arrayFilter('scores', 'score', greaterThan(variable('score'), constant(15)));
1321+
void arrayFilter(field('scores'), 'score', greaterThan(variable('score'), constant(15)));
1322+
void field('scores').arrayFilter('score', greaterThan(variable('score'), constant(15)));
13201323
// arrayConcat: (Expression, ...) | (string, ...)
13211324
void arrayConcat(field('tags'), field('moreTags'));
13221325
void arrayConcat(field('tags'), ['extra']);
@@ -1696,8 +1699,12 @@ const pipelineArrayOps = xDb
16961699
arrayGet('items', 0).as('firstItem2'),
16971700
arrayConcat(field('primaryTags'), field('secondaryTags')).as('allTags'),
16981701
arrayConcat('primaryTags', ['extra']).as('allTags2'),
1699-
arrayFilter('scores', 'score', greaterThan(constant(1), constant(0))).as('passingScores'),
1700-
field('scores').arrayFilter('score', greaterThan(constant(2), constant(1))).as('topScores'),
1702+
arrayFilter('scores', 'score', greaterThan(variable('score'), constant(15))).as(
1703+
'passingScores',
1704+
),
1705+
field('scores').arrayFilter('score', greaterThan(variable('score'), constant(20))).as(
1706+
'topScores',
1707+
),
17011708
arraySum(field('scores')).as('totalScore'),
17021709
arraySum('scores').as('totalScore2'),
17031710
);

0 commit comments

Comments
 (0)