Skip to content

Commit

Permalink
Convert test stubs to @vars
Browse files Browse the repository at this point in the history
This makes it possible to add more test files later and still get at the
stubs.
  • Loading branch information
lilyball committed Dec 18, 2014
1 parent 178896a commit 4f79aec
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions test/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,125 +9,123 @@ should = require 'should'
# Stub a few interfaces to grease the skids for tests. These are intentionally
# as minimal as possible and only provide enough to make the tests possible.
# Stubs are recreated before each test.
stubs = null
beforeEach ->
stubs =
@stubs =
# Slack client
channel:
name: 'general'
send: (msg) -> msg
client:
getUserByID: (id) ->
{name: 'name', email_address: '[email protected]'}
getChannelByID: (id) ->
stubs.channel
getChannelGroupOrDMByName: () ->
stubs.channel
getChannelByID: (id) =>
@stubs.channel
getChannelGroupOrDMByName: () =>
@stubs.channel
# Hubot.Robot instance
robot:
logger:
info: ->
debug: ->

# Generate a new slack instance for each test.
slackbot = null
beforeEach ->
slackbot = new SlackBot stubs.robot
slackbot.client = stubs.client
@slackbot = new SlackBot @stubs.robot
@slackbot.client = @stubs.client


###################################################################
# Start the tests
###################################################################
describe 'Adapter', ->
it 'Should initialize with a robot', ->
slackbot.robot.should.eql stubs.robot
@slackbot.robot.should.eql @stubs.robot

describe 'Login', ->
it 'Should set the robot name', ->
team =
name: 'Test Team'
user =
name: 'bot'
slackbot.loggedIn(user, team)
slackbot.robot.name.should.equal 'bot'
@slackbot.loggedIn(user, team)
@slackbot.robot.name.should.equal 'bot'

describe 'Removing message formatting', ->
it 'Should do nothing if there are no user links', ->
foo = slackbot.removeFormatting 'foo'
foo = @slackbot.removeFormatting 'foo'
foo.should.equal 'foo'

it 'Should decode entities', ->
foo = slackbot.removeFormatting 'foo > & < >&<'
foo = @slackbot.removeFormatting 'foo > & < >&<'
foo.should.equal 'foo > & < >&<'

it 'Should change <@U1234> links to @name', ->
foo = slackbot.removeFormatting 'foo <@U123> bar'
foo = @slackbot.removeFormatting 'foo <@U123> bar'
foo.should.equal 'foo @name bar'

it 'Should change <@U1234|label> links to label', ->
foo = slackbot.removeFormatting 'foo <@U123|label> bar'
foo = @slackbot.removeFormatting 'foo <@U123|label> bar'
foo.should.equal 'foo label bar'

it 'Should change <#C1234> links to #general', ->
foo = slackbot.removeFormatting 'foo <#C123> bar'
foo = @slackbot.removeFormatting 'foo <#C123> bar'
foo.should.equal 'foo #general bar'

it 'Should change <#C1234|label> links to label', ->
foo = slackbot.removeFormatting 'foo <#C123|label> bar'
foo = @slackbot.removeFormatting 'foo <#C123|label> bar'
foo.should.equal 'foo label bar'

it 'Should change <!everyone> links to @everyone', ->
foo = slackbot.removeFormatting 'foo <!everyone> bar'
foo = @slackbot.removeFormatting 'foo <!everyone> bar'
foo.should.equal 'foo @everyone bar'

it 'Should change <!channel> links to @channel', ->
foo = slackbot.removeFormatting 'foo <!channel> bar'
foo = @slackbot.removeFormatting 'foo <!channel> bar'
foo.should.equal 'foo @channel bar'

it 'Should change <!group> links to @group', ->
foo = slackbot.removeFormatting 'foo <!group> bar'
foo = @slackbot.removeFormatting 'foo <!group> bar'
foo.should.equal 'foo @group bar'

