Skip to content

Commit

Permalink
Invitation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpatel531 committed Aug 24, 2015
1 parent 86b6fad commit ccf8dc2
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/modules/invitations/hipchat_invitation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ class HipChatInvitation extends Invitation
else
true

getHipChat: ->
new HipChat(@session.hc_key)

send: (done) ->
collaboratorsArray = @recipient.match(/\w+/g)
collaboratorsString = _.map(collaboratorsArray, (collaborator) ->
"@" + collaborator unless collaborator[0] is "@"
).join(", ")

hc_client = new HipChat(@session.hc_key)
hc_client = @getHipChat()

hc_client.listRooms (data) =>

try
room_id = _.findWhere(data.rooms, {name: @session.room_name}).room_id
catch error
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/invitations/slack_invitation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class SlackInvitation extends Invitation
else
true

getSlack: -> new Slack()

send: (done)->
slack = new Slack()
slack = @getSlack()
slack.setWebhook @session.slack_url
params =
text: "Hello there #{@recipient}. You have been invited to a pairing session. If you haven't installed the AtomPair plugin, type \`apm install atom-pair\` into your terminal. Go onto Atom, hit 'Join a pairing session', and enter this string: #{@session.id}"
Expand Down
124 changes: 124 additions & 0 deletions spec/invitations/invitation-spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
Invitation = require '../../lib/modules/invitations/invitation'
HipChatInvitation = require '../../lib/modules/invitations/hipchat_invitation'
SlackInvitation = require '../../lib/modules/invitations/slack_invitation'
Session = require '../../lib/modules/session'
PusherMock = require '../pusher-mock'
User = require '../../lib/modules/user'

describe 'Invitation', ->

activationPromise = null

beforeEach ->
activationPromise = atom.packages.activatePackage('atom-pair')
pusher = new PusherMock
spyOn(window, 'Pusher').andReturn(pusher)

it 'complains if there are no Pusher keys', ->
waitsForPromise -> activationPromise
runs ->
session = new Session
spyOn(atom.notifications, 'addError')
invitation = new Invitation(session)
expect(atom.notifications.addError).toHaveBeenCalled()

it 'writes a session ID to clipboard and sets up session', ->
waitsForPromise -> activationPromise
runs ->
session = new Session
atom.config.set('atom-pair.pusher_app_key', 'key')
atom.config.set('atom-pair.pusher_app_secret', 'secret')
spyOn(atom.clipboard, 'write')
spyOn(atom.notifications, 'addError')
spyOn(atom.notifications, 'addInfo')
spyOn(session, 'pairingSetup')
session.id = 'lala'
invitation = new Invitation(session)
expect(atom.notifications.addError).not.toHaveBeenCalled()
expect(atom.clipboard.write).toHaveBeenCalledWith('lala')
expect(atom.notifications.addInfo).toHaveBeenCalled()
expect(User.me).toBeDefined()
expect(session.pairingSetup).toHaveBeenCalled()

describe 'SlackInvitation', ->

it 'complains if there is no slack webhook url', ->
waitsForPromise -> activationPromise

runs ->
session = new Session
atom.config.set('atom-pair.pusher_app_key', 'key')
atom.config.set('atom-pair.pusher_app_secret', 'secret')
spyOn(atom.notifications, 'addError')
invitation = new SlackInvitation(session)
expect(atom.notifications.addError).toHaveBeenCalledWith("Please set your Slack Incoming WebHook")

it 'sends the slack webhook', ->
waitsForPromise -> activationPromise

runs ->
mockSlack = {
setWebhook: ->
webhook: (params, done) ->
done()
}
spyOn(SlackInvitation.prototype, 'getSlack').andReturn(mockSlack)
spyOn(SlackInvitation.prototype, 'afterSend')
SlackInvitation.prototype.getRecipientName = (cta, callback) ->
callback()

spyOn(mockSlack, 'setWebhook')
spyOn(mockSlack, 'webhook').andCallThrough()
spyOn(atom.notifications, 'addInfo')
atom.config.set('atom-pair.pusher_app_key', 'key')
atom.config.set('atom-pair.pusher_app_secret', 'secret')
atom.config.set('atom-pair.slack_url', 'yolo.com')
session = new Session
invitation = new SlackInvitation(session)
expect(mockSlack.setWebhook).toHaveBeenCalledWith('yolo.com')
expect(mockSlack.webhook).toHaveBeenCalled()
expect(atom.notifications.addInfo).toHaveBeenCalled()
expect(SlackInvitation.prototype.afterSend).toHaveBeenCalled()

describe 'HipChat invitation', ->

it 'complains if there is no token', ->
waitsForPromise -> activationPromise

runs ->
session = new Session
atom.config.set('atom-pair.pusher_app_key', 'key')
atom.config.set('atom-pair.pusher_app_secret', 'secret')
spyOn(atom.notifications, 'addError')
invitation = new HipChatInvitation(session)
expect(atom.notifications.addError).toHaveBeenCalledWith("Please set your HipChat keys.")

it 'sends the hipchat invitation', ->
waitsForPromise -> activationPromise

runs ->
mockRoom = 'room'
mockHipChat = {
listRooms: (done) -> done(rooms: [{name:mockRoom, room_id: 1}])
postMessage: (params, done) ->
done()
}
spyOn(HipChatInvitation.prototype, 'getHipChat').andReturn(mockHipChat)
spyOn(HipChatInvitation.prototype, 'afterSend')
HipChatInvitation.prototype.getRecipientName = (cta, callback) ->
@recipient = "@jam"
callback()

spyOn(atom.notifications, 'addInfo')
spyOn(atom.notifications, 'addError')
spyOn(mockHipChat, 'postMessage').andCallThrough()
atom.config.set('atom-pair.pusher_app_key', 'key')
atom.config.set('atom-pair.pusher_app_secret', 'secret')
atom.config.set('atom-pair.hipchat_room_name', mockRoom)
atom.config.set('atom-pair.hipchat_token', 'token')
session = new Session
invitation = new HipChatInvitation(session)
expect(atom.notifications.addError).not.toHaveBeenCalled()
expect(mockHipChat.postMessage).toHaveBeenCalled()
expect(atom.notifications.addInfo).toHaveBeenCalled()
expect(HipChatInvitation.prototype.afterSend).toHaveBeenCalled()
Empty file.

0 comments on commit ccf8dc2

Please sign in to comment.