From 82d9b6f01e653250633748665a142d773feb39b7 Mon Sep 17 00:00:00 2001 From: Nicholas Molnar Date: Tue, 6 Sep 2022 08:35:38 -0700 Subject: [PATCH] fix: stop requesting when limit is less than the result size --- src/ApiClient.ts | 2 +- test/ApiClient.test.ts | 10 ++++++++++ test/Client.test.ts | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ApiClient.ts b/src/ApiClient.ts index 4766f7388..a190d0d95 100644 --- a/src/ApiClient.ts +++ b/src/ApiClient.ts @@ -181,7 +181,7 @@ export default class ApiClient { for (const envelope of page) { out.push(envelope) if (limit && out.length === limit) { - break + return out } } } diff --git a/test/ApiClient.test.ts b/test/ApiClient.test.ts index ab3238aa1..050513b66 100644 --- a/test/ApiClient.test.ts +++ b/test/ApiClient.test.ts @@ -54,6 +54,16 @@ describe('Query', () => { }) }) + it('stops when limit is used', async () => { + const apiMock = createQueryMock([createEnvelope()], 3) + const result = await client.query( + { contentTopics: [CONTENT_TOPIC] }, + { limit: 2 } + ) + expect(result).toHaveLength(2) + expect(apiMock).toHaveBeenCalledTimes(2) + }) + it('stops when receiving some results and a null cursor', async () => { const apiMock = createQueryMock([createEnvelope()], 1) const result = await client.query({ contentTopics: [CONTENT_TOPIC] }, {}) diff --git a/test/Client.test.ts b/test/Client.test.ts index 617c80c66..368a5ef7c 100644 --- a/test/Client.test.ts +++ b/test/Client.test.ts @@ -350,6 +350,15 @@ describe('Client', () => { assert.equal(msg.senderAddress, alice.address) assert.equal(msg.content, 'Hello from Alice') }) + + it('handles limiting page size', async () => { + const bobConvo = await alice.conversations.newConversation(bob.address) + for (let i = 0; i < 5; i++) { + await bobConvo.send('hi') + } + const messages = await bobConvo.messages({ limit: 2 }) + expect(messages).toHaveLength(2) + }) }) }) })