Skip to content

Commit a2133fb

Browse files
committed
fix: conversation list command failing when there are no conversations
1 parent 4add670 commit a2133fb

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

__tests__/commands/conversations/conversations.list.test.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,43 @@ describe('Command: vonage conversations list', () => {
1313
mockConsole();
1414
});
1515

16+
test('Will list with no conversations', async () => {
17+
confirm.mockResolvedValue(true);
18+
19+
const conversationMock = jest.fn()
20+
.mockResolvedValueOnce({
21+
conversations: [],
22+
links: {
23+
self: {
24+
href: 'https://api.nexmo.com/conversations',
25+
},
26+
},
27+
});
28+
29+
const sdkMock = {
30+
conversations: {
31+
getConversationPage: conversationMock,
32+
},
33+
};
34+
35+
await handler({ SDK: sdkMock, pageSize: 10 });
36+
37+
expect(conversationMock).toHaveBeenCalledTimes(1);
38+
expect(conversationMock).toHaveBeenNthCalledWith(
39+
1,
40+
{
41+
pageSize: 10,
42+
cursor: undefined,
43+
},
44+
);
45+
46+
expect(console.log).toHaveBeenNthCalledWith(
47+
1,
48+
'No conversations found',
49+
);
50+
expect(console.table).toHaveBeenCalledTimes(0);
51+
});
52+
1653
test('Will list all conversations', async () => {
1754
confirm.mockResolvedValue(true);
1855

@@ -31,7 +68,7 @@ describe('Command: vonage conversations list', () => {
3168
},
3269
})
3370
.mockResolvedValueOnce({
34-
conversations: conversations.slice(10, 10),
71+
conversations: conversations.slice(10, 20),
3572
links: {
3673
next: {
3774
href: 'https://api.nexmo.com/conversations?cursor=2',
@@ -50,6 +87,11 @@ describe('Command: vonage conversations list', () => {
5087

5188
await handler({ SDK: sdkMock, pageSize: 10 });
5289

90+
console.log(conversations.length);
91+
console.log(conversations.slice(0, 10).length);
92+
console.log(conversations.slice(10, 20).length);
93+
console.log(conversations.slice(20).length);
94+
5395
expect(confirm).toHaveBeenCalledTimes(2);
5496
expect(confirm).toHaveBeenNthCalledWith(
5597
1,
@@ -96,7 +138,7 @@ describe('Command: vonage conversations list', () => {
96138

97139
expect(console.table).toHaveBeenNthCalledWith(
98140
2,
99-
conversations.slice(10, 10).map((conversation) => ({
141+
conversations.slice(10, 20).map((conversation) => ({
100142
'Name': conversation.name,
101143
'Conversation ID': conversation.id,
102144
'Display Name': conversation.displayName,
@@ -133,7 +175,7 @@ describe('Command: vonage conversations list', () => {
133175
},
134176
})
135177
.mockResolvedValueOnce({
136-
conversations: conversations.slice(10, 10),
178+
conversations: conversations.slice(10, 20),
137179
links: {
138180
next: {
139181
href: 'https://api.nexmo.com/conversations?cursor=2',
@@ -179,7 +221,7 @@ describe('Command: vonage conversations list', () => {
179221

180222
expect(console.table).toHaveBeenNthCalledWith(
181223
2,
182-
conversations.slice(10, 10).map((conversation) => ({
224+
conversations.slice(10, 20).map((conversation) => ({
183225
'Name': conversation.name,
184226
'Conversation ID': conversation.id,
185227
'Display Name': conversation.displayName,

src/commands/conversations/list.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@ exports.handler = async (argv) => {
4747
return;
4848
}
4949

50+
const { conversations } = response;
51+
console.debug(`Fetched ${conversations.length} conversations`);
52+
53+
if (conversations.length < 1) {
54+
console.log('No conversations found');
55+
continue;
56+
}
57+
5058
console.log('');
51-
console.table([...response.conversations].map(conversationSummary));
59+
console.table(conversations.map(conversationSummary));
5260

5361
pageCursor = response.links?.next?.href
5462
? new URL(response.links.next.href).searchParams.get('cursor')

0 commit comments

Comments
 (0)