BigW Consortium Gitlab

merge_request_tabs_spec.js.coffee 2.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#= require merge_request_tabs

describe 'MergeRequestTabs', ->
  stubLocation = (stubs) ->
    defaults = {pathname: '', search: '', hash: ''}
    $.extend(defaults, stubs)

  fixture.preload('merge_request_tabs.html')

  beforeEach ->
    @class = new MergeRequestTabs()
    @spies = {
      ajax:    spyOn($, 'ajax').and.callFake ->
      history: spyOn(history, 'replaceState').and.callFake ->
    }

  describe '#activateTab', ->
    beforeEach ->
      fixture.load('merge_request_tabs.html')
      @subject = @class.activateTab

    it 'shows the first tab when action is show', ->
      @subject('show')
      expect($('#notes')).toHaveClass('active')

    it 'shows the notes tab when action is notes', ->
      @subject('notes')
      expect($('#notes')).toHaveClass('active')

    it 'shows the commits tab when action is commits', ->
      @subject('commits')
      expect($('#commits')).toHaveClass('active')

    it 'shows the diffs tab when action is diffs', ->
      @subject('diffs')
      expect($('#diffs')).toHaveClass('active')

  describe '#setCurrentAction', ->
    beforeEach ->
      @subject = @class.setCurrentAction

    it 'changes from commits', ->
      @class._location = stubLocation(pathname: '/foo/bar/merge_requests/1/commits')

      expect(@subject('notes')).toBe('/foo/bar/merge_requests/1')
      expect(@subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs')

    it 'changes from diffs', ->
      @class._location = stubLocation(pathname: '/foo/bar/merge_requests/1/diffs')

      expect(@subject('notes')).toBe('/foo/bar/merge_requests/1')
      expect(@subject('commits')).toBe('/foo/bar/merge_requests/1/commits')

54 55 56 57 58 59
    it 'changes from diffs.html', ->
      @class._location = stubLocation(pathname: '/foo/bar/merge_requests/1/diffs.html')

      expect(@subject('notes')).toBe('/foo/bar/merge_requests/1')
      expect(@subject('commits')).toBe('/foo/bar/merge_requests/1/commits')

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    it 'changes from notes', ->
      @class._location = stubLocation(pathname: '/foo/bar/merge_requests/1')

      expect(@subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs')
      expect(@subject('commits')).toBe('/foo/bar/merge_requests/1/commits')

    it 'includes search parameters and hash string', ->
      @class._location = stubLocation({
        pathname: '/foo/bar/merge_requests/1/diffs'
        search:   '?view=parallel'
        hash:     '#L15-35'
      })

      expect(@subject('show')).toBe('/foo/bar/merge_requests/1?view=parallel#L15-35')

    it 'replaces the current history state', ->
      @class._location = stubLocation(pathname: '/foo/bar/merge_requests/1')
      new_state = @subject('commits')

      expect(@spies.history).toHaveBeenCalledWith(
        {turbolinks: true, url: new_state},
        document.title,
        new_state
      )

    it 'treats "show" like "notes"', ->
      @class._location = stubLocation(pathname: '/foo/bar/merge_requests/1/commits')

      expect(@subject('show')).toBe('/foo/bar/merge_requests/1')