Skip to content

Commit

Permalink
Merge pull request #137 from freetrade-io/fix/start-after-issue
Browse files Browse the repository at this point in the history
Fix/start after issue
  • Loading branch information
varsis authored Oct 6, 2022
2 parents 4824e1e + e7911ea commit e1e37d0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@freetrade-io/ts-firebase-driver-testing",
"version": "1.0.18",
"version": "1.0.19",
"description": "Swap out Firebase as a driver for in-process testing",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/driver/Firestore/InProcessFirestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,16 @@ export class InProcessFirestoreQuery implements IFirestoreQuery {
if (_.isNumber(val)) {
return val
}

if (val instanceof InProcessFirestoreDocRef) {
return val.id
}
if (val && typeof val.toISOString === "function") {
val = val.toISOString()
}
if (val && typeof val.toString === "function") {
val = val.toString()
}

return String(val)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,42 @@ describe("In-process Firestore start after query on collectionGroup", () => {
result.docs.map((doc) => ({ id: doc.id, data: doc.data() })),
).toStrictEqual([{ id: "22da618d", data: { name: "aardvark" } }])
})

test("startAfter document id", async () => {
// Given there is a collection of documents with ids;
await db.doc("livingthings/animals/22da618d").set({ name: "aardvark" })
await db.doc("livingthings/animals/00a3382").set({ name: "badger" })
await db.doc("livingthings/animals/11cbe6b5").set({ name: "camel" })

const startAfterDoc = db.doc("livingthings/animals/11cbe6b5")

// When we order the collection by the document id;
const result = await db
.collectionGroup("animals")
.orderBy(FieldPath.documentId())
.startAfter(startAfterDoc)
.get()

// Then we should get the collection ordered by that field.
expect(result.size).toEqual(1)
expect(
result.docs.map((doc) => ({ id: doc.id, data: doc.data() })),
).toStrictEqual([{ id: "22da618d", data: { name: "aardvark" } }])
})

test("startAfter document id after using doc ref", async () => {
// Given there is a collection of documents with ids;
await db.doc("livingthings/animals/22da618d").set({ name: "aardvark" })
const startAfterDoc = db.doc("livingthings/animals/22da618d")

// When we order the collection by the document id;
const result = await db
.collectionGroup("animals")
.orderBy(FieldPath.documentId())
.startAfter(startAfterDoc)
.get()

// Then we should get no items
expect(result.size).toEqual(0)
})
})
38 changes: 38 additions & 0 deletions tests/driver/Firestore/InProcessFirestore.startAfter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,42 @@ describe("In-process Firestore start after query", () => {
result.docs.map((doc) => ({ id: doc.id, data: doc.data() })),
).toStrictEqual([{ id: "22da618d", data: { name: "aardvark" } }])
})

test.only("startAfter document id", async () => {
// Given there is a collection of documents with ids;
await db.doc("animals/22da618d").set({ name: "aardvark" })
await db.doc("animals/00a3382").set({ name: "badger" })
await db.doc("animals/11cbe6b5").set({ name: "camel" })

const startAfterDoc = db.doc("livingthings/animals/11cbe6b5")

// When we order the collection by the document id;
const result = await db
.collection("animals")
.orderBy(FieldPath.documentId())
.startAfter(startAfterDoc)
.get()

// Then we should get the collection ordered by that field.
expect(result.size).toEqual(1)
expect(
result.docs.map((doc) => ({ id: doc.id, data: doc.data() })),
).toStrictEqual([{ id: "22da618d", data: { name: "aardvark" } }])
})

test("startAfter document id after using doc ref", async () => {
// Given there is a collection of documents with ids;
await db.doc("animals/22da618d").set({ name: "aardvark" })
const startAfterDoc = db.doc("animals/22da618d")

// When we order the collection by the document id;
const result = await db
.collection("animals")
.orderBy(FieldPath.documentId())
.startAfter(startAfterDoc)
.get()

// Then we should get no items
expect(result.size).toEqual(0)
})
})

0 comments on commit e1e37d0

Please sign in to comment.