it 'Should remove formatting around <http> links', ->
foo = slackbot.removeFormatting 'foo <http://www.example.com> bar'
foo = @slackbot.removeFormatting 'foo <http://www.example.com> bar'
foo.should.equal 'foo http://www.example.com bar'

it 'Should remove formatting around <https> links', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com> bar'
foo = @slackbot.removeFormatting 'foo <https://www.example.com> bar'
foo.should.equal 'foo https://www.example.com bar'

it 'Should remove formatting around <skype> links', ->
foo = slackbot.removeFormatting 'foo <skype:echo123?call> bar'
foo = @slackbot.removeFormatting 'foo <skype:echo123?call> bar'
foo.should.equal 'foo skype:echo123?call bar'

it 'Should remove formatting around <https> links with a label', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com|label> bar'
foo = @slackbot.removeFormatting 'foo <https://www.example.com|label> bar'
foo.should.equal 'foo label (https://www.example.com) bar'

it 'Should remove formatting around <https> links with a substring label', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com|example.com> bar'
foo = @slackbot.removeFormatting 'foo <https://www.example.com|example.com> bar'
foo.should.equal 'foo https://www.example.com bar'

it 'Should remove formatting around <https> links with a label containing entitles', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com|label &gt; &amp; &lt;> bar'
foo = @slackbot.removeFormatting 'foo <https://www.example.com|label &gt; &amp; &lt;> bar'
foo.should.equal 'foo label > & < (https://www.example.com) bar'

it 'Should remove formatting around <mailto> links', ->
foo = slackbot.removeFormatting 'foo <mailto:[email protected]> bar'
foo = @slackbot.removeFormatting 'foo <mailto:[email protected]> bar'
foo.should.equal 'foo [email protected] bar'

it 'Should remove formatting around <mailto> links with an email label', ->
foo = slackbot.removeFormatting 'foo <mailto:[email protected]|[email protected]> bar'
foo = @slackbot.removeFormatting 'foo <mailto:[email protected]|[email protected]> bar'
foo.should.equal 'foo [email protected] bar'

it 'Should change multiple links at once', ->
foo = slackbot.removeFormatting 'foo <@U123|label> bar <#C123> <!channel> <https://www.example.com|label>'
foo = @slackbot.removeFormatting 'foo <@U123|label> bar <#C123> <!channel> <https://www.example.com|label>'
foo.should.equal 'foo label bar #general @channel label (https://www.example.com)'

describe 'Send Messages', ->
it 'Should send multiple messages', ->
sentMessages = slackbot.send {room: 'room-name'}, 'one', 'two', 'three'
sentMessages = @slackbot.send {room: 'room-name'}, 'one', 'two', 'three'
sentMessages.length.should.equal 3

it 'Should split long messages', ->
Expand All @@ -137,20 +135,20 @@ describe 'Send Messages', ->
len = 10000
msg += lines while msg.length < len

sentMessages = slackbot.send {room: 'room-name'}, msg
sentMessages = @slackbot.send {room: 'room-name'}, msg
sentMessage = sentMessages.pop()
sentMessage.length.should.equal Math.ceil(len / SlackBot.MAX_MESSAGE_LENGTH)

it 'Should try to split on word breaks', ->
msg = 'Foo bar baz'
slackbot.constructor.MAX_MESSAGE_LENGTH = 10
sentMessages = slackbot.send {room: 'room-name'}, msg
@slackbot.constructor.MAX_MESSAGE_LENGTH = 10
sentMessages = @slackbot.send {room: 'room-name'}, msg
sentMessage = sentMessages.pop()
sentMessage.length.should.equal 2

it 'Should split into max length chunks if there are no breaks', ->
msg = 'Foobar'
slackbot.constructor.MAX_MESSAGE_LENGTH = 3
sentMessages = slackbot.send {room: 'room-name'}, msg
@slackbot.constructor.MAX_MESSAGE_LENGTH = 3
sentMessages = @slackbot.send {room: 'room-name'}, msg
sentMessage = sentMessages.pop()
sentMessage.should.eql ['Foo', 'bar']

0 comments on commit 4f79aec

Please sign in to comment.