Skip to content

Commit e729144

Browse files
authored
fix: do not set message state to undefined (#81)
some API responses do not return messages -- do not update the message state for them
1 parent e3fe3c6 commit e729144

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/LeanplumInbox.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ export default class LeanplumInbox implements Inbox {
1919
queued: true,
2020
sendNow: true,
2121
response: (data) => {
22-
this.messageMap = data.response[0].newsfeedMessages
22+
const response = data.response[0]
23+
if (response && response.newsfeedMessages) {
24+
this.messageMap = response.newsfeedMessages
2325

24-
this.triggerChangeHandlers()
26+
this.triggerChangeHandlers()
27+
}
2528
},
2629
})
2730

test/specs/LeanplumInbox.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ describe(LeanplumInbox, () => {
3434

3535
expect(handler).toHaveBeenCalledTimes(1)
3636
})
37+
38+
it('works when reponse does not provide messages', () => {
39+
const handler = jest.fn()
40+
41+
inbox.onChanged(handler)
42+
43+
createRequestSpy.mockImplementationOnce(
44+
(method, args, options) => {
45+
options.response({
46+
response: [{
47+
success:true,
48+
warning:{
49+
message:"User not found; request skipped."
50+
}
51+
}]
52+
})
53+
}
54+
)
55+
inbox.downloadMessages()
56+
57+
expect(handler).toHaveBeenCalledTimes(0)
58+
expect(inbox.messageIds()).toEqual([])
59+
})
3760
})
3861

3962
describe('allMessages', () => {

0 commit comments

Comments
 (0)