Commit 51adda62a01568ee3fd7854d3ba56a12013f7524
1 parent
76666012
Exists in
colab
and in
4 other branches
Implement more Javascript tests for branch selection
Signed-off-by: Pedro Scocco <pedroscocco@gmail.com>
Showing
1 changed file
with
109 additions
and
27 deletions
Show diff stats
spec/javascripts/repository/branch_spec.js.coffee
| ... | ... | @@ -3,36 +3,118 @@ |
| 3 | 3 | #= require repository/branch |
| 4 | 4 | #= require sinon |
| 5 | 5 | |
| 6 | -describe "Branch#constructor", -> | |
| 7 | - it "should construct a branch", -> | |
| 8 | - subject = new Repository.Branch() | |
| 9 | - subject.names.should.deep.equal({}) | |
| 10 | - assert.isNull(subject.request) | |
| 11 | - | |
| 12 | -describe "toggle", -> | |
| 13 | - before -> | |
| 6 | +describe "Repository.Branch", -> | |
| 7 | + beforeEach -> | |
| 14 | 8 | @subject = new Repository.Branch() |
| 15 | 9 | |
| 16 | - @combo_box = sinon.stub() | |
| 17 | - $ = sinon.stub(window, "$") | |
| 18 | - $.withArgs("#branches").returns(@combo_box) | |
| 10 | + describe "constructor", -> | |
| 11 | + it "should construct a branch", -> | |
| 12 | + @subject.names.should.deep.equal({}) | |
| 13 | + assert.isNull(@subject.request) | |
| 19 | 14 | |
| 20 | - context "scm_type = SVN", -> | |
| 15 | + describe "#toggle", -> | |
| 21 | 16 | before -> |
| 22 | - @combo_box.hide = sinon.spy() | |
| 23 | - $.withArgs("#repository_scm_type").returns({val: -> "SVN"}) | |
| 17 | + @combo_box = sinon.stub() | |
| 18 | + sinon.stub(window, "$") | |
| 19 | + $.withArgs("#branches").returns(@combo_box) | |
| 24 | 20 | |
| 25 | - it "should hide the branches combo box", -> | |
| 26 | - @subject.toggle() | |
| 27 | - assert.isTrue(@combo_box.hide.calledOnce) | |
| 21 | + after -> | |
| 22 | + window.$.restore() | |
| 28 | 23 | |
| 29 | - context "scm_type != SVN", -> | |
| 30 | - before -> | |
| 31 | - @combo_box.show = sinon.spy() | |
| 32 | - $.withArgs("#repository_address").returns({val: -> "https://github.com/mezuro/prezento.git"}) | |
| 33 | - $.withArgs("#repository_scm_type").returns({val: -> "GIT"}) | |
| 34 | - sinon.stub(@subject, "fetch").withArgs("https://github.com/mezuro/prezento.git") | |
| 35 | - | |
| 36 | - it "should show the branches combo box", -> | |
| 37 | - @subject.toggle() | |
| 38 | - assert.isTrue(@combo_box.show.calledOnce) | |
| 24 | + context "scm_type = SVN", -> | |
| 25 | + beforeEach -> | |
| 26 | + @combo_box.hide = sinon.spy() | |
| 27 | + $.withArgs("#repository_scm_type").returns({val: -> "SVN"}) | |
| 28 | + | |
| 29 | + it "should hide the branches combo box", -> | |
| 30 | + @subject.toggle() | |
| 31 | + assert.isTrue(@combo_box.hide.calledOnce) | |
| 32 | + | |
| 33 | + context "scm_type != SVN", -> | |
| 34 | + beforeEach -> | |
| 35 | + @combo_box.show = sinon.spy() | |
| 36 | + $.withArgs("#repository_address").returns({val: -> "https://github.com/mezuro/prezento.git"}) | |
| 37 | + $.withArgs("#repository_scm_type").returns({val: -> "GIT"}) | |
| 38 | + sinon.stub(@subject, "fetch").withArgs("https://github.com/mezuro/prezento.git") | |
| 39 | + | |
| 40 | + it "should show the branches combo box", -> | |
| 41 | + @subject.toggle() | |
| 42 | + assert.isTrue(@combo_box.show.calledOnce) | |
| 43 | + | |
| 44 | + describe "#cancel_request", -> | |
| 45 | + context 'request is not null', -> | |
| 46 | + beforeEach -> | |
| 47 | + @request = sinon.stub() | |
| 48 | + @request.abort = sinon.spy() | |
| 49 | + @subject.request = @request | |
| 50 | + | |
| 51 | + it 'should abort the request', -> | |
| 52 | + @subject.cancel_request() | |
| 53 | + assert.isTrue(@request.abort.calledOnce) | |
| 54 | + assert.isNull(@subject.request) | |
| 55 | + | |
| 56 | + describe "#fill_options", -> | |
| 57 | + beforeEach -> | |
| 58 | + @el = jQuery("<select></select>") | |
| 59 | + | |
| 60 | + context 'with branches that contain master', -> | |
| 61 | + it 'should place master first', -> | |
| 62 | + @subject.fill_options(['dev', 'stable', 'master'], @el) | |
| 63 | + | |
| 64 | + option_values = [] | |
| 65 | + for option in @el.get(0).options | |
| 66 | + option_values.push(option.value) | |
| 67 | + | |
| 68 | + assert.deepEqual(option_values, ['master', 'dev', 'stable']) | |
| 69 | + | |
| 70 | + context "with branches that don't contain master", -> | |
| 71 | + it "shouldn't change anything", -> | |
| 72 | + @subject.fill_options(['dev', 'stable', 'branch_do_daniel'], @el) | |
| 73 | + | |
| 74 | + option_values = [] | |
| 75 | + for option in @el.get(0).options | |
| 76 | + option_values.push(option.value) | |
| 77 | + | |
| 78 | + assert.deepEqual(option_values, ['dev', 'stable', 'branch_do_daniel']) | |
| 79 | + | |
| 80 | + describe '#fetch', -> | |
| 81 | + beforeEach -> | |
| 82 | + @subject.cancel_request = sinon.spy() | |
| 83 | + | |
| 84 | + context 'with valid address', -> | |
| 85 | + beforeEach -> | |
| 86 | + @subject.fill_options = sinon.spy() | |
| 87 | +# @subject.names = {'https://github.com/mezuro/prezento.git': ['master', 'dev']} | |
| 88 | + | |
| 89 | + context 'with new address', -> | |
| 90 | + beforeEach -> | |
| 91 | + @subject.names = {} | |
| 92 | + | |
| 93 | + @address = 'https://github.com/mezuro/kalibro_processor.git' | |
| 94 | + | |
| 95 | + @select = sinon.stub() | |
| 96 | + # Stub just the selector and not the whole jQuery object | |
| 97 | + find_stub = sinon.stub($, "find") | |
| 98 | + find_stub.withArgs("#repository_branch").returns(@select) | |
| 99 | + find_stub.withArgs("#repository_scm_type").returns({val: -> "SVN"}) | |
| 100 | + | |
| 101 | + @server = sinon.fakeServer.create() | |
| 102 | + @server.respondImmediately = true | |
| 103 | + | |
| 104 | + address_url_encoded = encodeURI(@address) | |
| 105 | + @server.respondWith('GET', '/repository_branches&url=' + address_url_encoded + '&scm_type=' + 'GIT', | |
| 106 | + [200, { "Content-Type": "application/json" }, JSON.stringify({ | |
| 107 | + "branches": ["stable", "dev", "master"] | |
| 108 | + })] | |
| 109 | + ) | |
| 110 | + | |
| 111 | + afterEach -> | |
| 112 | + @server.restore() | |
| 113 | + $.find.restore() | |
| 114 | + | |
| 115 | + it 'should fetch the branches and fill the options', -> | |
| 116 | + @subject.fetch(@address) | |
| 117 | + @server.respond() | |
| 118 | + console.log(@server.requests[0]) | |
| 119 | + debugger | |
| 120 | + assert.isTrue(@subject.fill_options.calledWith(['stable', 'dev', 'master'], @select)) | ... | ... |