diff --git a/test/models/messages_test.js b/test/models/messages_test.js index a02752b7244c..cff211938b9c 100644 --- a/test/models/messages_test.js +++ b/test/models/messages_test.js @@ -33,6 +33,109 @@ describe('Message', () => { return messages.add(attrs); } + // NOTE: These tests are incomplete. + describe('send', () => { + it("saves the result's dataMessage", async () => { + const message = createMessage({ type: 'outgoing', source }); + + const fakeDataMessage = new ArrayBuffer(0); + const result = { + dataMessage: fakeDataMessage, + discoveredIdentifierPairs: [], + }; + const promise = Promise.resolve(result); + await message.send(promise); + + assert.strictEqual(message.get('dataMessage'), fakeDataMessage); + }); + + it('updates the `sent` attribute', async () => { + const message = createMessage({ type: 'outgoing', source, sent: false }); + + await message.send( + Promise.resolve({ + discoveredIdentifierPairs: [], + }) + ); + + assert.isTrue(message.get('sent')); + }); + + it("triggers the 'done' event on success", async () => { + const message = createMessage({ type: 'outgoing', source }); + + let callCount = 0; + message.on('done', () => { + callCount += 1; + }); + + await message.send( + Promise.resolve({ + discoveredIdentifierPairs: [], + }) + ); + + assert.strictEqual(callCount, 1); + }); + + it("triggers the 'sent' event on success", async () => { + const message = createMessage({ type: 'outgoing', source }); + + const calls = []; + message.on('sent', (...args) => { + calls.push(args); + }); + + await message.send( + Promise.resolve({ + discoveredIdentifierPairs: [], + }) + ); + + assert.lengthOf(calls, 1); + assert.strictEqual(calls[0][0], message); + }); + + it("triggers the 'done' event on failure", async () => { + const message = createMessage({ type: 'outgoing', source }); + + let callCount = 0; + message.on('done', () => { + callCount += 1; + }); + + await message.send(Promise.reject(new Error('something went wrong!'))); + + assert.strictEqual(callCount, 1); + }); + + it('saves errors from promise rejections with errors', async () => { + const message = createMessage({ type: 'outgoing', source }); + + const promise = Promise.reject(new Error('foo bar')); + await message.send(promise); + + const errors = message.get('errors') || []; + assert.lengthOf(errors, 1); + assert.strictEqual(errors[0].message, 'foo bar'); + }); + + it('saves errors from promise rejections with objects', async () => { + const message = createMessage({ type: 'outgoing', source }); + + const result = { + errors: [new Error('baz qux')], + discoveredIdentifierPairs: [], + }; + const promise = Promise.reject(result); + await message.send(promise); + + const errors = message.get('errors') || []; + assert.lengthOf(errors, 1); + assert.strictEqual(errors[0].message, 'baz qux'); + }); + }); + describe('getContact', () => { it('gets outgoing contact', () => { const messages = new Whisper.MessageCollection(); diff --git a/ts/models/messages.ts b/ts/models/messages.ts index e588e4d31be2..d829ac7f3200 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -1941,6 +1941,7 @@ export class MessageModel extends window.Backbone.Model { previewWithData, stickerWithData, null, + this.get('deletedForEveryoneTimestamp'), this.get('sent_at'), this.get('expireTimer'), profileKey, @@ -2155,17 +2156,23 @@ export class MessageModel extends window.Backbone.Model { this.trigger('sent', this); this.sendSyncMessage(); }) - .catch(result => { + .catch((result: CustomError | CallbackResultType) => { this.trigger('done'); - if (result.dataMessage) { + if ('dataMessage' in result && result.dataMessage) { this.set({ dataMessage: result.dataMessage }); } let promises = []; // If we successfully sent to a user, we can remove our unregistered flag. - result.successfulIdentifiers.forEach((identifier: string) => { + let successfulIdentifiers: Array; + if ('successfulIdentifiers' in result) { + ({ successfulIdentifiers = [] } = result); + } else { + successfulIdentifiers = []; + } + successfulIdentifiers.forEach((identifier: string) => { const c = window.ConversationController.get(identifier); if (c && c.isEverUnregistered()) { c.setRegistered(); @@ -2185,7 +2192,7 @@ export class MessageModel extends window.Backbone.Model { promises.push(c.getProfiles()); } } else { - if (result.successfulIdentifiers.length > 0) { + if (successfulIdentifiers.length > 0) { const sentTo = this.get('sent_to') || []; // If we just found out that we couldn't send to a user because they are no @@ -2229,7 +2236,7 @@ export class MessageModel extends window.Backbone.Model { unidentifiedDeliveries: result.unidentifiedDeliveries, }); promises.push(this.sendSyncMessage()); - } else { + } else if (result.errors) { this.saveErrors(result.errors); } promises = promises.concat(