diff --git a/spec/javascripts/repository/branch_spec.js.coffee b/spec/javascripts/repository/branch_spec.js.coffee index 6c9ec7e..82ad5a0 100644 --- a/spec/javascripts/repository/branch_spec.js.coffee +++ b/spec/javascripts/repository/branch_spec.js.coffee @@ -77,31 +77,51 @@ describe "Repository.Branch", -> describe '#fetch', -> beforeEach -> + @subject.names = {} + @address = 'https://github.com/mezuro/kalibro_processor.git' @subject.cancel_request = sinon.stub() + sinon.stub(@subject, "fill_options") - context 'with valid address', -> - beforeEach -> - sinon.stub(@subject, "fill_options") + @select = {empty: sinon.stub()} + # $ is a global jQuery variable. That is why we don't need to declare it as @$ + $ = sinon.stub(window, "$") + $.withArgs("#repository_branch").returns(@select) + $.withArgs("#repository_scm_type").returns({val: -> "GIT"}) - afterEach -> - @subject.fill_options.restore() + afterEach -> + @subject.fill_options.restore() + $.restore() - context 'with new address', -> + context 'with valid address', -> + context 'with an address that is not cached', -> beforeEach -> - @subject.names = {} - @address = 'https://github.com/mezuro/kalibro_processor.git' - - @select = {empty: sinon.stub()} - $ = sinon.stub(window, "$") - $.withArgs("#repository_branch").returns(@select) - $.withArgs("#repository_scm_type").returns({val: -> "GIT"}) $.get = sinon.stub().withArgs('/repository_branches', {'url': @address, 'scm_type': 'GIT'}).returns().yields({ 'branches': ['stable', 'dev', 'master'] }) - afterEach -> - $.restore() - it 'should fetch the branches and fill the options', -> @subject.fetch(@address) + sinon.assert.calledWith(@select.empty) + sinon.assert.calledWith(@subject.fill_options, ['stable', 'dev', 'master'], @select) + + context 'with an address that is cached', -> + beforeEach -> + @subject.names[@address] = ['stable', 'dev', 'master'] + $.get = sinon.stub() + + it 'should not request the branches but fill the select with the cached values', -> + @subject.fetch(@address) + sinon.assert.calledWith(@select.empty) sinon.assert.calledWith(@subject.fill_options, ['stable', 'dev', 'master'], @select) + sinon.assert.notCalled($.get) + + context 'with an invalid address', -> + beforeEach -> + $.get = sinon.stub().withArgs('/repository_branches', {'url': @address, 'scm_type': 'GIT'}).returns().yields({ + 'errors': ['InvalidUrl'] + }) + + it 'should not fill the options', -> + @subject.fetch(@address) + sinon.assert.calledWith(@select.empty) + sinon.assert.notCalled(@subject.fill_options) -- libgit2 0.21.2