Skip to content

Commit 1869f65

Browse files
committed
add a test
1 parent 32bf021 commit 1869f65

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

packages/firestore/src/model/values.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
isServerTimestamp
4242
} from './server_timestamps';
4343
import { TypeOrder } from './type_order';
44+
import {ByteString} from "../util/byte_string";
4445

4546
export const TYPE_KEY = '__type__';
4647
const MAX_VALUE_TYPE = '__max__';
@@ -251,7 +252,9 @@ export function valueCompare(left: Value, right: Value): number {
251252
getLocalWriteTime(right)
252253
);
253254
case TypeOrder.StringValue:
254-
return primitiveComparator(left.stringValue!, right.stringValue!);
255+
return compareBlobs(stringValueToUint8Array(left.stringValue!),stringValueToUint8Array(right.stringValue!))
256+
// return compareBlobs(left.stringValue!, right.stringValue!);
257+
// return primitiveComparator(left.stringValue!, right.stringValue!);
255258
case TypeOrder.BlobValue:
256259
return compareBlobs(left.bytesValue!, right.bytesValue!);
257260
case TypeOrder.RefValue:
@@ -269,6 +272,12 @@ export function valueCompare(left: Value, right: Value): number {
269272
}
270273
}
271274

275+
function stringValueToUint8Array(stringValue: string): Uint8Array {
276+
// Use TextEncoder to convert the string to UTF-8 encoded bytes
277+
const encoder = new TextEncoder();
278+
return encoder.encode(stringValue);
279+
}
280+
272281
function compareNumbers(left: Value, right: Value): number {
273282
const leftNumber = normalizeNumber(left.integerValue || left.doubleValue);
274283
const rightNumber = normalizeNumber(right.integerValue || right.doubleValue);

packages/firestore/test/integration/api/query.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ import {
5454
writeBatch,
5555
CollectionReference,
5656
WriteBatch,
57-
Firestore
57+
Firestore,
58+
getDocsFromServer
5859
} from '../util/firebase_export';
5960
import {
6061
apiDescribe,
@@ -66,7 +67,8 @@ import {
6667
withRetry,
6768
withTestCollection,
6869
withTestDb,
69-
checkOnlineAndOfflineResultsMatch
70+
checkOnlineAndOfflineResultsMatch,
71+
toIds
7072
} from '../util/helpers';
7173
import { USE_EMULATOR } from '../util/settings';
7274
import { captureExistenceFilterMismatches } from '../util/testing_hooks_util';
@@ -2218,6 +2220,32 @@ apiDescribe('Queries', persistence => {
22182220
}
22192221
).timeout('90s');
22202222

2223+
it.only('snapshot listener sorts cross type numbers same way as server', async () => {
2224+
const testDocs = {
2225+
'a': { value: 'Łukasiewicz' },
2226+
'b': { value: 'Sierpiński' },
2227+
'c': { value: '岩澤' },
2228+
'd': { value: '🄟' },
2229+
'e': { value: 'P' },
2230+
'f': { value: '︒' },
2231+
'g': { value: '🐵' },
2232+
};
2233+
2234+
return withTestCollection(persistence, testDocs, async collectionRef => {
2235+
const orderedQuery = query(collectionRef, orderBy('value'));
2236+
2237+
const getSnapshot = await getDocsFromServer(orderedQuery);
2238+
expect(toIds(getSnapshot)).to.deep.equal(["b", "a", "c", "f", "e", "d", "g"]);
2239+
2240+
const storeEvent = new EventsAccumulator<QuerySnapshot>();
2241+
const unsubscribe = onSnapshot(orderedQuery, storeEvent.storeEvent);
2242+
const watchSnapshot = await storeEvent.awaitEvent();
2243+
expect(toIds(watchSnapshot)).to.deep.equal(toIds(getSnapshot));
2244+
2245+
unsubscribe();
2246+
});
2247+
});
2248+
22212249
it('can query large documents with multi-byte character strings', () => {
22222250
function randomMultiByteCharString(length: number): string {
22232251
const charCodes: number[] = [];

0 commit comments

Comments
 (0)