From 93add3ad68a60d778b3ad61de80b7449da0a1286 Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:00:25 -0700 Subject: [PATCH 1/8] adding instance reference to robot in constructor --- src/giphy.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/giphy.coffee b/src/giphy.coffee index 81ff0a1..f134f0e 100644 --- a/src/giphy.coffee +++ b/src/giphy.coffee @@ -59,9 +59,10 @@ class Giphy @regex = new RegExp "^\\s*(#{Giphy.endpoints.join('|')}|#{Giphy.HelpName})?\\s*(.*?)$", 'i' - constructor: (api) -> + constructor: (robot, api) -> throw new Error 'Giphy API is required' if not api + @robot = robot @api = api @defaultLimit = process.env.HUBOT_GIPHY_DEFAULT_LIMIT or '5' @defaultEndpoint = process.env.HUBOT_GIPHY_DEFAULT_ENDPOINT or Giphy.SearchEndpointName @@ -252,7 +253,7 @@ module.exports = (robot) -> apiKey: process.env.HUBOT_GIPHY_API_KEY }) - giphy = new Giphy api + giphy = new Giphy robot, api robot.respond /giphy\s*(.*?)\s*$/, (msg) -> giphy.respond msg From dcd9daad0bc9f10fae656f56512a87a98d712348 Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:00:48 -0700 Subject: [PATCH 2/8] making robot a required param (throw on missing) --- src/giphy.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/giphy.coffee b/src/giphy.coffee index f134f0e..072eb39 100644 --- a/src/giphy.coffee +++ b/src/giphy.coffee @@ -60,6 +60,7 @@ class Giphy @regex = new RegExp "^\\s*(#{Giphy.endpoints.join('|')}|#{Giphy.HelpName})?\\s*(.*?)$", 'i' constructor: (robot, api) -> + throw new Error 'Robot is required' if not robot throw new Error 'Giphy API is required' if not api @robot = robot From 13da735df49ca7f7d62d38c6ba260682c2f29df5 Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:01:28 -0700 Subject: [PATCH 3/8] robot name now included in help text --- src/giphy.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/giphy.coffee b/src/giphy.coffee index 072eb39..f183391 100644 --- a/src/giphy.coffee +++ b/src/giphy.coffee @@ -68,7 +68,7 @@ class Giphy @defaultLimit = process.env.HUBOT_GIPHY_DEFAULT_LIMIT or '5' @defaultEndpoint = process.env.HUBOT_GIPHY_DEFAULT_ENDPOINT or Giphy.SearchEndpointName @helpText = """ -giphy [endpoint] [options...] [args] +#{@robot.name} giphy [endpoint] [options...] [args] endpoints: search, id, translate, random, trending options: rating, limit, offset, api @@ -77,7 +77,7 @@ default endpoint is '#{@defaultEndpoint}' if none is specified options can be specified using /option:value Example: - giphy search /limit:100 /offset:50 /rating:pg something to search for + #{@robot.name} giphy search /limit:100 /offset:50 /rating:pg something to search for """.trim() ### istanbul ignore next ### From ad52b7dbfa9f9cf2a0e274476d47b37d067b885f Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:01:38 -0700 Subject: [PATCH 4/8] adding rating info to help text --- src/giphy.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/giphy.coffee b/src/giphy.coffee index f183391..905e175 100644 --- a/src/giphy.coffee +++ b/src/giphy.coffee @@ -75,6 +75,7 @@ options: rating, limit, offset, api default endpoint is '#{@defaultEndpoint}' if none is specified options can be specified using /option:value +rating can be one of y,g, pg, pg-13, or r Example: #{@robot.name} giphy search /limit:100 /offset:50 /rating:pg something to search for From 7f7bc5baf36cd9433b1adb49d1de42206e041149 Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:02:02 -0700 Subject: [PATCH 5/8] updating tests for constructor changes --- test/giphy.spec.coffee | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/test/giphy.spec.coffee b/test/giphy.spec.coffee index 3db999a..f186878 100644 --- a/test/giphy.spec.coffee +++ b/test/giphy.spec.coffee @@ -53,10 +53,12 @@ describe 'giphy', -> # clone the original environment so we can inject variables process.env = extend { }, @env + # create a fake robot + robot = { name: 'robot' } # create a new test giphy api @api = giphyApi() # create a new test giphy instance - @giphy = new Giphy @api + @giphy = new Giphy robot, @api # protect against any real XHR attempts @fakes.stub @api, '_request', (options, callback) -> callback 'XHR Attempted', null @@ -202,33 +204,42 @@ describe 'giphy', -> describe 'class', -> describe '.constructor', -> + it 'assigns the provided robot', -> + giphyInstance = new Giphy 'robot', 'api' + should.exist giphyInstance.robot + giphyInstance.robot.should.eql 'robot' + it 'assigns the provided api', -> - giphyInstance = new Giphy 'api' + giphyInstance = new Giphy 'robot', 'api' should.exist giphyInstance.api giphyInstance.api.should.eql 'api' it 'assigns a default endpoint', -> - giphyInstance = new Giphy 'api' + giphyInstance = new Giphy 'robot', 'api' should.exist giphyInstance.defaultEndpoint giphyInstance.defaultEndpoint.should.eql Giphy.SearchEndpointName it 'assigns a default limit', -> - giphyInstance = new Giphy 'api' + giphyInstance = new Giphy 'robot', 'api' should.exist giphyInstance.defaultLimit giphyInstance.defaultEndpoint.should.have.length.greaterThan.zero it 'allows default endpoint override via HUBOT_GIPHY_DEFAULT_ENDPOINT', -> process.env.HUBOT_GIPHY_DEFAULT_ENDPOINT = 'testing' - giphyInstance = new Giphy 'api' + giphyInstance = new Giphy 'robot', 'api' giphyInstance.defaultEndpoint.should.eql 'testing' it 'allows default limit override via HUBOT_GIPHY_DEFAULT_LIMIT', -> process.env.HUBOT_GIPHY_DEFAULT_LIMIT = '123' - giphyInstance = new Giphy 'api' + giphyInstance = new Giphy 'robot', 'api' giphyInstance.defaultLimit.should.eql '123' - it 'throws an error if no api is provided', -> + it 'throws an error if no robot is provided', -> should.throw -> new Giphy() + should.throw -> new Giphy null, 'api' + + it 'throws an error if no api is provided', -> + should.throw -> new Giphy 'robot' describe '.error', -> beforeEach -> From 4a1c3647c69c91df3032c3461e4aed1252f06e34 Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:11:51 -0700 Subject: [PATCH 6/8] integration tests now have a robot name --- test/giphy.spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/giphy.spec.coffee b/test/giphy.spec.coffee index f186878..990f814 100644 --- a/test/giphy.spec.coffee +++ b/test/giphy.spec.coffee @@ -959,7 +959,7 @@ describe 'giphy', -> msg.send.should.have.callCount 0 beforeEach -> - robot = { respond: @fakes.spy() } + robot = { name: 'robot', respond: @fakes.spy() } msg = { send: @fakes.spy() } giphyPluginInstance = hubotGiphy robot [ regex, callback ] = robot.respond.lastCall.args From 4462d456374637c2904271d96a1747a9bd36d626 Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:12:16 -0700 Subject: [PATCH 7/8] adding basic integration test for help text (console.log the help text too) --- test/giphy.spec.coffee | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/giphy.spec.coffee b/test/giphy.spec.coffee index 990f814..fa6eb32 100644 --- a/test/giphy.spec.coffee +++ b/test/giphy.spec.coffee @@ -1030,3 +1030,8 @@ describe 'giphy', -> it 'sends a response for "giphy search /limit:123 /offset:25 test"', (done) -> testInput done, @fakes, 'giphy search /limit:123 /offset:25 test', sampleCollectionResult, { api: 'gifs', endpoint: 'search', query: { limit: '123', offset: '25', q: 'test' } } + + it 'sends help text for "giphy help"', (done) -> + testInput done, @fakes, 'giphy help', null, -> + msg.send.should.have.been.called + console.log msg.send.lastCall.args[0] From b18661c978d1970986fc7d4483911ff272e90c9d Mon Sep 17 00:00:00 2001 From: Pat Sissons Date: Sun, 15 May 2016 13:12:38 -0700 Subject: [PATCH 8/8] removing result content from integration test that does not use the result content --- test/giphy.spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/giphy.spec.coffee b/test/giphy.spec.coffee index fa6eb32..871b61c 100644 --- a/test/giphy.spec.coffee +++ b/test/giphy.spec.coffee @@ -974,7 +974,7 @@ describe 'giphy', -> testInput done, @fakes, 'giphy search test1 test2', sampleCollectionResult, { api: 'gifs', endpoint: 'search', query: { q: 'test1 test2' } } it 'sends a response for "giphy id"', (done) -> - testInput done, @fakes, 'giphy id', sampleCollectionResult, -> + testInput done, @fakes, 'giphy id', null, -> msg.send.should.have.been.calledWith 'No Id Provided' it 'sends a response for "giphy id test"', (done) ->