Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Add (mostly pending) tests for messaging helper
Browse files Browse the repository at this point in the history
  • Loading branch information
darh committed Feb 11, 2020
1 parent 4d53e29 commit 3a9c5c6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
45 changes: 45 additions & 0 deletions src/corredor/helpers/messaging.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, beforeEach } from 'mocha'
import { expect } from 'chai'
import { stubObject, StubbedInstance } from 'ts-sinon'
import { Messaging as MessagingAPI } from '../../api-clients'
import { kv } from './shared'
import MessagingHelper from './messaging'
import { Channel } from '../../messaging'

describe(__filename, () => {
describe('supporting functions', () => {
describe('resolving channel', () => {
let h: StubbedInstance<MessagingHelper>

beforeEach(() => {
h = stubObject<MessagingHelper>(
new MessagingHelper({ MessagingAPI: new MessagingAPI({}) }),
[
'findChannelByID',
],
)
})

it('should return first valid channel', async () => {
const c = new Channel({ channelID: '333' })
expect(await h.resolveChannel(undefined, null, false, 0, '', c)).to.deep.equal(c)
expect(h.findChannelByID.notCalled, 'findChannelByID call not expected').true
})

it('should resolve id', async () => {
const c = new Channel({ channelID: '444' })
h.findChannelByID.resolves(c)
expect(await h.resolveChannel(c.channelID)).to.deep.equal(c)
expect(h.findChannelByID.calledOnceWith(c.channelID), 'findChannelByID call expected').true
})
})
})

describe.skip('helpers', () => {
it('should have tests for sendMessage')
it('should have tests for findChannels')
it('should have tests for findChanneldById')
it('should have tests for directMessages')
it('should have tests for saveChannel')
})
})
25 changes: 12 additions & 13 deletions src/corredor/helpers/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,26 @@ export default class MessagingHelper {
* })
* })
*
* @param message
* @property {string} message.message
* @param ch User, Channel object or ID
* @param message - string or Message object
* @param ch - User, Channel object or ID
*/
async sendMessage (message: string|Message, ch: string|Channel|User): Promise<any/* Message */> {
async sendMessage (message: string|Message, ch: string|Channel|User): Promise<Message> {
if (ch instanceof User) {
ch = await this.directChannel(ch)
}

this.resolveChannel(ch, this.$channel).then(ch => {
return this.resolveChannel(ch, this.$channel).then(ch => {
let m: Message
if (typeof message === 'string') {
message = { message }
m = new Message({ message })
} else {
m = new Message(message)
}

message.channelID = ch.channelID
return this.MessagingAPI.messageCreate(message)
// Uncomment line below when message type is done
// return this.MessagingAPI.messageCreate(message).then(m => new Message(m))
m.channelID = ch.channelID
return this.MessagingAPI
.messageCreate(kv(message))
.then(m => new Message(m))
})
}

Expand Down Expand Up @@ -175,9 +177,6 @@ export default class MessagingHelper {
* - string - find by handle
* - Channel object
* - object with channelID properties
*
* @param
* @property {string} [r.channelID]
*/
async resolveChannel (...args: unknown[]): Promise<Channel> {
for (let c of args) {
Expand Down
11 changes: 10 additions & 1 deletion src/messaging/types/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
type PartialMessage = { [_: string]: unknown }

export class Message {
// @todo port message class here
[_: string]: unknown

constructor (m?: PartialMessage) {
this.apply(m)
}

apply (m?: PartialMessage): void {
Object.assign(this, m)
}
}

0 comments on commit 3a9c5c6

Please sign in to comment.